Skip to content

Commit

Permalink
refactor to use jest
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydaly committed Mar 24, 2021
1 parent 6511772 commit 0f4fc6f
Show file tree
Hide file tree
Showing 50 changed files with 15,182 additions and 7,419 deletions.
4 changes: 3 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
coverage
node_modules
test
__tests__
*.test.js
dist
68 changes: 33 additions & 35 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
{
"env": {
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 8,
"sourceType": "module"
},
"rules": {
"indent": [
"error",
2
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"never"
],
"indent": [
"env": {
"es6": true,
"node": true,
"jest": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"rules": {
"linebreak-style": [
"error",
2,
{ "SwitchCase": 1 }
]
},
"globals": {
"expect": true,
"it": true
}
"unix"
],
"quotes": [
"error",
"single",
{ "allowTemplateLiterals": true }
],
"semi": [
"error",
"never"
],
"indent": [
"error",
2,
{ "SwitchCase": 1, "flatTernaryExpressions": true }
]
},
"globals": {
"expect": true,
"it": true
}
}
7 changes: 2 additions & 5 deletions test/_testApp.js → __tests__/_testApp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const Promise = require('bluebird');
const delay = ms => new Promise(res => setTimeout(res, ms))

module.exports = {

Expand All @@ -11,10 +11,7 @@ module.exports = {

promise: function(req,res) {
let start = Date.now()
Promise.try(() => {
for(let i = 0; i<40000000; i++) {}
return true
}).then((x) => {
delay(100).then((x) => {
res.json({ method:'get', status:'ok', app:'app2'})
})
},
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 8 additions & 8 deletions test/attachments.js → __tests__/attachments.unit.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const expect = require('chai').expect // Assertion library


// Init API instance
const api = require('../index')({ version: 'v1.0', mimeTypes: { test: 'text/test' } })
Expand Down Expand Up @@ -59,43 +59,43 @@ describe('Attachment Tests:', function() {
it('Simple attachment', async function() {
let _event = Object.assign({},event,{ path: '/attachment' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ multiValueHeaders: { 'content-disposition': ['attachment'], 'content-type': ['application/json'] }, statusCode: 200, body: '{"status":"ok"}', isBase64Encoded: false })
expect(result).toEqual({ multiValueHeaders: { 'content-disposition': ['attachment'], 'content-type': ['application/json'] }, statusCode: 200, body: '{"status":"ok"}', isBase64Encoded: false })
}) // end it

it('PDF attachment w/ path', async function() {
let _event = Object.assign({},event,{ path: '/attachment/pdf' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ multiValueHeaders: { 'content-disposition': ['attachment; filename=\"foo.pdf\"'], 'content-type': ['application/pdf'] }, statusCode: 200, body: 'filedata', isBase64Encoded: false })
expect(result).toEqual({ multiValueHeaders: { 'content-disposition': ['attachment; filename=\"foo.pdf\"'], 'content-type': ['application/pdf'] }, statusCode: 200, body: 'filedata', isBase64Encoded: false })
}) // end it

it('PNG attachment w/ path', async function() {
let _event = Object.assign({},event,{ path: '/attachment/png' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ multiValueHeaders: { 'content-disposition': ['attachment; filename=\"foo.png\"'], 'content-type': ['image/png'] }, statusCode: 200, body: 'filedata', isBase64Encoded: false })
expect(result).toEqual({ multiValueHeaders: { 'content-disposition': ['attachment; filename=\"foo.png\"'], 'content-type': ['image/png'] }, statusCode: 200, body: 'filedata', isBase64Encoded: false })
}) // end it

it('CSV attachment w/ path', async function() {
let _event = Object.assign({},event,{ path: '/attachment/csv' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ multiValueHeaders: { 'content-disposition': ['attachment; filename=\"foo.csv\"'], 'content-type': ['text/csv'] }, statusCode: 200, body: 'filedata', isBase64Encoded: false })
expect(result).toEqual({ multiValueHeaders: { 'content-disposition': ['attachment; filename=\"foo.csv\"'], 'content-type': ['text/csv'] }, statusCode: 200, body: 'filedata', isBase64Encoded: false })
}) // end it

it('Custom MIME type attachment w/ path', async function() {
let _event = Object.assign({},event,{ path: '/attachment/custom' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ multiValueHeaders: { 'content-disposition': ['attachment; filename=\"foo.test\"'], 'content-type': ['text/test'] }, statusCode: 200, body: 'filedata', isBase64Encoded: false })
expect(result).toEqual({ multiValueHeaders: { 'content-disposition': ['attachment; filename=\"foo.test\"'], 'content-type': ['text/test'] }, statusCode: 200, body: 'filedata', isBase64Encoded: false })
}) // end it

it('Empty string', async function() {
let _event = Object.assign({},event,{ path: '/attachment/empty-string' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ multiValueHeaders: { 'content-disposition': ['attachment'], 'content-type': ['application/json'] }, statusCode: 200, body: 'filedata', isBase64Encoded: false })
expect(result).toEqual({ multiValueHeaders: { 'content-disposition': ['attachment'], 'content-type': ['application/json'] }, statusCode: 200, body: 'filedata', isBase64Encoded: false })
}) // end it

it('Null string', async function() {
let _event = Object.assign({},event,{ path: '/attachment/empty-string' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ multiValueHeaders: { 'content-disposition': ['attachment'], 'content-type': ['application/json'] }, statusCode: 200, body: 'filedata', isBase64Encoded: false })
expect(result).toEqual({ multiValueHeaders: { 'content-disposition': ['attachment'], 'content-type': ['application/json'] }, statusCode: 200, body: 'filedata', isBase64Encoded: false })
}) // end it

}) // end HEADER tests
8 changes: 4 additions & 4 deletions test/basePath.js → __tests__/basePath.unit.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const expect = require('chai').expect // Assertion library


// Init API instance
const api = require('../index')({ version: 'v1.0', base: '/v1' })
Expand Down Expand Up @@ -43,19 +43,19 @@ describe('Base Path Tests:', function() {
it('Simple path with base: /v1/test', async function() {
let _event = Object.assign({},event,{ path: '/v1/test' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 200, body: '{"method":"get","status":"ok"}', isBase64Encoded: false })
expect(result).toEqual({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 200, body: '{"method":"get","status":"ok"}', isBase64Encoded: false })
}) // end it

it('Path with base and parameter: /v1/test/123', async function() {
let _event = Object.assign({},event,{ path: '/v1/test/123' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 200, body: '{"method":"get","status":"ok","param":"123"}', isBase64Encoded: false })
expect(result).toEqual({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 200, body: '{"method":"get","status":"ok","param":"123"}', isBase64Encoded: false })
}) // end it

it('Nested path with base: /v1/test/test2/test3', async function() {
let _event = Object.assign({},event,{ path: '/v1/test/test2/test3' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 200, body: '{"path":"/v1/test/test2/test3","method":"get","status":"ok"}', isBase64Encoded: false })
expect(result).toEqual({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 200, body: '{"path":"/v1/test/test2/test3","method":"get","status":"ok"}', isBase64Encoded: false })
}) // end it

}) // end BASEPATH tests
24 changes: 10 additions & 14 deletions test/cacheControl.js → __tests__/cacheControl.unit.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
'use strict';

const Promise = require('bluebird') // Promise library
const expect = require('chai').expect // Assertion library

// Init API instance
const api = require('../index')({ version: 'v1.0' })


// NOTE: Set test to true
api._test = true;

Expand Down Expand Up @@ -74,7 +70,7 @@ describe('cacheControl Tests:', function() {
it('Basic cacheControl (no options)', async function() {
let _event = Object.assign({},event,{ path: '/cache' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({
expect(result).toEqual({
multiValueHeaders: {
'content-type': ['application/json'],
'cache-control': ['max-age=0'],
Expand All @@ -89,7 +85,7 @@ describe('cacheControl Tests:', function() {
it('Basic cacheControl (true)', async function() {
let _event = Object.assign({},event,{ path: '/cacheTrue' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({
expect(result).toEqual({
multiValueHeaders: {
'content-type': ['application/json'],
'cache-control': ['max-age=0'],
Expand All @@ -104,7 +100,7 @@ describe('cacheControl Tests:', function() {
it('Basic cacheControl (false)', async function() {
let _event = Object.assign({},event,{ path: '/cacheFalse' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({
expect(result).toEqual({
multiValueHeaders: {
'content-type': ['application/json'],
'cache-control': ['no-cache, no-store, must-revalidate']
Expand All @@ -118,7 +114,7 @@ describe('cacheControl Tests:', function() {
it('Basic cacheControl (maxAge)', async function() {
let _event = Object.assign({},event,{ path: '/cacheMaxAge' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({
expect(result).toEqual({
multiValueHeaders: {
'content-type': ['application/json'],
'cache-control': ['max-age=1'],
Expand All @@ -133,7 +129,7 @@ describe('cacheControl Tests:', function() {
it('Basic cacheControl (private)', async function() {
let _event = Object.assign({},event,{ path: '/cachePrivate' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({
expect(result).toEqual({
multiValueHeaders: {
'content-type': ['application/json'],
'cache-control': ['private, max-age=1'],
Expand All @@ -148,7 +144,7 @@ describe('cacheControl Tests:', function() {
it('Basic cacheControl (disable private)', async function() {
let _event = Object.assign({},event,{ path: '/cachePrivateFalse' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({
expect(result).toEqual({
multiValueHeaders: {
'content-type': ['application/json'],
'cache-control': ['max-age=1'],
Expand All @@ -163,7 +159,7 @@ describe('cacheControl Tests:', function() {
it('Basic cacheControl (invalid private value)', async function() {
let _event = Object.assign({},event,{ path: '/cachePrivateInvalid' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({
expect(result).toEqual({
multiValueHeaders: {
'content-type': ['application/json'],
'cache-control': ['max-age=1'],
Expand All @@ -178,7 +174,7 @@ describe('cacheControl Tests:', function() {
it('Basic cacheControl (undefined)', async function() {
let _event = Object.assign({},event,{ path: '/cacheCustomUndefined' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({
expect(result).toEqual({
multiValueHeaders: {
'content-type': ['application/json'],
'cache-control': ['max-age=0'],
Expand All @@ -193,7 +189,7 @@ describe('cacheControl Tests:', function() {
it('Basic cacheControl (null)', async function() {
let _event = Object.assign({},event,{ path: '/cacheCustomNull' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({
expect(result).toEqual({
multiValueHeaders: {
'content-type': ['application/json'],
'cache-control': ['max-age=0'],
Expand All @@ -208,7 +204,7 @@ describe('cacheControl Tests:', function() {
it('Custom cacheControl (string)', async function() {
let _event = Object.assign({},event,{ path: '/cacheCustom' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({
expect(result).toEqual({
multiValueHeaders: {
'content-type': ['application/json'],
'cache-control': ['custom value']
Expand Down
4 changes: 1 addition & 3 deletions test/context.js → __tests__/context.unit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const expect = require('chai').expect // Assertion library

// Init API instance
const api = require('../index')({ version: 'v1.0' })

Expand Down Expand Up @@ -44,7 +42,7 @@ describe('Context Tests:', function() {
clientContext: {},
identity: { cognitoIdentityId: 321 }
},(e,res) => { r(res) }))
expect(result).to.deep.equal({
expect(result).toEqual({
multiValueHeaders: { 'content-type': ['application/json'] },
statusCode: 200,
body: '{"id":"1234","context":{"functionName":"testFunction","awsRequestId":"1234","log_group_name":"testLogGroup","log_stream_name":"testLogStream","clientContext":{},"identity":{"cognitoIdentityId":321}}}',
Expand Down
Loading

0 comments on commit 0f4fc6f

Please sign in to comment.