Skip to content

Commit 503f4bf

Browse files
authored
Merge pull request #338 from Automattic/update/testing
chore(dev-deps): update `jest` and `supertest`
2 parents 83f29aa + 4566bd4 commit 503f4bf

File tree

3 files changed

+2639
-1532
lines changed

3 files changed

+2639
-1532
lines changed

__tests__/server.tests.js

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,28 @@ const HEALTHCHECKURL = '/cache-healthcheck?';
77

88
describe( 'src/server', () => {
99
describe( 'should work with an express application', () => {
10-
it( 'should add a /cache-healthcheck? route returning 200 OK', async () => {
10+
it( 'should add a /cache-healthcheck? route returning 200 OK', () => {
1111
const expressServer = server( expressApp );
12-
const response = await request( expressServer.app ).get( HEALTHCHECKURL );
13-
14-
expect( response.statusCode ).toBe( 200 );
15-
expect( response.text ).toBe( 'ok' );
12+
return request( expressServer.app ).get( HEALTHCHECKURL ).expect( 200, 'ok' );
1613
} );
1714

18-
it( 'should keep already defined routes', async () => {
15+
it( 'should keep already defined routes', () => {
1916
expressApp.get( '/down', ( _req, res ) => {
2017
res.status( 501 ).end();
2118
} );
2219

2320
const expressServer = server( expressApp );
24-
const response = await request( expressServer.app ).get( '/down' );
25-
26-
expect( response.statusCode ).toBe( 501 );
21+
return request( expressServer.app ).get( '/down' ).expect( 501 );
2722
} );
2823

2924
it( 'should boot up a server on the provided PORT', async () => {
3025
const expressServerOnPort = server( expressApp, { PORT: 8000 } );
3126
expressServerOnPort.listen();
32-
const response = await request( 'http://localhost:8000' ).get( HEALTHCHECKURL );
33-
34-
expect( response.statusCode ).toBe( 200 );
35-
expressServerOnPort.close();
27+
try {
28+
await request( 'http://localhost:8000' ).get( HEALTHCHECKURL ).expect( 200 );
29+
} finally {
30+
expressServerOnPort.close();
31+
}
3632
} );
3733
} );
3834

@@ -59,45 +55,40 @@ describe( 'src/server', () => {
5955
} ).toThrow( 'Please include a requestHandler' );
6056
} );
6157

62-
it( 'should add a /cache-healthcheck? route returning 200 OK', async () => {
58+
it( 'should add a /cache-healthcheck? route returning 200 OK', () => {
6359
const httpServer = server( requestHandler );
64-
const response = await request( httpServer.app ).get( HEALTHCHECKURL );
65-
66-
expect( response.statusCode ).toBe( 200 );
67-
expect( response.text ).toBe( 'ok' );
60+
return request( httpServer.app ).get( HEALTHCHECKURL ).expect( 200, 'ok' );
6861
} );
6962

70-
it( 'should respond to /cache-healthcheck? route and not forward the request', async () => {
63+
it( 'should respond to /cache-healthcheck? route and not forward the request', () => {
7164
const httpServer = server( requestHandler );
72-
const response = await request( httpServer.app ).get( HEALTHCHECKURL );
73-
74-
expect( response.statusCode ).toBe( 200 );
75-
expect( response.text ).toBe( 'ok' );
76-
expect( mock ).not.toHaveBeenCalled();
65+
return request( httpServer.app )
66+
.get( HEALTHCHECKURL )
67+
.expect( 200, 'ok' )
68+
.expect( () => {
69+
expect( mock ).not.toHaveBeenCalled();
70+
} );
7771
} );
7872

79-
it( 'should match defined routes', async () => {
73+
it( 'should match defined routes', () => {
8074
const httpServer = server( requestHandler );
81-
const response = await request( httpServer.app ).get( '/custom' );
82-
83-
expect( response.statusCode ).toBe( 201 );
75+
return request( httpServer.app ).get( '/custom' ).expect( 201 );
8476
} );
8577

86-
it( 'should return default response if no route is matched', async () => {
78+
it( 'should return default response if no route is matched', () => {
8779
const httpServer = server( requestHandler );
8880

89-
const response = await request( httpServer.app ).get( '/notfound' );
90-
91-
expect( response.statusCode ).toBe( 404 );
81+
return request( httpServer.app ).get( '/notfound' ).expect( 404 );
9282
} );
9383

9484
it( 'should boot up a server on the provided PORT', async () => {
9585
const httpServerOnPort = server( requestHandler, { PORT: 8000 } );
9686
httpServerOnPort.listen();
97-
const response = await request( 'http://localhost:8000' ).get( HEALTHCHECKURL );
98-
99-
expect( response.statusCode ).toBe( 200 );
100-
httpServerOnPort.close();
87+
try {
88+
await request( 'http://localhost:8000' ).get( HEALTHCHECKURL ).expect( 200 );
89+
} finally {
90+
httpServerOnPort.close();
91+
}
10192
} );
10293
} );
10394
} );

0 commit comments

Comments
 (0)