/ test / app.test.ts
app.test.ts
 1  import type { Server } from 'node:http';
 2  import url from 'node:url';
 3  import axios from 'axios';
 4  
 5  import app from '../src/app';
 6  
 7  const port = app.get('port') || 8998;
 8  const getUrl = (pathname?: string) =>
 9    url.format({
10      hostname: app.get('host') || 'localhost',
11      protocol: 'http',
12      port,
13      pathname,
14    });
15  
16  describe('Feathers application tests (with jest)', () => {
17    let server: Server;
18  
19    beforeAll(async () => {
20      const task = app.listen(port);
21      server = await task;
22    });
23  
24    afterAll((done) => {
25      server.close(done);
26    });
27  
28    it('starts and shows the index page', async () => {
29      expect.assertions(1);
30  
31      const { data } = await axios.get(getUrl());
32  
33      expect(data.indexOf('<html lang="en">')).not.toBe(-1);
34    });
35  
36    it('shows a 404 JSON error without stack trace', async () => {
37      expect.assertions(4);
38  
39      try {
40        await axios.get(getUrl('path/to/nowhere'));
41      } catch (error) {
42        const { response } = error;
43  
44        expect(response.status).toBe(404);
45        expect(response.data.code).toBe(404);
46        expect(response.data.message).toBe('Page not found');
47        expect(response.data.name).toBe('NotFound');
48      }
49    });
50  });