Skip to content

Commit d13a058

Browse files
authored
refactor: tests (#3462)
1 parent 91daacc commit d13a058

17 files changed

+433
-347
lines changed

test/cli/basic.test.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,20 @@ describe('basic', () => {
6161
expect(normalizeStderr(stderr)).toMatchSnapshot('stderr');
6262
});
6363

64-
it('should accept the promise function of webpack.config.js', (done) => {
65-
testBin(
66-
false,
67-
path.resolve(__dirname, '../fixtures/promise-config/webpack.config.js')
68-
)
69-
.then((output) => {
70-
expect(output.exitCode).toEqual(0);
71-
done();
72-
})
73-
.catch((err) => {
74-
// for windows
75-
expect(err.stdout).toContain('main.js');
76-
done();
77-
});
64+
it('should accept the promise function of webpack.config.js', async () => {
65+
try {
66+
const { exitCode } = await testBin(
67+
false,
68+
path.resolve(
69+
__dirname,
70+
'../fixtures/promise-config/webpack.config.js'
71+
)
72+
);
73+
expect(exitCode).toEqual(0);
74+
} catch (err) {
75+
// for windows
76+
expect(err.stdout).toContain('main.js');
77+
}
7878
});
7979

8080
it('should exit the process when SIGINT is detected', (done) => {

test/integration/MultiCompiler.test.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ describe('multi compiler', () => {
1616

1717
afterAll(testServer.close);
1818

19-
it('should handle GET request to bundle', (done) => {
20-
req
21-
.get('/main.js')
22-
.expect('Content-Type', 'application/javascript; charset=utf-8')
23-
.expect(200, done);
19+
it('should handle GET request to bundle', async () => {
20+
const res = await req.get('/main.js');
21+
expect(res.headers['content-type']).toEqual(
22+
'application/javascript; charset=utf-8'
23+
);
24+
expect(res.status).toEqual(200);
2425
});
2526
});

test/integration/UniversalCompiler.test.js

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,25 @@ describe('universal compiler', () => {
1616

1717
afterAll(testServer.close);
1818

19-
it('client bundle should have the inlined the client runtime', (done) => {
20-
req
21-
.get('/client.js')
22-
.expect('Content-Type', 'application/javascript; charset=utf-8')
23-
.expect(200)
24-
.end((err, res) => {
25-
if (err) {
26-
return done(err);
27-
}
28-
expect(res.text).toContain('Hello from the client');
29-
expect(res.text).toContain('WebsocketClient');
30-
done();
31-
});
19+
it('client bundle should have the inlined the client runtime', async () => {
20+
const res = await req.get('/client.js');
21+
expect(res.headers['content-type']).toEqual(
22+
'application/javascript; charset=utf-8'
23+
);
24+
expect(res.status).toEqual(200);
25+
expect(res.text).toContain('Hello from the client');
26+
expect(res.text).toContain('WebsocketClient');
3227
});
3328

34-
it('server bundle should NOT have the inlined the client runtime', (done) => {
29+
it('server bundle should NOT have the inlined the client runtime', async () => {
3530
// we wouldn't normally request a server bundle
3631
// but we'll do it here to check the contents
37-
req
38-
.get('/server.js')
39-
.expect('Content-Type', 'application/javascript; charset=utf-8')
40-
.expect(200)
41-
.end((err, res) => {
42-
if (err) {
43-
return done(err);
44-
}
45-
expect(res.text).toContain('Hello from the server');
46-
expect(res.text).not.toContain('WebsocketClient');
47-
done();
48-
});
32+
const res = await req.get('/server.js');
33+
expect(res.headers['content-type']).toEqual(
34+
'application/javascript; charset=utf-8'
35+
);
36+
expect(res.status).toEqual(200);
37+
expect(res.text).toContain('Hello from the server');
38+
expect(res.text).not.toContain('WebsocketClient');
4939
});
5040
});

test/server/client-option.test.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ describe('client option', () => {
3535
expect(server.options.client.overlay).toBe(true);
3636
});
3737

38-
it('responds with a 200', (done) => {
39-
req.get('/ws').expect(200, done);
38+
it('responds with a 200', async () => {
39+
const res = await req.get('/ws');
40+
expect(res.status).toEqual(200);
4041
});
4142
});
4243

@@ -65,8 +66,9 @@ describe('client option', () => {
6566
req = request(`http://localhost:${port}`);
6667
});
6768

68-
it('responds with a 200 second', (done) => {
69-
req.get(path).expect(200, done);
69+
it('responds with a 200 second', async () => {
70+
const res = await req.get(path);
71+
expect(res.status).toEqual(200);
7072
});
7173
});
7274

test/server/compress-option.test.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ describe('compress option', () => {
2222

2323
afterAll(testServer.close);
2424

25-
it('request to bundle file', (done) => {
26-
req.get('/main.js').expect('Content-Encoding', 'gzip').expect(200, done);
25+
it('request to bundle file', async () => {
26+
const res = await req.get('/main.js');
27+
expect(res.headers['content-encoding']).toEqual('gzip');
28+
expect(res.status).toEqual(200);
2729
});
2830
});
2931

@@ -42,8 +44,10 @@ describe('compress option', () => {
4244

4345
afterAll(testServer.close);
4446

45-
it('request to bundle file', (done) => {
46-
req.get('/main.js').expect('Content-Encoding', 'gzip').expect(200, done);
47+
it('request to bundle file', async () => {
48+
const res = await req.get('/main.js');
49+
expect(res.headers['content-encoding']).toEqual('gzip');
50+
expect(res.status).toEqual(200);
4751
});
4852
});
4953

@@ -62,15 +66,11 @@ describe('compress option', () => {
6266

6367
afterAll(testServer.close);
6468

65-
it('request to bundle file', (done) => {
66-
req
67-
.get('/main.js')
68-
.expect((res) => {
69-
if (res.header['content-encoding']) {
70-
throw new Error('Expected `content-encoding` header is undefined.');
71-
}
72-
})
73-
.expect(200, done);
69+
it('request to bundle file', async () => {
70+
const res = await req.get('/main.js');
71+
// eslint-disable-next-line no-undefined
72+
expect(res.headers['content-encoding']).toEqual(undefined);
73+
expect(res.status).toEqual(200);
7474
});
7575
});
7676
});

test/server/headers-option.test.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ describe('headers option', () => {
2424

2525
afterAll(testServer.close);
2626

27-
it('GET request with headers', (done) => {
28-
req.get('/main').expect('X-Foo', '1').expect(200, done);
27+
it('GET request with headers', async () => {
28+
const res = await req.get('/main.js');
29+
expect(res.headers['x-foo']).toEqual('1');
30+
expect(res.status).toEqual(200);
2931
});
3032
});
3133

@@ -44,10 +46,12 @@ describe('headers option', () => {
4446

4547
afterAll(testServer.close);
4648

47-
it('GET request with headers as an array', (done) => {
49+
it('GET request with headers as an array', async () => {
4850
// https://github.com/webpack/webpack-dev-server/pull/1650#discussion_r254217027
4951
const expected = 'key1=value1, key2=value2';
50-
req.get('/main').expect('X-Bar', expected).expect(200, done);
52+
const res = await req.get('/main.js');
53+
expect(res.headers['x-bar']).toEqual(expected);
54+
expect(res.status).toEqual(200);
5155
});
5256
});
5357

@@ -68,10 +72,12 @@ describe('headers option', () => {
6872

6973
afterAll(testServer.close);
7074

71-
it('GET request with headers as a function', (done) => {
75+
it('GET request with headers as a function', async () => {
7276
// https://github.com/webpack/webpack-dev-server/pull/1650#discussion_r254217027
7377
const expected = 'key1=value1, key2=value2';
74-
req.get('/main').expect('X-Bar', expected).expect(200, done);
78+
const res = await req.get('/main.js');
79+
expect(res.headers['x-bar']).toEqual(expected);
80+
expect(res.status).toEqual(200);
7581
});
7682
});
7783

@@ -93,8 +99,10 @@ describe('headers option', () => {
9399

94100
afterAll(testServer.close);
95101

96-
it('GET request with headers', (done) => {
97-
req.get('/main.js').expect('X-Foo', '2').expect(200, done);
102+
it('GET request with headers', async () => {
103+
const res = await req.get('/main.js');
104+
expect(res.headers['x-foo']).toEqual('2');
105+
expect(res.status).toEqual(200);
98106
});
99107
});
100108
});

0 commit comments

Comments
 (0)