Skip to content

Commit 28a4a07

Browse files
authored
Add some additional specs and set up jest coverage (#542)
* collect coverage report * getServePath spec * add getServer spec
1 parent efad3a5 commit 28a4a07

File tree

5 files changed

+84
-2
lines changed

5 files changed

+84
-2
lines changed

jest.config.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ module.exports = {
33
testMatch: ['**/__tests__/**/*.+(ts|js)', '**/?(*.)+(spec|test).+(ts|js)'],
44
setupFiles: ['<rootDir>/test/setup.js'],
55
transform: {},
6+
collectCoverageFrom: ['**/*.js'],
67
};

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"local": "node -e 'import(\"./src/index.js\").then(index => index.onPostBuild());'",
88
"lint": "eslint 'src/**/*.js'",
99
"format": "prettier --write 'src/**/*.js'",
10-
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
10+
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --collect-coverage",
1111
"format:ci": "prettier --check 'src/**/*.js'"
1212
},
1313
"keywords": [
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import getServePath from '.';
2+
3+
describe('getServePath', () => {
4+
it('returns undefined for dir thats not a string', () => {
5+
expect(getServePath(2)).toEqual({ serveDir: undefined });
6+
});
7+
8+
it('returns undefined for subdir thats not a string', () => {
9+
expect(getServePath(2, 2)).toEqual({ serveDir: undefined });
10+
});
11+
12+
it('returns joined path for serveDir', () => {
13+
expect(getServePath('example', 'path')).toEqual({
14+
serveDir: 'example/path',
15+
});
16+
});
17+
});

src/lib/get-server/get-server.test.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
jest.unstable_mockModule('chalk', () => {
2+
return {
3+
default: {
4+
magenta: (m) => m,
5+
},
6+
};
7+
});
8+
9+
const mockedExpress = () => ({
10+
use: jest.fn(),
11+
listen: jest.fn(),
12+
});
13+
const mockStatic = jest.fn();
14+
Object.defineProperty(mockedExpress, 'static', { value: mockStatic });
15+
16+
jest.unstable_mockModule('express', () => {
17+
return {
18+
default: mockedExpress,
19+
};
20+
});
21+
const getServer = (await import('.')).default;
22+
23+
describe('getServer', () => {
24+
beforeEach(() => {
25+
jest.clearAllMocks();
26+
});
27+
28+
it('returns a mock server if audit URL is defined', () => {
29+
const mockListen = jest.fn();
30+
console.log = jest.fn();
31+
32+
const { server } = getServer({ auditUrl: '/' });
33+
expect(server.listen).toBeDefined();
34+
server.listen(mockListen);
35+
expect(mockListen).toHaveBeenCalled();
36+
expect(console.log.mock.calls[0][0]).toEqual('Scanning url /');
37+
38+
expect(server.close).toBeDefined();
39+
expect(server.close()).toBeUndefined();
40+
expect(server.url).toEqual('/');
41+
});
42+
43+
it('throws an error if no audit URL and no serveDir', () => {
44+
expect(() => getServer({})).toThrow('Empty publish dir');
45+
});
46+
47+
it('returns an express server if no audit URL and a serveDir', () => {
48+
const { server } = getServer({ serveDir: 'dir' });
49+
expect(mockStatic).toHaveBeenCalled();
50+
51+
// Check we log when we start serving directory
52+
server.listen(jest.fn());
53+
expect(console.log.mock.calls[0][0]).toEqual(
54+
'Serving and scanning site from directory dir',
55+
);
56+
57+
expect(server.url).toEqual('http://localhost:5100');
58+
59+
// Check close method closes the given instance
60+
const close = jest.fn();
61+
server.close({ close });
62+
expect(close).toHaveBeenCalled();
63+
});
64+
});

0 commit comments

Comments
 (0)