From 79c51243a7e24367c7393a9a18de2b935f69860c Mon Sep 17 00:00:00 2001 From: Richard Tan Date: Sun, 31 May 2020 11:02:14 +1000 Subject: [PATCH 1/2] chore(tests): generateHapiPath unit tests --- jest.config.units.js | 7 +++ package-lock.json | 2 +- package.json | 1 + src/utils/__tests__/generateHapiPath.test.js | 45 ++++++++++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 jest.config.units.js create mode 100644 src/utils/__tests__/generateHapiPath.test.js diff --git a/jest.config.units.js b/jest.config.units.js new file mode 100644 index 000000000..96d06e1a2 --- /dev/null +++ b/jest.config.units.js @@ -0,0 +1,7 @@ +'use strict' + +module.exports = { + // Ignore 'tests' directory as it contains all the integration tests + modulePathIgnorePatterns: ['src/lambda/__tests__/fixtures/', 'tests/'], + setupFiles: ['object.fromentries/auto.js'], +} diff --git a/package-lock.json b/package-lock.json index 2376dac53..eb95ffaf9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "serverless-offline", - "version": "6.2.0", + "version": "6.3.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 347f82e1e..4856fc733 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "prepare": "npm run build", "prepublishOnly": "npm run lint && npm run build", "test": "npm run build && jest --verbose --silent --runInBand", + "test:unit": "jest --verbose --silent --runInBand --config jest.config.units.js", "test:cov": "npm run build && jest --coverage --silent --runInBand --collectCoverageFrom=src/**/*.js", "test:log": "npm run build && jest --verbose", "test:noBuild": "jest --verbose --runInBand --bail" diff --git a/src/utils/__tests__/generateHapiPath.test.js b/src/utils/__tests__/generateHapiPath.test.js new file mode 100644 index 000000000..83bed18e0 --- /dev/null +++ b/src/utils/__tests__/generateHapiPath.test.js @@ -0,0 +1,45 @@ +import generateHapiPath from '../generateHapiPath.js' + +const serverless = { + service: { + provider: { + stage: 'dev', + }, + }, +} + +describe('generateHapiPath', () => { + test('should generate url starting with a slash', () => { + const options = {} + const result = generateHapiPath('users', options, serverless) + expect(result[0]).toEqual('/') + }) + + test('should generate url with the stage prepended', () => { + const options = {} + const result = generateHapiPath('users', options, serverless) + expect(result).toEqual('/dev/users') + }) + + describe('when a prefix option is set', () => { + test('the url should add the prefix', () => { + const options = { prefix: 'some-prefix' } + const result = generateHapiPath('users', options, serverless) + expect(result).toEqual('/some-prefix/dev/users') + }) + }) + + describe('when the noPrependStageInUrl option is set', () => { + test('the url should omit the stage', () => { + const options = { noPrependStageInUrl: true } + const result = generateHapiPath('users', options, serverless) + expect(result).toEqual('/users') + }) + }) + + test('the stage from options should override stage from serverless config', () => { + const options = { stage: 'prod' } + const result = generateHapiPath('users', options, serverless) + expect(result).toEqual('/prod/users') + }) +}) From 6dbd28fc405d066da6a36027313708bb33b948c5 Mon Sep 17 00:00:00 2001 From: Richard Tan Date: Sun, 31 May 2020 11:04:41 +1000 Subject: [PATCH 2/2] chore(tests): Add integration test for prefix option --- .../uncategorized/uncategorized.test.js | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/integration/uncategorized/uncategorized.test.js b/tests/integration/uncategorized/uncategorized.test.js index 85ea05541..fa2234236 100644 --- a/tests/integration/uncategorized/uncategorized.test.js +++ b/tests/integration/uncategorized/uncategorized.test.js @@ -64,3 +64,26 @@ describe('noPrependStageInUrl tests', () => { expect(json.statusCode).toEqual(404) }) }) + +describe('prefix options', () => { + // init + beforeAll(() => + setup({ + servicePath: resolve(__dirname), + args: ['--prefix', 'someprefix'], + }), + ) + + // cleanup + afterAll(() => teardown()) + + describe('when the prefix option is used', () => { + test('the prefixed path should return a payload', async () => { + const url = joinUrl(TEST_BASE_URL, '/someprefix/dev/uncategorized-1') + const response = await fetch(url) + const json = await response.json() + + expect(json).toEqual({ foo: 'bar' }) + }) + }) +})