From df254333cab7989436aa0d6bf6ffca8346d41d10 Mon Sep 17 00:00:00 2001 From: Evilebot Tnawi Date: Fri, 7 Jun 2019 00:30:20 +0300 Subject: [PATCH] test: more (#1987) --- lib/utils/createLogger.js | 2 +- lib/utils/runOpen.js | 2 +- lib/utils/tryParseInt.js | 2 + .../utils/__snapshots__/findPort.test.js.snap | 2 +- test/server/utils/createLogger.test.js | 47 ++++++ test/server/utils/defaultPort.test.js | 9 ++ test/server/utils/defaultTo.test.js | 20 +++ test/server/utils/findPort.test.js | 6 +- .../getSocketServerImplementation.test.js | 8 +- test/server/utils/runOpen.test.js | 134 ++++++++++++++++++ test/server/utils/tryParseInt.test.js | 22 +++ 11 files changed, 244 insertions(+), 10 deletions(-) create mode 100644 test/server/utils/createLogger.test.js create mode 100644 test/server/utils/defaultPort.test.js create mode 100644 test/server/utils/defaultTo.test.js create mode 100644 test/server/utils/runOpen.test.js create mode 100644 test/server/utils/tryParseInt.test.js diff --git a/lib/utils/createLogger.js b/lib/utils/createLogger.js index 86479575b5..2f7d1cfbfb 100644 --- a/lib/utils/createLogger.js +++ b/lib/utils/createLogger.js @@ -2,7 +2,7 @@ const log = require('webpack-log'); -function createLogger(options) { +function createLogger(options = {}) { let level = options.logLevel || 'info'; if (options.noInfo === true) { diff --git a/lib/utils/runOpen.js b/lib/utils/runOpen.js index 50acf163bd..b499888356 100644 --- a/lib/utils/runOpen.js +++ b/lib/utils/runOpen.js @@ -11,7 +11,7 @@ function runOpen(uri, options, log) { openMessage += `: ${options.open}`; } - open(`${uri}${options.openPage || ''}`, openOptions).catch(() => { + return open(`${uri}${options.openPage || ''}`, openOptions).catch(() => { log.warn( `${openMessage}. If you are running in a headless environment, please do not use the --open flag` ); diff --git a/lib/utils/tryParseInt.js b/lib/utils/tryParseInt.js index 3aeb0d07a5..94eff3fa90 100644 --- a/lib/utils/tryParseInt.js +++ b/lib/utils/tryParseInt.js @@ -2,9 +2,11 @@ function tryParseInt(input) { const output = parseInt(input, 10); + if (Number.isNaN(output)) { return null; } + return output; } diff --git a/test/server/utils/__snapshots__/findPort.test.js.snap b/test/server/utils/__snapshots__/findPort.test.js.snap index eae0efcc61..5d2be6480f 100644 --- a/test/server/utils/__snapshots__/findPort.test.js.snap +++ b/test/server/utils/__snapshots__/findPort.test.js.snap @@ -1,3 +1,3 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -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"`; +exports[`findPort util should throws the error when the port isn't found 1`] = `"No open ports found in between 8080 and 8085"`; diff --git a/test/server/utils/createLogger.test.js b/test/server/utils/createLogger.test.js new file mode 100644 index 0000000000..44e4b3feaf --- /dev/null +++ b/test/server/utils/createLogger.test.js @@ -0,0 +1,47 @@ +'use strict'; + +const createLogger = require('../../../lib/utils/createLogger'); + +describe('createLogger util', () => { + it('should create logger without options', () => { + const logger = createLogger(); + + expect(logger.name).toBe('wds'); + expect(logger.currentLevel).toBe(2); + }); + + it('should create logger with logLevel option (debug)', () => { + const logger = createLogger({ logLevel: 'debug' }); + + expect(logger.name).toBe('wds'); + expect(logger.currentLevel).toBe(1); + }); + + it('should create logger with logLevel option (warn)', () => { + const logger = createLogger({ logLevel: 'warn' }); + + expect(logger.name).toBe('wds'); + expect(logger.currentLevel).toBe(3); + }); + + it('should create logger with noInfo option', () => { + const logger = createLogger({ noInfo: true }); + + expect(logger.name).toBe('wds'); + expect(logger.currentLevel).toBe(3); + }); + + it('should create logger with quiet option', () => { + const logger = createLogger({ quiet: true }); + + expect(logger.name).toBe('wds'); + expect(logger.currentLevel).toBe(5); + }); + + it('should create logger with logTime option', () => { + const logger = createLogger({ logTime: true }); + + expect(logger.name).toBe('wds'); + expect(logger.currentLevel).toBe(2); + }); +}); diff --git a/test/server/utils/defaultPort.test.js b/test/server/utils/defaultPort.test.js new file mode 100644 index 0000000000..865baceb42 --- /dev/null +++ b/test/server/utils/defaultPort.test.js @@ -0,0 +1,9 @@ +'use strict'; + +const defaultPort = require('../../../lib/utils/defaultPort'); + +describe('defaultPort util', () => { + it('should return value', () => { + expect(defaultPort).toBe(8080); + }); +}); diff --git a/test/server/utils/defaultTo.test.js b/test/server/utils/defaultTo.test.js new file mode 100644 index 0000000000..9f084b8939 --- /dev/null +++ b/test/server/utils/defaultTo.test.js @@ -0,0 +1,20 @@ +'use strict'; + +const defaultTo = require('../../../lib/utils/defaultTo'); + +const noop = () => {}; +const array = [1, 2, 3]; + +describe('defaultTo util', () => { + it('should returns value', () => { + expect(defaultTo(0, 200)).toBe(0); + expect(defaultTo(100, 200)).toBe(100); + expect(defaultTo('', 200)).toBe(''); + expect(defaultTo('string', 200)).toBe('string'); + expect(defaultTo(noop, 200)).toBe(noop); + expect(defaultTo(array, 200)).toEqual(array); + expect(defaultTo(null, 200)).toBe(200); + // eslint-disable-next-line no-undefined + expect(defaultTo(undefined, 200)).toBe(200); + }); +}); diff --git a/test/server/utils/findPort.test.js b/test/server/utils/findPort.test.js index d618a82505..c652a292fd 100644 --- a/test/server/utils/findPort.test.js +++ b/test/server/utils/findPort.test.js @@ -3,7 +3,7 @@ const http = require('http'); const findPort = require('../../../lib/utils/findPort'); -describe('findPort cli utility function', () => { +describe('findPort util', () => { let dummyServers = []; afterEach(() => { @@ -34,7 +34,7 @@ describe('findPort cli utility function', () => { }, Promise.resolve()); } - it('should return the port when the port is specified', () => { + it('should returns the port when the port is specified', () => { process.env.DEFAULT_PORT_RETRY = 5; return findPort(8082).then((port) => { @@ -54,7 +54,7 @@ describe('findPort cli utility function', () => { }); }); - it("should throw the error when the port isn't found", () => { + it("should throws the error when the port isn't found", () => { process.env.DEFAULT_PORT_RETRY = 5; return createDummyServers(10) diff --git a/test/server/utils/getSocketServerImplementation.test.js b/test/server/utils/getSocketServerImplementation.test.js index 51bcdf9143..0a157c39a9 100644 --- a/test/server/utils/getSocketServerImplementation.test.js +++ b/test/server/utils/getSocketServerImplementation.test.js @@ -4,7 +4,7 @@ const getSocketServerImplementation = require('../../../lib/utils/getSocketServe const SockJSServer = require('../../../lib/servers/SockJSServer'); describe('getSocketServerImplementation util', () => { - it("should work with serverMode: 'sockjs'", () => { + it("should works with string serverMode ('sockjs')", () => { let result; expect(() => { @@ -16,7 +16,7 @@ describe('getSocketServerImplementation util', () => { expect(result).toEqual(SockJSServer); }); - it('should work with serverMode: SockJSServer class', () => { + it('should works with serverMode (SockJSServer class)', () => { let result; expect(() => { @@ -28,7 +28,7 @@ describe('getSocketServerImplementation util', () => { expect(result).toEqual(SockJSServer); }); - it('should work with serverMode: SockJSServer full path', () => { + it('should work with serverMode (SockJSServer full path)', () => { let result; expect(() => { @@ -40,7 +40,7 @@ describe('getSocketServerImplementation util', () => { expect(result).toEqual(SockJSServer); }); - it('should throw with serverMode: bad path', () => { + it('should throws with serverMode (bad path)', () => { expect(() => { getSocketServerImplementation({ serverMode: '/bad/path/to/implementation', diff --git a/test/server/utils/runOpen.test.js b/test/server/utils/runOpen.test.js new file mode 100644 index 0000000000..51fce41300 --- /dev/null +++ b/test/server/utils/runOpen.test.js @@ -0,0 +1,134 @@ +'use strict'; + +const opn = require('opn'); +const runOpen = require('../../../lib/utils/runOpen'); + +jest.mock('opn'); + +describe('runOpen util', () => { + afterEach(() => { + opn.mockClear(); + }); + + describe('should open browser', () => { + beforeEach(() => { + opn.mockImplementation(() => Promise.resolve()); + }); + + it('on specify URL', () => { + return runOpen('https://example.com', {}, console).then(() => { + expect(opn.mock.calls[0]).toEqual(['https://example.com', {}]); + }); + }); + + it('on specify URL with page', () => { + return runOpen( + 'https://example.com', + { openPage: '/index.html' }, + console + ).then(() => { + expect(opn.mock.calls[0]).toEqual([ + 'https://example.com/index.html', + {}, + ]); + }); + }); + + it('on specify URL in Google Chrome', () => { + return runOpen( + 'https://example.com', + { open: 'Google Chrome' }, + console + ).then(() => { + expect(opn.mock.calls[0]).toEqual([ + 'https://example.com', + { app: 'Google Chrome' }, + ]); + }); + }); + + it('on specify URL with page in Google Chrome ', () => { + return runOpen( + 'https://example.com', + { open: 'Google Chrome', openPage: '/index.html' }, + console + ).then(() => { + expect(opn.mock.calls[0]).toEqual([ + 'https://example.com/index.html', + { app: 'Google Chrome' }, + ]); + }); + }); + }); + + describe('should not open browser', () => { + const logMock = { warn: jest.fn() }; + + beforeEach(() => { + opn.mockImplementation(() => Promise.reject()); + }); + + afterEach(() => { + logMock.warn.mockClear(); + }); + + it('on specify URL and log error', () => { + return runOpen('https://example.com', {}, logMock).then(() => { + expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot( + `"Unable to open browser. If you are running in a headless environment, please do not use the --open flag"` + ); + expect(opn.mock.calls[0]).toEqual(['https://example.com', {}]); + }); + }); + + it('on specify URL with page and log error', () => { + return runOpen( + 'https://example.com', + { openPage: '/index.html' }, + logMock + ).then(() => { + expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot( + `"Unable to open browser. If you are running in a headless environment, please do not use the --open flag"` + ); + expect(opn.mock.calls[0]).toEqual([ + 'https://example.com/index.html', + {}, + ]); + }); + }); + + it('on specify URL in Google Chrome and log error', () => { + return runOpen( + 'https://example.com', + { open: 'Google Chrome' }, + logMock + ).then(() => { + expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot( + `"Unable to open browser: Google Chrome. If you are running in a headless environment, please do not use the --open flag"` + ); + expect(opn.mock.calls[0]).toEqual([ + 'https://example.com', + { + app: 'Google Chrome', + }, + ]); + }); + }); + + it('on specify URL with page in Google Chrome and log error ', () => { + return runOpen( + 'https://example.com', + { open: 'Google Chrome', openPage: '/index.html' }, + logMock + ).then(() => { + expect(logMock.warn.mock.calls[0][0]).toMatchInlineSnapshot( + `"Unable to open browser: Google Chrome. If you are running in a headless environment, please do not use the --open flag"` + ); + expect(opn.mock.calls[0]).toEqual([ + 'https://example.com/index.html', + { app: 'Google Chrome' }, + ]); + }); + }); + }); +}); diff --git a/test/server/utils/tryParseInt.test.js b/test/server/utils/tryParseInt.test.js new file mode 100644 index 0000000000..fb2668149e --- /dev/null +++ b/test/server/utils/tryParseInt.test.js @@ -0,0 +1,22 @@ +'use strict'; + +const tryParseInt = require('../../../lib/utils/tryParseInt'); + +describe('tryParseInt util', () => { + it('should parser number as number', () => { + expect(tryParseInt(1)).toBe(1); + }); + + it('should parser string as number', () => { + expect(tryParseInt('1')).toBe(1); + }); + + it('should parser undefined as null', () => { + // eslint-disable-next-line no-undefined + expect(tryParseInt(undefined)).toBe(null); + }); + + it('should parser NaN as null', () => { + expect(tryParseInt(NaN)).toBe(null); + }); +});