0
0
Expressframework~20 mins

Jest or Vitest setup for Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Jest test for an Express route?

Consider this Express app and Jest test. What will the test output?

Express
const express = require('express');
const request = require('supertest');

const app = express();
app.get('/hello', (req, res) => {
  res.status(200).send('Hello World');
});

test('GET /hello returns Hello World', async () => {
  const response = await request(app).get('/hello');
  return response.text;
});
ATypeError
B"hello world"
C"Hello World"
Dundefined
Attempts:
2 left
💡 Hint

Check the exact string sent by the Express route.

📝 Syntax
intermediate
2:00remaining
Which Vitest setup code correctly mocks an Express middleware?

Choose the correct Vitest code snippet to mock an Express middleware function.

A
import { vi } from 'vitest';
const mockMiddleware = vi.fn((req, res, next) => next());
Bconst mockMiddleware = jest.fn((req, res, next) => next());
C
import { mock } from 'vitest';
const mockMiddleware = mock((req, res, next) => next());
D
import { spy } from 'vitest';
const mockMiddleware = spy((req, res, next) => next());
Attempts:
2 left
💡 Hint

Vitest uses vi.fn to create mocks.

🔧 Debug
advanced
2:00remaining
Why does this Jest test for an Express async route fail with timeout?

Look at this Jest test for an async Express route. Why does it fail with a timeout?

Express
const express = require('express');
const request = require('supertest');

const app = express();
app.get('/data', async (req, res) => {
  await new Promise(resolve => setTimeout(resolve, 100));
  res.json({ success: true });
});

test('GET /data returns success true', () => {
  return request(app).get('/data').expect(200).expect('Content-Type', /json/).expect({ success: true });
});
AThe test lacks async/await or returning the promise, so Jest finishes before response.
BThe Express route does not call next(), causing the request to hang.
CThe test expects wrong status code 200 instead of 201.
DThe test uses .expect with an object which is invalid syntax.
Attempts:
2 left
💡 Hint

Check how Jest knows when async tests finish.

state_output
advanced
2:00remaining
What is the value of 'count' after this Vitest test runs?

Given this Express app and Vitest test, what is the final value of the variable count?

Express
import express from 'express';
import request from 'supertest';
import { describe, it, expect } from 'vitest';

let count = 0;
const app = express();
app.get('/increment', (req, res) => {
  count += 1;
  res.send('ok');
});

describe('Increment route', () => {
  it('increments count twice', async () => {
    await request(app).get('/increment');
    await request(app).get('/increment');
  });
});
Aundefined
B0
C1
D2
Attempts:
2 left
💡 Hint

Each GET request to /increment adds 1 to count.

🧠 Conceptual
expert
2:00remaining
Which option correctly explains why Vitest can run Express tests faster than Jest?

Vitest is often faster than Jest for testing Express apps. Why?

AVitest compiles Express code to native binaries before testing.
BVitest runs tests in the same process without spawning workers, reducing overhead.
CVitest uses a slower but more accurate virtual DOM for Express testing.
DVitest disables all asynchronous code to speed up tests.
Attempts:
2 left
💡 Hint

Think about how test runners handle processes and workers.