Skip to content

Commit df25433

Browse files
evilebottnawihiroppy
authored andcommitted
test: more (#1987)
1 parent 176283b commit df25433

11 files changed

+244
-10
lines changed

lib/utils/createLogger.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const log = require('webpack-log');
44

5-
function createLogger(options) {
5+
function createLogger(options = {}) {
66
let level = options.logLevel || 'info';
77

88
if (options.noInfo === true) {

lib/utils/runOpen.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function runOpen(uri, options, log) {
1111
openMessage += `: ${options.open}`;
1212
}
1313

14-
open(`${uri}${options.openPage || ''}`, openOptions).catch(() => {
14+
return open(`${uri}${options.openPage || ''}`, openOptions).catch(() => {
1515
log.warn(
1616
`${openMessage}. If you are running in a headless environment, please do not use the --open flag`
1717
);

lib/utils/tryParseInt.js

+2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
function tryParseInt(input) {
44
const output = parseInt(input, 10);
5+
56
if (Number.isNaN(output)) {
67
return null;
78
}
9+
810
return output;
911
}
1012

Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`findPort cli utility function should throw the error when the port isn't found 1`] = `"No open ports found in between 8080 and 8085"`;
3+
exports[`findPort util should throws the error when the port isn't found 1`] = `"No open ports found in between 8080 and 8085"`;
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
const createLogger = require('../../../lib/utils/createLogger');
4+
5+
describe('createLogger util', () => {
6+
it('should create logger without options', () => {
7+
const logger = createLogger();
8+
9+
expect(logger.name).toBe('wds');
10+
expect(logger.currentLevel).toBe(2);
11+
});
12+
13+
it('should create logger with logLevel option (debug)', () => {
14+
const logger = createLogger({ logLevel: 'debug' });
15+
16+
expect(logger.name).toBe('wds');
17+
expect(logger.currentLevel).toBe(1);
18+
});
19+
20+
it('should create logger with logLevel option (warn)', () => {
21+
const logger = createLogger({ logLevel: 'warn' });
22+
23+
expect(logger.name).toBe('wds');
24+
expect(logger.currentLevel).toBe(3);
25+
});
26+
27+
it('should create logger with noInfo option', () => {
28+
const logger = createLogger({ noInfo: true });
29+
30+
expect(logger.name).toBe('wds');
31+
expect(logger.currentLevel).toBe(3);
32+
});
33+
34+
it('should create logger with quiet option', () => {
35+
const logger = createLogger({ quiet: true });
36+
37+
expect(logger.name).toBe('wds');
38+
expect(logger.currentLevel).toBe(5);
39+
});
40+
41+
it('should create logger with logTime option', () => {
42+
const logger = createLogger({ logTime: true });
43+
44+
expect(logger.name).toBe('wds');
45+
expect(logger.currentLevel).toBe(2);
46+
});
47+
});

test/server/utils/defaultPort.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
const defaultPort = require('../../../lib/utils/defaultPort');
4+
5+
describe('defaultPort util', () => {
6+
it('should return value', () => {
7+
expect(defaultPort).toBe(8080);
8+
});
9+
});

test/server/utils/defaultTo.test.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const defaultTo = require('../../../lib/utils/defaultTo');
4+
5+
const noop = () => {};
6+
const array = [1, 2, 3];
7+
8+
describe('defaultTo util', () => {
9+
it('should returns value', () => {
10+
expect(defaultTo(0, 200)).toBe(0);
11+
expect(defaultTo(100, 200)).toBe(100);
12+
expect(defaultTo('', 200)).toBe('');
13+
expect(defaultTo('string', 200)).toBe('string');
14+
expect(defaultTo(noop, 200)).toBe(noop);
15+
expect(defaultTo(array, 200)).toEqual(array);
16+
expect(defaultTo(null, 200)).toBe(200);
17+
// eslint-disable-next-line no-undefined
18+
expect(defaultTo(undefined, 200)).toBe(200);
19+
});
20+
});

test/server/utils/findPort.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const http = require('http');
44
const findPort = require('../../../lib/utils/findPort');
55

6-
describe('findPort cli utility function', () => {
6+
describe('findPort util', () => {
77
let dummyServers = [];
88

99
afterEach(() => {
@@ -34,7 +34,7 @@ describe('findPort cli utility function', () => {
3434
}, Promise.resolve());
3535
}
3636

37-
it('should return the port when the port is specified', () => {
37+
it('should returns the port when the port is specified', () => {
3838
process.env.DEFAULT_PORT_RETRY = 5;
3939

4040
return findPort(8082).then((port) => {
@@ -54,7 +54,7 @@ describe('findPort cli utility function', () => {
5454
});
5555
});
5656

57-
it("should throw the error when the port isn't found", () => {
57+
it("should throws the error when the port isn't found", () => {
5858
process.env.DEFAULT_PORT_RETRY = 5;
5959

6060
return createDummyServers(10)

test/server/utils/getSocketServerImplementation.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const getSocketServerImplementation = require('../../../lib/utils/getSocketServe
44
const SockJSServer = require('../../../lib/servers/SockJSServer');
55

66
describe('getSocketServerImplementation util', () => {
7-
it("should work with serverMode: 'sockjs'", () => {
7+
it("should works with string serverMode ('sockjs')", () => {
88
let result;
99

1010
expect(() => {
@@ -16,7 +16,7 @@ describe('getSocketServerImplementation util', () => {
1616
expect(result).toEqual(SockJSServer);
1717
});
1818

19-
it('should work with serverMode: SockJSServer class', () => {
19+
it('should works with serverMode (SockJSServer class)', () => {
2020
let result;
2121

2222
expect(() => {
@@ -28,7 +28,7 @@ describe('getSocketServerImplementation util', () => {
2828
expect(result).toEqual(SockJSServer);
2929
});
3030

31-
it('should work with serverMode: SockJSServer full path', () => {
31+
it('should work with serverMode (SockJSServer full path)', () => {
3232
let result;
3333

3434
expect(() => {
@@ -40,7 +40,7 @@ describe('getSocketServerImplementation util', () => {
4040
expect(result).toEqual(SockJSServer);
4141
});
4242

43-
it('should throw with serverMode: bad path', () => {
43+
it('should throws with serverMode (bad path)', () => {
4444
expect(() => {
4545
getSocketServerImplementation({
4646
serverMode: '/bad/path/to/implementation',

test/server/utils/runOpen.test.js

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
'use strict';
2+
3+
const opn = require('opn');
4+
const runOpen = require('../../../lib/utils/runOpen');
5+
6+
jest.mock('opn');
7+
8+
describe('runOpen util', () => {
9+
afterEach(() => {
10+
opn.mockClear();
11+
});
12+
13+
describe('should open browser', () => {
14+
beforeEach(() => {
15+
opn.mockImplementation(() => Promise.resolve());
16+
});
17+
18+
it('on specify URL', () => {
19+
return runOpen('https://example.com', {}, console).then(() => {
20+
expect(opn.mock.calls[0]).toEqual(['https://example.com', {}]);
21+
});
22+
});
23+
24+
it('on specify URL with page', () => {
25+
return runOpen(
26+
'https://example.com',
27+
{ openPage: '/index.html' },
28+
console
29+
).then(() => {
30+
expect(opn.mock.calls[0]).toEqual([
31+
'https://example.com/index.html',
32+
{},
33+
]);
34+
});
35+
});
36+
37+
it('on specify URL in Google Chrome', () => {
38+
return runOpen(
39+
'https://example.com',
40+
{ open: 'Google Chrome' },
41+
console
42+
).then(() => {
43+
expect(opn.mock.calls[0]).toEqual([
44+
'https://example.com',
45+
{ app: 'Google Chrome' },
46+
]);
47+
});
48+
});
49+
50+
it('on specify URL with page in Google Chrome ', () => {
51+
return runOpen(
52+
'https://example.com',
53+
{ open: 'Google Chrome', openPage: '/index.html' },
54+
console
55+
).then(() => {
56+
expect(opn.mock.calls[0]).toEqual([
57+
'https://example.com/index.html',
58+
{ app: 'Google Chrome' },
59+
]);
60+
});
61+
});
62+
});
63+
64+
describe('should not open browser', () => {
65+
const logMock = { warn: jest.fn() };
66+
67+
beforeEach(() => {
68+
opn.mockImplementation(() => Promise.reject());
69+
});
70+
71+
afterEach(() => {
72+
logMock.warn.mockClear();
73+
});
74+
75+
it('on specify URL and log error', () => {
76+
return runOpen('https://example.com', {}, logMock).then(() => {
77+
expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot(
78+
`"Unable to open browser. If you are running in a headless environment, please do not use the --open flag"`
79+
);
80+
expect(opn.mock.calls[0]).toEqual(['https://example.com', {}]);
81+
});
82+
});
83+
84+
it('on specify URL with page and log error', () => {
85+
return runOpen(
86+
'https://example.com',
87+
{ openPage: '/index.html' },
88+
logMock
89+
).then(() => {
90+
expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot(
91+
`"Unable to open browser. If you are running in a headless environment, please do not use the --open flag"`
92+
);
93+
expect(opn.mock.calls[0]).toEqual([
94+
'https://example.com/index.html',
95+
{},
96+
]);
97+
});
98+
});
99+
100+
it('on specify URL in Google Chrome and log error', () => {
101+
return runOpen(
102+
'https://example.com',
103+
{ open: 'Google Chrome' },
104+
logMock
105+
).then(() => {
106+
expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot(
107+
`"Unable to open browser: Google Chrome. If you are running in a headless environment, please do not use the --open flag"`
108+
);
109+
expect(opn.mock.calls[0]).toEqual([
110+
'https://example.com',
111+
{
112+
app: 'Google Chrome',
113+
},
114+
]);
115+
});
116+
});
117+
118+
it('on specify URL with page in Google Chrome and log error ', () => {
119+
return runOpen(
120+
'https://example.com',
121+
{ open: 'Google Chrome', openPage: '/index.html' },
122+
logMock
123+
).then(() => {
124+
expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot(
125+
`"Unable to open browser: Google Chrome. If you are running in a headless environment, please do not use the --open flag"`
126+
);
127+
expect(opn.mock.calls[0]).toEqual([
128+
'https://example.com/index.html',
129+
{ app: 'Google Chrome' },
130+
]);
131+
});
132+
});
133+
});
134+
});

test/server/utils/tryParseInt.test.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
const tryParseInt = require('../../../lib/utils/tryParseInt');
4+
5+
describe('tryParseInt util', () => {
6+
it('should parser number as number', () => {
7+
expect(tryParseInt(1)).toBe(1);
8+
});
9+
10+
it('should parser string as number', () => {
11+
expect(tryParseInt('1')).toBe(1);
12+
});
13+
14+
it('should parser undefined as null', () => {
15+
// eslint-disable-next-line no-undefined
16+
expect(tryParseInt(undefined)).toBe(null);
17+
});
18+
19+
it('should parser NaN as null', () => {
20+
expect(tryParseInt(NaN)).toBe(null);
21+
});
22+
});

0 commit comments

Comments
 (0)