From 75000424d42a5e1053747ac0f06abc19cc8c8344 Mon Sep 17 00:00:00 2001 From: Patryk Mrukot Date: Tue, 30 Jan 2018 15:41:47 +0100 Subject: [PATCH 1/4] chore: Upgrade roc-repo version, add jest and update roc.config.js --- .eslintrc | 4 ++++ .gitignore | 2 ++ package.json | 3 ++- roc.config.js | 7 +++++-- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/.eslintrc b/.eslintrc index ece1857..9b1227b 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,4 +1,8 @@ { + "env": { + "jest": true + }, + "extends": "airbnb", "parser": "babel-eslint", diff --git a/.gitignore b/.gitignore index f105689..aeb2a2b 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ lib build package-lock.json yarn.lock +.idea +coverage/ diff --git a/package.json b/package.json index cdf4d88..0b0316d 100644 --- a/package.json +++ b/package.json @@ -13,8 +13,9 @@ "eslint-plugin-import": "^1.16.0", "eslint-plugin-jsx-a11y": "2.2.1", "eslint-plugin-react": "6.2.0", + "jest": "^22.0.0", "roc": "^1.0.0-rc.23", - "roc-plugin-repo": "0.0.25", + "roc-plugin-repo": "0.1.6", "roc-plugin-repo-roc": "0.0.1" } } diff --git a/roc.config.js b/roc.config.js index 15bf512..2fec5c5 100644 --- a/roc.config.js +++ b/roc.config.js @@ -1,10 +1,9 @@ module.exports = { settings: { repo: { - targets: ['cjs'], babelPresetEnv: { targets: { - node: '6', + node: '8', }, }, mono: [ @@ -13,6 +12,10 @@ module.exports = { 'packages', 'plugins', ], + release: { + includeBody: true, + changelogTypes: true, + }, }, }, }; From 416895ba789a2ebf0806b7c5757fe507f1628f2e Mon Sep 17 00:00:00 2001 From: Patryk Mrukot Date: Tue, 30 Jan 2018 15:41:56 +0100 Subject: [PATCH 2/4] feat(roc-package-web-app): Upgrade package to use Koa@2 BREAKING CHANGE: Middlewares need to be defined as an async funcions instead of generators. --- packages/roc-package-web-app/package.json | 26 ++++---- .../src/app/createServer.js | 8 +-- ...iddlewares.js => defaultKoaMiddlewares.js} | 17 ++--- .../src/app/defaultKoaMiddlewares.spec.js | 64 +++++++++++++++++++ .../src/app/middlewares/accesslog.js | 18 ++++++ .../src/app/middlewares/accesslog.spec.js | 52 +++++++++++++++ .../app/{ => middlewares}/basepathSupport.js | 10 +-- .../app/middlewares/basepathSupport.spec.js | 39 +++++++++++ 8 files changed, 204 insertions(+), 30 deletions(-) rename packages/roc-package-web-app/src/app/{middlewares.js => defaultKoaMiddlewares.js} (68%) create mode 100644 packages/roc-package-web-app/src/app/defaultKoaMiddlewares.spec.js create mode 100644 packages/roc-package-web-app/src/app/middlewares/accesslog.js create mode 100644 packages/roc-package-web-app/src/app/middlewares/accesslog.spec.js rename packages/roc-package-web-app/src/app/{ => middlewares}/basepathSupport.js (82%) create mode 100644 packages/roc-package-web-app/src/app/middlewares/basepathSupport.spec.js diff --git a/packages/roc-package-web-app/package.json b/packages/roc-package-web-app/package.json index 60c41a3..635aef0 100644 --- a/packages/roc-package-web-app/package.json +++ b/packages/roc-package-web-app/package.json @@ -26,21 +26,21 @@ "dependencies": { "config": "~1.16.0", "debug": "~2.2.0", - "koa": "~1.1.1", - "koa-accesslog": "~0.0.2", - "koa-add-trailing-slashes": "~1.1.0", - "koa-compressor": "~1.0.3", + "koa": "~2.4.1", + "koa-add-trailing-slashes": "~2.0.1", + "koa-compress": "~2.0.0", "koa-conditional-get": "~1.0.3", - "koa-errors": "~1.0.1", - "koa-etag": "~2.0.0", - "koa-favicon": "~1.2.0", - "koa-helmet": "~0.3.0", - "koa-logger": "~1.3.0", - "koa-lowercase-path": "~1.0.0", - "koa-normalize-path": "~1.0.0", - "koa-remove-trailing-slashes": "~1.0.0", - "koa-static": "~2.0.0", + "koa-error": "~3.1.1", + "koa-etag": "~3.0.0", + "koa-favicon": "~2.0.0", + "koa-helmet": "~3.3.0", + "koa-logger": "~3.1.0", + "koa-lowercase-path": "~2.0.0", + "koa-normalize-path": "~2.0.0", + "koa-remove-trailing-slashes": "~2.0.0", + "koa-static": "~4.0.2", "lodash": "4.13.1", + "moment": "~2.20.1", "roc": "^1.0.0-rc.23", "roc-package-webpack-node": "^1.0.0", "roc-package-webpack-web": "^1.0.0" diff --git a/packages/roc-package-web-app/src/app/createServer.js b/packages/roc-package-web-app/src/app/createServer.js index 25b5a2a..267407d 100644 --- a/packages/roc-package-web-app/src/app/createServer.js +++ b/packages/roc-package-web-app/src/app/createServer.js @@ -3,7 +3,7 @@ import http from 'http'; import https from 'https'; import debug from 'debug'; -import koa from 'koa'; +import Koa from 'koa'; import serve from 'koa-static'; import addTrailingSlash from 'koa-add-trailing-slashes'; import normalizePath from 'koa-normalize-path'; @@ -11,7 +11,7 @@ import lowercasePath from 'koa-lowercase-path'; import removeTrailingSlash from 'koa-remove-trailing-slashes'; import { merge, getSettings, getAbsolutePath } from 'roc'; -import basepathSupport from './basepathSupport'; +import basepathSupport from './middlewares/basepathSupport'; /** * Creates a server instance. @@ -41,7 +41,7 @@ export default function createServer(options = {}, beforeUserMiddlewares = [], { const logger = debug('roc:server'); logger.log = console.info.bind(console); - const server = koa(); + const server = new Koa(); const runtimeSettings = merge(getSettings('runtime'), options); // Add support for rocPath @@ -49,7 +49,7 @@ export default function createServer(options = {}, beforeUserMiddlewares = [], { if (useDefaultKoaMiddlewares) { // eslint-disable-next-line - const middlewares = require('./middlewares').default(runtimeSettings, { dev, dist }); + const middlewares = require('./defaultKoaMiddlewares').default(runtimeSettings, { dev, dist }); middlewares.forEach((middleware) => server.use(middleware)); } diff --git a/packages/roc-package-web-app/src/app/middlewares.js b/packages/roc-package-web-app/src/app/defaultKoaMiddlewares.js similarity index 68% rename from packages/roc-package-web-app/src/app/middlewares.js rename to packages/roc-package-web-app/src/app/defaultKoaMiddlewares.js index 205529e..75b1e4c 100644 --- a/packages/roc-package-web-app/src/app/middlewares.js +++ b/packages/roc-package-web-app/src/app/defaultKoaMiddlewares.js @@ -1,11 +1,12 @@ -import koaErrors from 'koa-errors'; -import helmet from 'koa-helmet'; +import koaError from 'koa-error'; +import koaHelmet from 'koa-helmet'; import koaEtag from 'koa-etag'; -import koaCompressor from 'koa-compressor'; +import koaCompress from 'koa-compress'; import koaFavicon from 'koa-favicon'; -import koaAccesslog from 'koa-accesslog'; import koaLogger from 'koa-logger'; +import accesslog from './middlewares/accesslog'; + /** * Returns the middlewares to be used. * @@ -16,17 +17,17 @@ export default function middlewares(config, { dev, dist }) { const middlewaresList = []; if (dev) { - middlewaresList.push(koaErrors()); + middlewaresList.push(koaError()); } // Security headers - middlewaresList.push(helmet()); + middlewaresList.push(koaHelmet()); middlewaresList.push(koaEtag()); // We only enable gzip in dist if (dist) { - middlewaresList.push(koaCompressor()); + middlewaresList.push(koaCompress()); } const favicon = config.favicon; @@ -35,7 +36,7 @@ export default function middlewares(config, { dev, dist }) { } if (dist) { - middlewaresList.push(koaAccesslog()); + middlewaresList.push(accesslog()); } else { middlewaresList.push(koaLogger()); } diff --git a/packages/roc-package-web-app/src/app/defaultKoaMiddlewares.spec.js b/packages/roc-package-web-app/src/app/defaultKoaMiddlewares.spec.js new file mode 100644 index 0000000..525180c --- /dev/null +++ b/packages/roc-package-web-app/src/app/defaultKoaMiddlewares.spec.js @@ -0,0 +1,64 @@ +import koaErrorMock from 'koa-error'; +import koaHelmetMock from 'koa-helmet'; +import koaEtagMock from 'koa-etag'; +import koaLoggerMock from 'koa-logger'; +import koaCompressMock from 'koa-compress'; +import koaFaviconMock from 'koa-favicon'; + +import accessLogMock from './middlewares/accesslog'; +import getKoaMiddlewares from './defaultKoaMiddlewares'; + +jest.mock('koa-error', () => jest.fn(() => function koaError() {})); +jest.mock('koa-helmet', () => jest.fn(() => function koaHelmet() {})); +jest.mock('koa-etag', () => jest.fn(() => function koaEtag() {})); +jest.mock('koa-logger', () => jest.fn(() => function koaLogger() {})); +jest.mock('koa-compress', () => jest.fn(() => function koaCompress() {})); +jest.mock('koa-favicon', () => jest.fn(() => function koaFavicon() {})); +jest.mock('./middlewares/accesslog', () => jest.fn(() => function accessLog() {})); + + +describe('defaultKoaMiddleware', () => { + afterEach(() => { + koaErrorMock.mockClear(); + koaHelmetMock.mockClear(); + koaEtagMock.mockClear(); + koaLoggerMock.mockClear(); + koaCompressMock.mockClear(); + }); + + it('should return default array of middlewares even when all options are falsy', () => { + const middlewares = getKoaMiddlewares({}, { dev: false, dist: false }); + + expect(middlewares.length).toEqual(3); + expect(middlewares.map(f => f.name)).toEqual(['koaHelmet', 'koaEtag', 'koaLogger']); + expect(koaHelmetMock).toHaveBeenCalled(); + expect(koaEtagMock).toHaveBeenCalled(); + expect(koaLoggerMock).toHaveBeenCalled(); + }); + + it('should include koa-error when dev is set to true', () => { + const middlewares = getKoaMiddlewares({}, { dev: true, dist: false }); + + expect(middlewares.length).toEqual(4); + expect(middlewares.map(f => f.name)).toEqual(['koaError', 'koaHelmet', 'koaEtag', 'koaLogger']); + expect(koaErrorMock).toHaveBeenCalled(); + }); + + it('should include koa-compress and accessLog instead of koa-logger when dist is set to true', () => { + const middlewares = getKoaMiddlewares({}, { dev: false, dist: true }); + + expect(middlewares.length).toEqual(4); + expect(middlewares.map(f => f.name)).toEqual(['koaHelmet', 'koaEtag', 'koaCompress', 'accessLog']); + expect(koaCompressMock).toHaveBeenCalled(); + expect(accessLogMock).toHaveBeenCalled(); + }); + + it('should include koa-favicon when favicon is specified in config', () => { + const config = { favicon: './pathToFavicon.ico' }; + const middlewares = getKoaMiddlewares(config, { dev: false, dist: false }); + + expect(middlewares.length).toEqual(4); + expect(middlewares.map(f => f.name)).toEqual(['koaHelmet', 'koaEtag', 'koaFavicon', 'koaLogger']); + expect(koaFaviconMock).toHaveBeenCalledWith(config.favicon); + }); +}); diff --git a/packages/roc-package-web-app/src/app/middlewares/accesslog.js b/packages/roc-package-web-app/src/app/middlewares/accesslog.js new file mode 100644 index 0000000..2b729b5 --- /dev/null +++ b/packages/roc-package-web-app/src/app/middlewares/accesslog.js @@ -0,0 +1,18 @@ +import util from 'util'; + +import moment from 'moment'; + +/* + Implementation based on koa-accesslog + */ +export default function accesslog(stream = process.stdout) { + return async function(ctx, next) { + await next(); + + const format = '%s - - [%s] "%s %s HTTP/1.X" %d %s\n'; + const length = ctx.length ? ctx.length.toString() : '-'; + const date = moment().format('D/MMM/YYYY:HH:mm:ss ZZ'); + + stream.write(util.format(format, ctx.ip, date, ctx.method, ctx.path, ctx.status, length)); + }; +} diff --git a/packages/roc-package-web-app/src/app/middlewares/accesslog.spec.js b/packages/roc-package-web-app/src/app/middlewares/accesslog.spec.js new file mode 100644 index 0000000..4f52f79 --- /dev/null +++ b/packages/roc-package-web-app/src/app/middlewares/accesslog.spec.js @@ -0,0 +1,52 @@ +import { format as formatMock } from 'util'; + +import createAccessLogMiddleware from './accesslog'; + +jest.mock('util', () => ({ + format: jest.fn(), +})); + +describe('middlewares/accesslog', () => { + const next = async () => {}; + + afterEach(() => { + formatMock.mockClear(); + }); + + it('should call write method of passed stream', async () => { + const streamWriteMock = jest.fn(); + const streamMock = { + write: streamWriteMock, + }; + const accessLogMiddleware = createAccessLogMiddleware(streamMock); + await accessLogMiddleware({}, next); + + expect(streamWriteMock).toHaveBeenCalled(); + }); + + it('should call util.format with certain ctx properties', async () => { + const streamWriteMock = jest.fn(); + const streamMock = { + write: streamWriteMock, + }; + const accessLogMiddleware = createAccessLogMiddleware(streamMock); + const ctx = { + length: 42, + ip: '127.0.0.1', + method: 'GET', + path: '/app', + status: 200, + }; + await accessLogMiddleware(ctx, next); + + expect(formatMock).toHaveBeenCalledWith( + '%s - - [%s] \"%s %s HTTP/1.X\" %d %s\n', + ctx.ip, + expect.anything(), + ctx.method, + ctx.path, + ctx.status, + ctx.length.toString(), + ); + }); +}); diff --git a/packages/roc-package-web-app/src/app/basepathSupport.js b/packages/roc-package-web-app/src/app/middlewares/basepathSupport.js similarity index 82% rename from packages/roc-package-web-app/src/app/basepathSupport.js rename to packages/roc-package-web-app/src/app/middlewares/basepathSupport.js index 1d6c36f..03df57e 100644 --- a/packages/roc-package-web-app/src/app/basepathSupport.js +++ b/packages/roc-package-web-app/src/app/middlewares/basepathSupport.js @@ -26,16 +26,16 @@ export default function basepathSupport(basepath) { return newPath; } - return function* (next) { + return async function(ctx, next) { // Do nothing if the basepath is / if (basepath === '/') { - return yield next; + return await next(); } - const newPath = matcher(this.path); + const newPath = matcher(ctx.path); if (newPath) { - this.path = newPath; - return yield next; + ctx.path = newPath; + return await next(); } // If the path does not match Koa will render a default 404 Not Found page. diff --git a/packages/roc-package-web-app/src/app/middlewares/basepathSupport.spec.js b/packages/roc-package-web-app/src/app/middlewares/basepathSupport.spec.js new file mode 100644 index 0000000..07288e9 --- /dev/null +++ b/packages/roc-package-web-app/src/app/middlewares/basepathSupport.spec.js @@ -0,0 +1,39 @@ +import createBasepathSupportMiddleware from './basepathSupport'; + +describe('middlewares/basepathSupport', () => { + const next = async () => {}; + + it('should throw an error when basepath does not start with "/"', () => { + const veryWrongBasepath = 'veryWrongBasepath'; + expect(() => { + createBasepathSupportMiddleware(veryWrongBasepath); + }).toThrowError(`The basepath must start with "/", was ${veryWrongBasepath}`); + }); + + it('should set new ctx.path with eased basepath', async () => { + const basepath = '/basepath'; + const ctx = { path: '/basepath/application' }; + const basepathMiddleware = createBasepathSupportMiddleware(basepath); + await basepathMiddleware(ctx, next); + + expect(ctx.path).toEqual('/application'); + }); + + it('should set new ctx.path to "/" if its the same as basepath', async () => { + const basepath = '/basepath'; + const ctx = { path: '/basepath' }; + const basepathMiddleware = createBasepathSupportMiddleware(basepath); + await basepathMiddleware(ctx, next); + + expect(ctx.path).toEqual('/'); + }); + + it('should return an undefined when basepath is different than "/" and no new path is set', async () => { + const basepath = '/'; + const ctx = { path: '/' }; + const basepathMiddleware = createBasepathSupportMiddleware(basepath); + const result = await basepathMiddleware(ctx, next); + + expect(result).toEqual(undefined); + }); +}); From a187e9da52d4992a869963d1b00498e0547b4a64 Mon Sep 17 00:00:00 2001 From: Patryk Mrukot Date: Wed, 31 Jan 2018 22:19:47 +0100 Subject: [PATCH 3/4] test(roc-package-web-app): Add tests to createServer --- .../src/app/createServer.spec.js | 201 ++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 packages/roc-package-web-app/src/app/createServer.spec.js diff --git a/packages/roc-package-web-app/src/app/createServer.spec.js b/packages/roc-package-web-app/src/app/createServer.spec.js new file mode 100644 index 0000000..2efb08a --- /dev/null +++ b/packages/roc-package-web-app/src/app/createServer.spec.js @@ -0,0 +1,201 @@ +import Koa from 'koa'; +import serveMiddlewareMock from 'koa-static'; +import removeTrailingSlashMiddlewareMock from 'koa-remove-trailing-slashes'; +import normalizePathMiddlewareMock from 'koa-normalize-path'; +import lowercasePathMiddlewareMock from 'koa-lowercase-path'; +import addTrailingSlashMiddlewareMock from 'koa-add-trailing-slashes'; +import { readFileSync as readFileSyncMock } from 'fs'; +import { createServer as createHttpServerMock } from 'http'; +import { createServer as createHttpsServerMock } from 'https'; + +import basepathMiddlewareMock from './middlewares/basepathSupport'; +import createServer from './createServer'; + +jest.mock('roc', () => ({ + getSettings: () => {}, + merge: (_, a) => a, + getAbsolutePath: a => a, +})); +jest.mock('./middlewares/basepathSupport', () => jest.fn(() => function basepath() {})); +jest.mock('koa-remove-trailing-slashes', () => jest.fn(() => function removeTrailingSlash() {})); +jest.mock('koa-static', () => jest.fn(() => function serve() {})); +jest.mock('koa-normalize-path', () => jest.fn(() => function normalizePath() {})); +jest.mock('koa-lowercase-path', () => jest.fn(() => function lowercasePath() {})); +jest.mock('koa-add-trailing-slashes', () => jest.fn(() => function addTrailingSlash() {})); +jest.mock('fs'); +jest.mock('http', () => ({ + createServer: jest.fn(() => ({ + listen: jest.fn(() => 'http'), + })), +})); +jest.mock('https', () => ({ + createServer: jest.fn(() => ({ + listen: jest.fn(() => 'https'), + })), +})); + + +describe('createServer', () => { + let options; + let beforeUserMiddlewares; + let params; + + beforeEach(() => { + options = { + koa: { + staticServe: { + maxage: 42, + }, + normalize: { + enabled: false, + defer: false, + }, + lowercase: { + enabled: false, + defer: false, + }, + trailingSlashes: { + enabled: false, + defer: false, + }, + }, + serve: '/static', + port: 4000, + https: { + port: 4001, + key: '/pathtokey', + cert: '/pathtocert', + }, + }; + beforeUserMiddlewares = []; + params = { + useDefaultKoaMiddlewares: false, + hasKoaMiddlewares: false, + koaMiddlewares: { + default: () => [], + }, + dev: false, + dist: false, + rocPath: '/rocPath', + }; + }); + + afterEach(() => { + basepathMiddlewareMock.mockClear(); + removeTrailingSlashMiddlewareMock.mockClear(); + serveMiddlewareMock.mockClear(); + }); + + it('should return object with Koa server and start function', () => { + const result = createServer(options, beforeUserMiddlewares, params); + + expect(result.server).toBeInstanceOf(Koa); + expect(result.start).toBeInstanceOf(Function); + }); + + it('should add predefined set of middlewares to the Koa instance', () => { + const result = createServer(options, beforeUserMiddlewares, params); + + expect(result.server.middleware.length).toEqual(3); + expect(result.server.middleware.map(f => f.name)) + .toEqual(['basepath', 'removeTrailingSlash', 'serve']); + expect(basepathMiddlewareMock).toHaveBeenCalledWith(params.rocPath); + expect(removeTrailingSlashMiddlewareMock).toHaveBeenCalled(); + expect(serveMiddlewareMock).toHaveBeenCalledWith(options.serve, expect.anything()); + }); + + it('should add defaultKoaMiddlewares to Koa instance if proper param is set to true', () => { + params.useDefaultKoaMiddlewares = true; + + const result = createServer(options, beforeUserMiddlewares, params); + + expect(result.server.middleware.length).toEqual(6); + expect(result.server.middleware.map(f => f.name)) + .toEqual(['basepath', 'middleware', 'etag', 'logger', 'removeTrailingSlash', 'serve']); + }); + + it('should include beforeUserMiddleware to Koa instance', () => { + beforeUserMiddlewares = [ + function middlewareOne() {}, + function middlewareTwo() {}, + ]; + + const result = createServer(options, beforeUserMiddlewares, params); + + expect(result.server.middleware.length).toEqual(5); + expect(result.server.middleware.map(f => f.name)) + .toEqual(['basepath', 'middlewareOne', 'middlewareTwo', 'removeTrailingSlash', 'serve']); + }); + + it('should include koaMiddlewares to Koa instance', () => { + const koaMiddlewaresMock = jest.fn(() => [ + function koaMiddlewareOne() {}, + function koaMiddlewareTwo() {}, + ]); + params.hasKoaMiddlewares = true; + params.koaMiddlewares.default = koaMiddlewaresMock; + + const result = createServer(options, beforeUserMiddlewares, params); + + expect(result.server.middleware.length).toEqual(5); + expect(result.server.middleware.map(f => f.name)) + .toEqual(['basepath', 'koaMiddlewareOne', 'koaMiddlewareTwo', 'removeTrailingSlash', 'serve']); + expect(koaMiddlewaresMock).toHaveBeenCalledWith(options, { server: expect.anything() }); + }); + + it('should call serve for each path defined in options', () => { + const pathsToServe = ['/assets', '/static', '/kappa']; + options.serve = pathsToServe; + + const result = createServer(options, beforeUserMiddlewares, params); + + expect(result.server.middleware.length).toEqual(5); + expect(result.server.middleware.map(f => f.name)) + .toEqual(['basepath', 'removeTrailingSlash', 'serve', 'serve', 'serve']); + expect(serveMiddlewareMock).toHaveBeenCalledTimes(3); + expect(serveMiddlewareMock).toHaveBeenCalledWith(pathsToServe[0], expect.anything()); + expect(serveMiddlewareMock).toHaveBeenCalledWith(pathsToServe[1], expect.anything()); + expect(serveMiddlewareMock).toHaveBeenCalledWith(pathsToServe[2], expect.anything()); + }); + + it('should include normalizePath to Koa instance when proper option is set to true', () => { + options.koa.normalize.enabled = true; + + const result = createServer(options, beforeUserMiddlewares, params); + + expect(result.server.middleware.length).toBe(4); + expect(result.server.middleware.map(f => f.name)).toContain('normalizePath'); + expect(normalizePathMiddlewareMock).toHaveBeenCalledWith({ defer: options.koa.normalize.defer }); + }); + + it('should include normalizePath to Koa instance when proper option is set to true', () => { + options.koa.lowercase.enabled = true; + + const result = createServer(options, beforeUserMiddlewares, params); + + expect(result.server.middleware.length).toBe(4); + expect(result.server.middleware.map(f => f.name)).toContain('lowercasePath'); + expect(lowercasePathMiddlewareMock).toHaveBeenCalledWith({ defer: options.koa.lowercase.defer }); + }); + + it('should include koa-trailing-slashes instead of koa-remove-trailing-slash when proper option is set', () => { + options.koa.trailingSlashes.enabled = true; + + const result = createServer(options, beforeUserMiddlewares, params); + + expect(result.server.middleware.length).toBe(3); + expect(result.server.middleware.map(f => f.name)).toContain('addTrailingSlash'); + expect(addTrailingSlashMiddlewareMock).toHaveBeenCalledWith({ defer: options.koa.trailingSlashes.defer }); + }); + + it('should return servers after calling start function', () => { + const result = createServer(options, beforeUserMiddlewares, params).start(); + + expect(result).toEqual({ http: expect.anything(), https: expect.anything() }); + expect(readFileSyncMock).toHaveBeenCalledTimes(2); + expect(readFileSyncMock).toHaveBeenCalledWith(options.https.key); + expect(readFileSyncMock).toHaveBeenCalledWith(options.https.cert); + expect(createHttpServerMock).toHaveBeenCalled(); + expect(createHttpsServerMock).toHaveBeenCalled(); + }); +}); From 649985c9e265426b9e24651d06a66b987da439ff Mon Sep 17 00:00:00 2001 From: Patryk Mrukot Date: Thu, 1 Feb 2018 11:58:17 +0100 Subject: [PATCH 4/4] test(roc-package-web-app): Fix linting --- .../src/app/createServer.spec.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/roc-package-web-app/src/app/createServer.spec.js b/packages/roc-package-web-app/src/app/createServer.spec.js index 2efb08a..bd9ff6e 100644 --- a/packages/roc-package-web-app/src/app/createServer.spec.js +++ b/packages/roc-package-web-app/src/app/createServer.spec.js @@ -1,12 +1,13 @@ +import { readFileSync as readFileSyncMock } from 'fs'; +import { createServer as createHttpServerMock } from 'http'; +import { createServer as createHttpsServerMock } from 'https'; + import Koa from 'koa'; import serveMiddlewareMock from 'koa-static'; import removeTrailingSlashMiddlewareMock from 'koa-remove-trailing-slashes'; import normalizePathMiddlewareMock from 'koa-normalize-path'; import lowercasePathMiddlewareMock from 'koa-lowercase-path'; import addTrailingSlashMiddlewareMock from 'koa-add-trailing-slashes'; -import { readFileSync as readFileSyncMock } from 'fs'; -import { createServer as createHttpServerMock } from 'http'; -import { createServer as createHttpsServerMock } from 'https'; import basepathMiddlewareMock from './middlewares/basepathSupport'; import createServer from './createServer'; @@ -159,13 +160,13 @@ describe('createServer', () => { }); it('should include normalizePath to Koa instance when proper option is set to true', () => { - options.koa.normalize.enabled = true; + options.koa.normalize.enabled = true; - const result = createServer(options, beforeUserMiddlewares, params); + const result = createServer(options, beforeUserMiddlewares, params); expect(result.server.middleware.length).toBe(4); expect(result.server.middleware.map(f => f.name)).toContain('normalizePath'); - expect(normalizePathMiddlewareMock).toHaveBeenCalledWith({ defer: options.koa.normalize.defer }); + expect(normalizePathMiddlewareMock).toHaveBeenCalledWith({ defer: options.koa.normalize.defer }); }); it('should include normalizePath to Koa instance when proper option is set to true', () => {