Skip to content

Commit

Permalink
test: refactor to use mocha (#1453)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk authored May 27, 2022
1 parent 20e174a commit 675ee69
Show file tree
Hide file tree
Showing 80 changed files with 2,346 additions and 3,638 deletions.
4,420 changes: 1,560 additions & 2,860 deletions package-lock.json

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"prettier-check:updated": "pipe-git-updated --ext=css --ext=html --ext=js --ext=json --ext=md --ext=yaml --ext=yml -- prettier -c",
"prettify": "prettier --write --ignore-path .gitignore \"**/*.{css,html,js,json,md,yaml,yml}\"",
"prettify:updated": "pipe-git-updated --ext=css --ext=html --ext=js --ext=json --ext=md --ext=yaml --ext=yml -- prettier --write",
"test": "npm run build && jest --verbose --silent --runInBand",
"test": "npm run build && mocha 'tests/**/*.test.js' --exclude '**/node_modules/**/*' --exclude '**/old-unit/**/*' --require @babel/register --require ./tests/mochaGlobalSetup.js",
"test:jest": "npm run build && jest --verbose --silent --runInBand",
"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",
Expand Down Expand Up @@ -193,7 +194,7 @@
"@hapi/boom": "^10.0.0",
"@hapi/h2o2": "^9.1.0",
"@hapi/hapi": "^20.2.2",
"aws-sdk": "^2.1140.0",
"aws-sdk": "^2.1145.0",
"boxen": "^5.1.2",
"chalk": "^4.1.2",
"cuid": "^2.1.8",
Expand All @@ -214,12 +215,12 @@
"semver": "^7.3.7",
"update-notifier": "^5.1.0",
"velocityjs": "^2.0.6",
"ws": "^8.6.0"
"ws": "^8.7.0"
},
"devDependencies": {
"@babel/cli": "^7.17.10",
"@babel/core": "^7.18.0",
"@babel/preset-env": "^7.18.0",
"@babel/core": "^7.18.2",
"@babel/preset-env": "^7.18.2",
"@babel/register": "^7.17.7",
"archiver": "^5.3.1",
"copyfiles": "^2.4.1",
Expand All @@ -230,12 +231,12 @@
"eslint-plugin-prettier": "^4.0.0",
"git-list-updated": "^1.2.1",
"husky": "^8.0.1",
"jest": "^28.1.0",
"lint-staged": "^12.4.1",
"lint-staged": "^12.4.2",
"mocha": "^10.0.0",
"p-map": "^4.0.0",
"prettier": "^2.6.2",
"rimraf": "^3.0.2",
"serverless": "^3.18.1",
"serverless": "^3.18.2",
"standard-version": "^9.5.0"
},
"peerDependencies": {
Expand Down
6 changes: 4 additions & 2 deletions src/lambda/__tests__/LambdaContext.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import assert from 'node:assert'
import LambdaContext from '../LambdaContext.js'

describe('LambdaContext', () => {
test('should create LambdaContext with correct values', () => {
it('should create LambdaContext with correct values', () => {
const functionName = 'foo'
const memorySize = 512
const requestId = 'abc123'
Expand All @@ -23,6 +24,7 @@ describe('LambdaContext', () => {
memoryLimitInMB: '512',
}

expect(context).toEqual(expected)
// expect(context).toEqual(expected)
assert.deepEqual(context, expected)
})
})
17 changes: 9 additions & 8 deletions src/lambda/__tests__/LambdaFunction.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import assert from 'node:assert'
import { resolve } from 'node:path'
// import { performance } from 'node:perf_hooks'
import LambdaFunction from '../LambdaFunction.js'
Expand Down Expand Up @@ -106,7 +107,7 @@ describe('LambdaFunction', () => {
handler: 'fixtures/lambdaFunction.fixture.callbackInsidePromiseHandler',
},
].forEach(({ description, expected, handler }) => {
test(description, async () => {
it(description, async () => {
const functionDefinition = {
handler,
}
Expand All @@ -119,12 +120,12 @@ describe('LambdaFunction', () => {
)
const result = await lambdaFunction.runHandler()

expect(result).toEqual(expected)
assert.equal(result, expected)
})
})
})

test('should pass remaining time to LambdaContext', async () => {
it('should pass remaining time to LambdaContext', async () => {
const functionDefinition = {
handler: 'fixtures/lambdaFunction.fixture.remainingExecutionTimeHandler',
}
Expand All @@ -138,11 +139,11 @@ describe('LambdaFunction', () => {
const [first, second, third] = await lambdaFunction.runHandler()

// handler "pauses" for 100 ms
expect(first).toBeGreaterThan(second - 100)
expect(second).toBeGreaterThan(third - 200)
assert.ok(first > second - 100)
assert.ok(second > third - 200)
})

test('should use default lambda timeout when timeout is not provided', async () => {
it('should use default lambda timeout when timeout is not provided', async () => {
const functionDefinition = {
handler: 'fixtures/lambdaFunction.fixture.defaultTimeoutHandler',
}
Expand All @@ -155,11 +156,11 @@ describe('LambdaFunction', () => {
)
const remainingTime = await lambdaFunction.runHandler()

expect(remainingTime).toBeLessThan(DEFAULT_LAMBDA_TIMEOUT * 1000)
assert.ok(remainingTime < DEFAULT_LAMBDA_TIMEOUT * 1000)

// result might be flaky/unreliable:
// (assmuning handler runs no longer than 1 s)
expect(remainingTime + 1000).toBeGreaterThan(DEFAULT_LAMBDA_TIMEOUT * 1000)
assert.ok(remainingTime + 1000 > DEFAULT_LAMBDA_TIMEOUT * 1000)
})

// // might run flaky (unreliable)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import assert from 'node:assert'
import InvocationsController from '../../../routes/invocations/InvocationsController.js'
import LambdaFunctionThatReturnsJSONObject from '../../fixtures/Lambda/LambdaFunctionThatReturnsJSONObject.fixture.js'
import LambdaFunctionThatReturnsNativeString from '../../fixtures/Lambda/LambdaFunctionThatReturnsNativeString.fixture.js'

jest.mock('../../../../serverlessLog')

describe('InvocationController', () => {
const functionName = 'foo'

describe('when event type is "RequestResponse"', () => {
const eventType = 'RequestResponse'

test('should return json object if lambda response is json', async () => {
it('should return json object if lambda response is json', async () => {
const expected = {
Payload: {
foo: 'bar',
Expand All @@ -23,10 +22,10 @@ describe('InvocationController', () => {
)
const result = await invocationController.invoke(functionName, eventType)

expect(result).toStrictEqual(expected)
assert.deepEqual(result, expected)
})

test('should wrap native string responses with ""', async () => {
it('should wrap native string responses with ""', async () => {
const expected = {
Payload: '"foo"',
StatusCode: 200,
Expand All @@ -37,7 +36,7 @@ describe('InvocationController', () => {
)
const result = await invocationController.invoke(functionName, eventType)

expect(result).toStrictEqual(expected)
assert.deepEqual(result, expected)
})
})
})
35 changes: 16 additions & 19 deletions tests/endToEnd/environmentVariables/environmentVariables.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import assert from 'node:assert'
import { resolve } from 'node:path'
import { env } from 'node:process'
import fetch from 'node-fetch'
Expand All @@ -7,50 +8,46 @@ import {
teardown,
} from '../../integration/_testHelpers/index.js'

jest.setTimeout(30000)
describe('environment variables', function desc() {
this.timeout(30000)

describe('environment variables', () => {
const ENV_VAR_QUOTED = 'I am ENV_VAR_1'
const ENV_VAR_UNQUOTED = 'I am ENV_VAR_2'
const ENV_VAR_MAPPED = 'I am ENV_VAR_3'

// init
let json
beforeAll(async () => {

beforeEach(async () => {
env.ENV_VAR_QUOTED = ENV_VAR_QUOTED
env.ENV_VAR_UNQUOTED = ENV_VAR_UNQUOTED
env.ENV_VAR_MAPPED_FROM_ANOTHER = ENV_VAR_MAPPED

await setup({
servicePath: resolve(__dirname),
})
const url = joinUrl(TEST_BASE_URL, '/dev/hello')

const url = joinUrl(env.TEST_BASE_URL, '/dev/hello')
const response = await fetch(url)
json = await response.json()
})

// cleanup
afterAll(async () => {
afterEach(async () => {
env.ENV_VAR_QUOTED = undefined
env.ENV_VAR_UNQUOTED = undefined
env.ENV_VAR_MAPPED_FROM_ANOTHER = undefined

await teardown()
})

test('it should handle a quoted environment variable', () => {
expect(json).toMatchObject({
ENV_VAR_QUOTED,
})
it('it should handle a quoted environment variable', () => {
assert.equal(json.ENV_VAR_QUOTED, ENV_VAR_QUOTED)
})

test('it should handle an unquoted environment variable', () => {
expect(json).toMatchObject({
ENV_VAR_UNQUOTED,
})
it('it should handle an unquoted environment variable', () => {
assert.equal(json.ENV_VAR_UNQUOTED, ENV_VAR_UNQUOTED)
})

test('it should handle a mapped environment variable', () => {
expect(json).toMatchObject({
ENV_VAR_MAPPED,
})
it('it should handle a mapped environment variable', () => {
assert.equal(json.ENV_VAR_MAPPED, ENV_VAR_MAPPED)
})
})
14 changes: 3 additions & 11 deletions tests/endToEnd/environmentVariables/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,16 @@ const { env } = require('node:process')
const { stringify } = JSON

exports.hello = async () => {
const {
ENV_VAR_QUOTED,
ENV_VAR_UNQUOTED,
ENV_VAR_MAPPED,
ENV_VAR_EMPTY_STRING,
ENV_VAR_UNDEFINED,
} = env
const { ENV_VAR_MAPPED, ENV_VAR_QUOTED, ENV_VAR_UNQUOTED } = env

const body = stringify({
ENV_VAR_MAPPED,
ENV_VAR_QUOTED,
ENV_VAR_UNQUOTED,
ENV_VAR_MAPPED,
ENV_VAR_EMPTY_STRING, // This should be undefined
ENV_VAR_UNDEFINED, // This should be undefined
})

return {
statusCode: 200,
body,
statusCode: 200,
}
}
4 changes: 2 additions & 2 deletions tests/endToEnd/environmentVariables/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ plugins:

provider:
environment:
ENV_VAR_MAPPED: '${env:ENV_VAR_MAPPED_FROM_ANOTHER}'
ENV_VAR_QUOTED: '${env:ENV_VAR_QUOTED}'
ENV_VAR_UNQUOTED: ${env:ENV_VAR_UNQUOTED}
ENV_VAR_MAPPED: '${env:ENV_VAR_MAPPED_FROM_ANOTHER}'
memorySize: 128
name: aws
region: us-east-1 # default
runtime: nodejs12.x
runtime: nodejs16.x
stage: dev
versionFunctions: false

Expand Down
2 changes: 1 addition & 1 deletion tests/endToEnd/optionParameters/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ const { stringify } = JSON

exports.hello = async () => {
return {
statusCode: 200,
body: stringify({
foo: 'bar',
}),
statusCode: 200,
}
}
42 changes: 21 additions & 21 deletions tests/endToEnd/optionParameters/optionParameters.test.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
import assert from 'node:assert'
import { resolve } from 'node:path'
import { env } from 'node:process'
import fetch from 'node-fetch'
import {
joinUrl,
setup,
teardown,
} from '../../integration/_testHelpers/index.js'

jest.setTimeout(30000)
describe('noPrependStageInUrl option', function desc() {
this.timeout(30000)

describe('noPrependStageInUrl option', () => {
// init
beforeAll(() =>
beforeEach(() =>
setup({
servicePath: resolve(__dirname),
args: ['--noPrependStageInUrl'],
}),
)

// cleanup
afterAll(() => teardown())
afterEach(() => teardown())

describe('when --noPrependStageInUrl is used, and the stage isnt in the url', () => {
test('it should return a payload', async () => {
const url = joinUrl(TEST_BASE_URL, '/hello')
it('it should return a payload', async () => {
const url = joinUrl(env.TEST_BASE_URL, '/hello')
const response = await fetch(url)
const json = await response.json()

expect(json).toEqual({ foo: 'bar' })
assert.deepEqual(json, { foo: 'bar' })
})
})

describe('when --noPrependStageInUrl is used, and the stage isnt in the url', () => {
test('noPrependStageInUrl 2', async () => {
const url = joinUrl(TEST_BASE_URL, '/dev/hello')
it('noPrependStageInUrl 2', async () => {
const url = joinUrl(env.TEST_BASE_URL, '/dev/hello')
const response = await fetch(url)
const json = await response.json()

expect(json.statusCode).toEqual(404)
assert.equal(json.statusCode, 404)
})
})
})

describe('prefix option', () => {
// init
beforeAll(() =>
describe('prefix option', function desc() {
this.timeout(30000)

beforeEach(() =>
setup({
servicePath: resolve(__dirname),
args: ['--prefix', 'someprefix'],
servicePath: resolve(__dirname),
}),
)

// cleanup
afterAll(() => teardown())
afterEach(() => 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/hello')
it('the prefixed path should return a payload', async () => {
const url = joinUrl(env.TEST_BASE_URL, '/someprefix/dev/hello')
const response = await fetch(url)
const json = await response.json()

expect(json).toEqual({ foo: 'bar' })
assert.deepEqual(json, { foo: 'bar' })
})
})
})
4 changes: 2 additions & 2 deletions tests/endToEnd/optionParameters/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ provider:
memorySize: 128
name: aws
region: us-east-1 # default
runtime: nodejs12.x
runtime: nodejs16.x
stage: dev
versionFunctions: false

functions:
hello:
handler: handler.hello
events:
- http:
method: get
path: /hello
handler: handler.hello
Loading

0 comments on commit 675ee69

Please sign in to comment.