Skip to content

Commit

Permalink
refactor: migrate tests on async await (#3460)
Browse files Browse the repository at this point in the history
* refactor: migrate tests on async await

* refactor: migrate tests on async await

* refactor: migrate tests on async await
  • Loading branch information
anshumanv authored Jun 19, 2021
1 parent 591b810 commit 13597a2
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 221 deletions.
79 changes: 33 additions & 46 deletions test/server/Server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,75 +521,63 @@ describe('Server', () => {
);
}

it('should returns the port when the port is specified', () => {
it('should returns the port when the port is specified', async () => {
process.env.WEBPACK_DEV_SERVER_PORT_RETRY = 1;

return Server.getFreePort(8082).then((freePort) => {
expect(freePort).toEqual(8082);
});
const freePort = await Server.getFreePort(8082);
expect(freePort).toEqual(8082);
});

it('should returns the port when the port is null', () => {
it('should returns the port when the port is null', async () => {
const retryCount = 2;

process.env.WEBPACK_DEV_SERVER_PORT_RETRY = 2;

return createDummyServers(retryCount)
.then(() => Server.getFreePort(null))
.then((freePort) => {
expect(freePort).toEqual(60000 + retryCount);
});
await createDummyServers(retryCount);
const freePort = await Server.getFreePort(null);
expect(freePort).toEqual(60000 + retryCount);
});

it('should returns the port when the port is undefined', () => {
it('should returns the port when the port is undefined', async () => {
const retryCount = 3;

process.env.WEBPACK_DEV_SERVER_PORT_RETRY = 3;

return (
createDummyServers(retryCount)
// eslint-disable-next-line no-undefined
.then(() => Server.getFreePort(undefined))
.then((freePort) => {
expect(freePort).toEqual(60000 + retryCount);
})
);
await createDummyServers(retryCount);
// eslint-disable-next-line no-undefined
const freePort = await Server.getFreePort(undefined);
expect(freePort).toEqual(60000 + retryCount);
});

it('should retry finding the port for up to defaultPortRetry times (number)', () => {
const retryCount = 4;
it('should retry finding the port for up to defaultPortRetry times (number)', async () => {
const retryCount = 3;

process.env.WEBPACK_DEV_SERVER_PORT_RETRY = retryCount;

return createDummyServers(retryCount)
.then(() => Server.getFreePort())
.then((freePort) => {
expect(freePort).toEqual(60000 + retryCount);
});
await createDummyServers(retryCount);
const freePort = await Server.getFreePort();
expect(freePort).toEqual(60000 + retryCount);
});

it('should retry finding the port for up to defaultPortRetry times (string)', () => {
const retryCount = 5;
it('should retry finding the port for up to defaultPortRetry times (string)', async () => {
const retryCount = 3;

process.env.WEBPACK_DEV_SERVER_PORT_RETRY = `${retryCount}`;

return createDummyServers(retryCount)
.then(() => Server.getFreePort())
.then((freePort) => {
expect(freePort).toEqual(60000 + retryCount);
});
await createDummyServers(retryCount);
const freePort = await Server.getFreePort();
expect(freePort).toEqual(60000 + retryCount);
});

it('should retry finding the port when serial ports are busy', () => {
// TODO: fix me, Flaky on CI
it('should retry finding the port when serial ports are busy', async () => {
const busyPorts = [60000, 60001, 60002, 60003, 60004, 60005, 60006];

process.env.WEBPACK_DEV_SERVER_PORT_RETRY = 6;

return createDummyServers(busyPorts)
.then(() => Server.getFreePort())
.then((freePort) => {
expect(freePort).toEqual(60000 + busyPorts.length);
});
await createDummyServers(busyPorts);
const freePort = await Server.getFreePort();
expect(freePort).toEqual(60000 + busyPorts.length);
});

it("should throws the error when the port isn't found", async () => {
Expand All @@ -605,17 +593,16 @@ describe('Server', () => {

try {
await Server.getFreePort();
} catch (error) {
expect(error.message).toMatchSnapshot();
} catch (err) {
expect(err.message).toMatchSnapshot();
}
});

it('should work with findPort util', () => {
process.env.WEBPACK_DEV_SERVER_PORT_RETRY = 5;
it('should work with findPort util', async () => {
process.env.DEFAULT_PORT_RETRY = 5;

return findPort(8082).then((freePort) => {
expect(freePort).toEqual(8082);
});
const freePort = await findPort(8082);
expect(freePort).toEqual(8082);
});
});
});
22 changes: 8 additions & 14 deletions test/server/client-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,10 @@ describe('client option', () => {
},
port,
},
() => {
request(server.app)
.get('/main.js')
.then((res) => {
expect(res.text).not.toMatch(/client\/index\.js/);
})
.then(done, done);
async () => {
const res = await request(server.app).get('/main.js');
expect(res.text).not.toMatch(/client\/index\.js/);
done();
}
);
});
Expand All @@ -100,13 +97,10 @@ describe('client option', () => {
},
port,
},
() => {
request(server.app)
.get('/main.js')
.then((res) => {
expect(res.text).not.toMatch(/webpack\/hot\/dev-server\.js/);
})
.then(done, done);
async () => {
const res = await request(server.app).get('/main.js');
expect(res.text).not.toMatch(/webpack\/hot\/dev-server\.js/);
done();
}
);
});
Expand Down
12 changes: 4 additions & 8 deletions test/server/hot-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,10 @@ describe('hot option', () => {

afterAll(testServer.close);

it('should NOT include hot script in the bundle', (done) => {
req
.get('/main.js')
.expect(200)
.then(({ text }) => {
expect(text).not.toMatch(/webpack\/hot\/dev-server\.js/);
done();
});
it('should NOT include hot script in the bundle', async () => {
const res = await req.get('/main.js');
expect(res.status).toEqual(200);
expect(res.text).not.toMatch(/webpack\/hot\/dev-server\.js/);
});
});

Expand Down
13 changes: 5 additions & 8 deletions test/server/http2-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,11 @@ describe('http2 option', () => {
req = request(server.app);
});

it('Request to index', (done) => {
req
.get('/')
.expect(200, /Heyo/)
.then(({ res }) => {
expect(res.httpVersion).not.toEqual('2.0');
done();
});
it('Request to index', async () => {
const res = await req.get('/');
expect(res.status).toEqual(200);
expect(res.text).toContain('Heyo');
expect(res.res.httpVersion).not.toEqual('2.0');
});

afterAll(testServer.close);
Expand Down
30 changes: 13 additions & 17 deletions test/server/onAfterSetupMiddleware-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,17 @@ describe('onAfterSetupMiddleware option', () => {

afterAll(testServer.close);

it('should handle after route', () =>
req
.get('/after/some/path')
.expect('Content-Type', 'text/html; charset=utf-8')
.expect(200)
.then((response) => {
expect(response.text).toBe('after');
}));

it('should handle POST requests to after route', () =>
req
.post('/after/some/path')
.expect('Content-Type', 'text/html; charset=utf-8')
.expect(200)
.then((response) => {
expect(response.text).toBe('after POST');
}));
it('should handle after route', async () => {
const res = await req.get('/after/some/path');
expect(res.headers['content-type']).toEqual('text/html; charset=utf-8');
expect(res.status).toEqual(200);
expect(res.text).toBe('after');
});

it('should handle POST requests to after route', async () => {
const res = await req.post('/after/some/path');
expect(res.headers['content-type']).toEqual('text/html; charset=utf-8');
expect(res.status).toEqual(200);
expect(res.text).toBe('after POST');
});
});
14 changes: 6 additions & 8 deletions test/server/onBeforeSetupMiddleware-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ describe('onBeforeSetupMiddleware option', () => {

afterAll(testServer.close);

it('should handle before route', () =>
req
.get('/before/some/path')
.expect('Content-Type', 'text/html; charset=utf-8')
.expect(200)
.then((response) => {
expect(response.text).toBe('before');
}));
it('should handle before route', async () => {
const res = await req.get('/before/some/path');
expect(res.headers['content-type']).toEqual('text/html; charset=utf-8');
expect(res.status).toEqual(200);
expect(res.text).toBe('before');
});
});
89 changes: 39 additions & 50 deletions test/server/utils/DevServerPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('DevServerPlugin util', () => {
return compiler.options.entry;
}

it('should preserves dynamic entry points', (done) => {
it('should preserves dynamic entry points', async () => {
let i = 0;
const webpackOptions = {
// simulate dynamic entry
Expand Down Expand Up @@ -42,33 +42,27 @@ describe('DevServerPlugin util', () => {

plugin.apply(compiler);

getEntries(compiler).then((entries) => {
expect(typeof entries).toEqual('function');

entries()
.then((entryFirstRun) =>
entries().then((entrySecondRun) => {
if (isWebpack5) {
expect(entryFirstRun.main.import.length).toEqual(1);
expect(entryFirstRun.main.import[0]).toEqual('./src-1.js');

expect(entrySecondRun.main.import.length).toEqual(1);
expect(entrySecondRun.main.import[0]).toEqual('./src-2.js');
} else {
expect(entryFirstRun.length).toEqual(3);
expect(entryFirstRun[2]).toEqual('./src-1.js');

expect(entrySecondRun.length).toEqual(3);
expect(entrySecondRun[2]).toEqual('./src-2.js');
}
done();
})
)
.catch(done);
});
const entries = await getEntries(compiler);
expect(typeof entries).toEqual('function');

const entryFirstRun = await entries();
const entrySecondRun = await entries();
if (isWebpack5) {
expect(entryFirstRun.main.import.length).toEqual(1);
expect(entryFirstRun.main.import[0]).toEqual('./src-1.js');

expect(entrySecondRun.main.import.length).toEqual(1);
expect(entrySecondRun.main.import[0]).toEqual('./src-2.js');
} else {
expect(entryFirstRun.length).toEqual(3);
expect(entryFirstRun[2]).toEqual('./src-1.js');

expect(entrySecondRun.length).toEqual(3);
expect(entrySecondRun[2]).toEqual('./src-2.js');
}
});

it('should preserves asynchronous dynamic entry points', (done) => {
it('should preserves asynchronous dynamic entry points', async () => {
let i = 0;
const webpackOptions = {
// simulate async dynamic entry
Expand Down Expand Up @@ -96,30 +90,25 @@ describe('DevServerPlugin util', () => {

const plugin = new DevServerPlugin(devServerOptions);
plugin.apply(compiler);
getEntries(compiler).then((entries) => {
expect(typeof entries).toEqual('function');

entries()
.then((entryFirstRun) =>
entries().then((entrySecondRun) => {
if (isWebpack5) {
expect(entryFirstRun.main.import.length).toEqual(1);
expect(entryFirstRun.main.import[0]).toEqual('./src-1.js');

expect(entrySecondRun.main.import.length).toEqual(1);
expect(entrySecondRun.main.import[0]).toEqual('./src-2.js');
} else {
expect(entryFirstRun.length).toEqual(3);
expect(entryFirstRun[2]).toEqual('./src-1.js');

expect(entrySecondRun.length).toEqual(3);
expect(entrySecondRun[2]).toEqual('./src-2.js');
}
done();
})
)
.catch(done);
});

const entries = await getEntries(compiler);
expect(typeof entries).toEqual('function');

const entryFirstRun = await entries();
const entrySecondRun = await entries();
if (isWebpack5) {
expect(entryFirstRun.main.import.length).toEqual(1);
expect(entryFirstRun.main.import[0]).toEqual('./src-1.js');

expect(entrySecondRun.main.import.length).toEqual(1);
expect(entrySecondRun.main.import[0]).toEqual('./src-2.js');
} else {
expect(entryFirstRun.length).toEqual(3);
expect(entryFirstRun[2]).toEqual('./src-1.js');

expect(entrySecondRun.length).toEqual(3);
expect(entrySecondRun[2]).toEqual('./src-2.js');
}
});

it("should doesn't add the HMR plugin if not hot and no plugins", () => {
Expand Down
Loading

0 comments on commit 13597a2

Please sign in to comment.