Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix jest usage and add retry #2838

Merged
merged 4 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ module.exports = {
testMatch: ['**/test/**/*.test.js'],
setupFilesAfterEnv: ['<rootDir>/setupTest.js'],
globalSetup: '<rootDir>/globalSetupTest.js',
testRunner: 'jest-circus/runner',
testSequencer: '<rootDir>/test/testSequencer.js',
};
3,488 changes: 3,038 additions & 450 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@
"html-loader": "^1.1.0",
"html-webpack-plugin": "^4.3.0",
"husky": "^4.2.5",
"jest": "^26.3.0",
"jest": "^26.6.3",
"jest-circus": "^26.6.3",
"jest-junit": "^11.1.0",
"less": "^3.12.2",
"less-loader": "^6.2.0",
Expand Down
3 changes: 3 additions & 0 deletions setupTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@

process.env.CHOKIDAR_USEPOLLING = true;
jest.setTimeout(120000);

// retry 3 times for flaky tests
jest.retryTimes(3);
2 changes: 1 addition & 1 deletion test/e2e/ClientOptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ describe('Client console.log', () => {
})`;
options = { ...mode, ...options };
const testOptions = Object.assign({}, baseOptions, options);
await it(title, async (done) => {
await it(title, (done) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test functions cannot both take a 'done' callback and return something. Either use a 'done' callback, or return a promise.

testServer.startAwaitingCompilation(config, testOptions, async () => {
const res = [];
const { page, browser } = await runBrowser();
Expand Down
6 changes: 3 additions & 3 deletions test/options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ describe('options', () => {

afterAll((done) => {
if (server) {
server.close(() => {
done();
});
server.close(done);
} else {
done();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done was not called if the server was null, causing Exceeded timeout of 20000 ms for a hook..

}
});

Expand Down
4 changes: 1 addition & 3 deletions test/server/https-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ describe('https option', () => {
req.get('/').expect(200, /Heyo/, done);
});

afterAll(() => {
testServer.close();
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testServer.close requires a callback, causing TypeError: done is not a function.

afterAll(testServer.close);
});

describe('as an object when ca, pfx, key and cert are buffer', () => {
Expand Down
23 changes: 9 additions & 14 deletions test/server/utils/runBonjour.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,29 @@ jest.mock('os', () => {
});

describe('runBonjour', () => {
let mock;
let publish = jest.fn();
let unpublishAll = jest.fn();
Copy link
Contributor Author

@ylemkimon ylemkimon Nov 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variables referenced outside of jest.mock() now requires to be prefixed with mock.

const mockPublish = jest.fn();
const mockUnpublishAll = jest.fn();

beforeAll(() => {
mock = jest.mock('bonjour', () => () => {
jest.mock('bonjour', () => () => {
return {
publish,
unpublishAll,
publish: mockPublish,
unpublishAll: mockUnpublishAll,
};
});
});

afterEach(() => {
publish = jest.fn();
unpublishAll = jest.fn();
});

afterAll(() => {
mock.mockRestore();
mockPublish.mockClear();
mockUnpublishAll.mockClear();
});

it('should call bonjour.publish', () => {
runBonjour({
port: 1111,
});

expect(publish.mock.calls[0][0]).toMatchSnapshot();
expect(mockPublish.mock.calls[0][0]).toMatchSnapshot();
});

it('should call bonjour.publish with different name for different ports', () => {
Expand All @@ -47,7 +42,7 @@ describe('runBonjour', () => {
port: 2222,
});

const calls = publish.mock.calls;
const calls = mockPublish.mock.calls;

expect(calls[0][0].name).not.toEqual(calls[1][0].name);
});
Expand Down