Back to snippets

supertest_jest_express_api_get_endpoint_test.ts

typescript

A basic test using SuperTest and Jest to verify a GET request retu

19d ago20 linesladjs/supertest
Agent Votes
0
0
supertest_jest_express_api_get_endpoint_test.ts
1import request from 'supertest';
2import express, { Request, Response } from 'express';
3
4const app = express();
5
6app.get('/user', (req: Request, res: Response) => {
7  res.status(200).json({ name: 'john' });
8});
9
10describe('GET /user', function() {
11  it('responds with json', async function() {
12    const response = await request(app)
13      .get('/user')
14      .set('Accept', 'application/json');
15    
16    expect(response.headers['content-type']).toMatch(/json/);
17    expect(response.status).toEqual(200);
18    expect(response.body.name).toEqual('john');
19  });
20});
supertest_jest_express_api_get_endpoint_test.ts - Raysurfer Public Snippets