lambda-test
Advanced tools
Comparing version
@@ -18,5 +18,4 @@ | ||
files: [ | ||
'lib/handlerTester.js', | ||
'lib/apiBlueprint.js', | ||
'lib/apiBlueprintLoader.js' | ||
'lib/HandlerTester.js', | ||
'lib/LambdaTest.js' | ||
] | ||
@@ -23,0 +22,0 @@ }); |
28
index.js
'use strict'; | ||
const HandlerTester = require('./lib/handler-tester'); | ||
const LambdaTest = require('./lib/LambdaTest'); | ||
const HandlerTester = require('./lib/HandlerTester'); | ||
module.exports = { | ||
handlerTester: (handler) => { | ||
return new HandlerTester(handler); | ||
} | ||
}; | ||
/** | ||
* Create test and checks for status code | ||
* | ||
* when first parameter is API path, response is checked against api blueprint | ||
* | ||
* @param {Function} handler - function to test | ||
* @param {number} [statusCode] - expected status code | ||
* @param {string} [httpMethod] - http method to use | ||
* @returns {HandlerTester} | ||
*/ | ||
function test (handler, statusCode, httpMethod) { | ||
const t = new LambdaTest(); | ||
return t.test(handler, statusCode, httpMethod); | ||
} | ||
module.exports = test; | ||
module.exports.LambdaTest = LambdaTest; | ||
module.exports.HandlerTester = HandlerTester; |
{ | ||
"name": "lambda-test", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "", | ||
@@ -13,3 +13,3 @@ "main": "index.js", | ||
"test:lint": "eslint ./lib/**/*.js ./test/**/*.js", | ||
"doc:generate": "node ./bin/makeApiDoc.js" | ||
"doc": "node ./bin/makeApiDoc.js" | ||
}, | ||
@@ -30,18 +30,19 @@ "author": "Marek Takac <takacmarek1221@gmail.com> (https://stackoverflow.com/users/story/872755)", | ||
"chai": "^4.1.2", | ||
"eslint": "^4.13.1", | ||
"eslint": "^4.19.1", | ||
"eslint-config-airbnb": "^16.1.0", | ||
"eslint-mocha": "^0.3.1", | ||
"eslint-plugin-import": "^2.8.0", | ||
"eslint-plugin-jsdoc": "^3.3.0", | ||
"eslint-mocha": "^0.4.0", | ||
"eslint-plugin-import": "^2.11.0", | ||
"eslint-plugin-jsdoc": "^3.6.2", | ||
"eslint-plugin-jsx-a11y": "^6.0.3", | ||
"eslint-plugin-mocha": "^4.11.0", | ||
"eslint-plugin-react": "^7.5.1", | ||
"eslint-plugin-mocha": "^5.0.0", | ||
"eslint-plugin-react": "^7.7.0", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^4.0.1", | ||
"jsdoc-to-markdown": "^4.0.1", | ||
"mocha": "^5.0.5", | ||
"mocha-istanbul": "^0.3.0" | ||
}, | ||
"dependencies": { | ||
"jsonschema": "^1.2.2", | ||
"protagonist": "^1.5.2" | ||
"jsonschema": "^1.2.4", | ||
"protagonist": "1.5.2" | ||
} | ||
} |
232
README.md
@@ -1,43 +0,213 @@ | ||
# *NPM module template* | ||
# Lambda test | ||
Small template for building NPM modules. | ||
Simple Serverless tester, which works like this: | ||
**This App follows [Sustainable App Manifest](https://github.com/pragonauts/developer-friendly-app-manifest)** | ||
```javascript | ||
----------------------- | ||
const lambdaTest = require('lambda-test'); | ||
const { getById } = require('../../routes/users.js'); | ||
1. [NPM Tasks](#npm-tasks) | ||
1. [How To Use](#how-to-use) | ||
1. [Developing in right Node.js environment](#developing-in-right-nodejs-environment) | ||
1. [Conributing](#contributing) | ||
describe('GET /users/{id}', () => { | ||
## NPM Tasks | ||
it('should get user by id', async () => { | ||
const response = await lambdaTest(getById) | ||
.pathParameters({ id: 123 }) | ||
.run(); | ||
}) | ||
}); | ||
- `$ npm test` - runs automatic tests (linter, coverage, unit & API tests) | ||
- `$ npm run test:backend` - run backend unit & API test | ||
- `$ npm run test:backend:watch` - run and watch backend unit & API test | ||
- `$ npm run test:coverage` - run coverage test | ||
- `$ npm run test:coverage:threshold` - run coverage threshold test | ||
- `$ npm run test:lint` - run eslint test | ||
``` | ||
## How To Use | ||
or much more sophisticated with Api Blueprint check | ||
1. Fork the project | ||
1. Clone your fork | ||
1. Rename the root directory and name atrribute in `package.json` | ||
1. Run `npm install` command | ||
1. And you're ready to start developing your own NPM module | ||
```javascript | ||
## Developing in right Node.js environment | ||
const assert = require('assert'); | ||
const { LambdaTest } = require('lambda-test'); | ||
const { updateById } = require('../../routes/users.js'); | ||
- you can use [NVM](https://github.com/creationix/nvm) to manage multiple Node versions | ||
- just specify wanted Node version in `.nvmrc` file | ||
// in project root | ||
const tester = new LambdaTest('./apiBlueprint.apib'); | ||
## Contributing | ||
describe('UPDATE /users/{id}', () => { | ||
- Follow [AirBnB style guide](https://github.com/airbnb/javascript) | ||
- Mind the [Gitflow workflow](http://nvie.com/posts/a-successful-git-branching-model/) | ||
- Check ESLint settings in `.eslintrc` and ensure your ESLint is enabled | ||
- (*do the magick*) | ||
- run `npm test` to ensure everything is fine | ||
- create feature branch at your fork and make pull request to upstream (ask App Owner) | ||
it('should get user by id', async () => { | ||
const response = await tester.test(updateById, '/users/{id}', 'UPDATE', 200) | ||
.pathParameters({ id: 123 }) | ||
.queryStringParameters({ fields: 'name' }) | ||
.headers({ Authorization: 'secret' }) | ||
.body({ name: 'John Doe' }) | ||
.verify(); | ||
assert.equal(response.body.name, 'John Doe'); | ||
}); | ||
}); | ||
``` | ||
# API | ||
<a name="HandlerTester"></a> | ||
## HandlerTester | ||
**Kind**: global class | ||
* [HandlerTester](#HandlerTester) | ||
* [new HandlerTester(handler, [statusCode], [httpMethod], [route], [api])](#new_HandlerTester_new) | ||
* [.queryStringParameters(query)](#HandlerTester+queryStringParameters) ⇒ <code>this</code> | ||
* [.body(body)](#HandlerTester+body) ⇒ <code>this</code> | ||
* [.headers(headers)](#HandlerTester+headers) ⇒ <code>this</code> | ||
* [.pathParameters(params)](#HandlerTester+pathParameters) ⇒ <code>this</code> | ||
* [.run()](#HandlerTester+run) ⇒ <code>Promise.<Object></code> | ||
* [.verify()](#HandlerTester+verify) ⇒ <code>Promise.<Object></code> | ||
<a name="new_HandlerTester_new"></a> | ||
### new HandlerTester(handler, [statusCode], [httpMethod], [route], [api]) | ||
| Param | Type | Default | | ||
| --- | --- | --- | | ||
| handler | <code>function</code> | | | ||
| [statusCode] | <code>number</code> \| <code>null</code> | <code></code> | | ||
| [httpMethod] | <code>string</code> \| <code>null</code> | <code>null</code> | | ||
| [route] | <code>string</code> \| <code>null</code> | <code>null</code> | | ||
| [api] | <code>ApiBlueprint</code> \| <code>null</code> | <code></code> | | ||
<a name="HandlerTester+queryStringParameters"></a> | ||
### handlerTester.queryStringParameters(query) ⇒ <code>this</code> | ||
Sets query string | ||
**Kind**: instance method of [<code>HandlerTester</code>](#HandlerTester) | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| query | <code>Object</code> \| <code>null</code> | <code></code> | the query string | | ||
<a name="HandlerTester+body"></a> | ||
### handlerTester.body(body) ⇒ <code>this</code> | ||
Sets request body | ||
**Kind**: instance method of [<code>HandlerTester</code>](#HandlerTester) | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| body | <code>Object</code> \| <code>string</code> | <code></code> | request body | | ||
<a name="HandlerTester+headers"></a> | ||
### handlerTester.headers(headers) ⇒ <code>this</code> | ||
Set request headers | ||
**Kind**: instance method of [<code>HandlerTester</code>](#HandlerTester) | ||
| Param | Type | Default | | ||
| --- | --- | --- | | ||
| headers | <code>Object</code> \| <code>null</code> | <code></code> | | ||
<a name="HandlerTester+pathParameters"></a> | ||
### handlerTester.pathParameters(params) ⇒ <code>this</code> | ||
**Kind**: instance method of [<code>HandlerTester</code>](#HandlerTester) | ||
| Param | Type | Default | | ||
| --- | --- | --- | | ||
| params | <code>Object</code> \| <code>null</code> | <code></code> | | ||
<a name="HandlerTester+run"></a> | ||
### handlerTester.run() ⇒ <code>Promise.<Object></code> | ||
Send request | ||
**Kind**: instance method of [<code>HandlerTester</code>](#HandlerTester) | ||
<a name="HandlerTester+verify"></a> | ||
### handlerTester.verify() ⇒ <code>Promise.<Object></code> | ||
Send request | ||
**Kind**: instance method of [<code>HandlerTester</code>](#HandlerTester) | ||
----------------- | ||
# API | ||
<a name="HandlerTester"></a> | ||
## HandlerTester | ||
**Kind**: global class | ||
* [HandlerTester](#HandlerTester) | ||
* [new HandlerTester(handler, [statusCode], [httpMethod], [route], [api])](#new_HandlerTester_new) | ||
* [.queryStringParameters(query)](#HandlerTester+queryStringParameters) ⇒ <code>this</code> | ||
* [.body(body)](#HandlerTester+body) ⇒ <code>this</code> | ||
* [.headers(headers)](#HandlerTester+headers) ⇒ <code>this</code> | ||
* [.pathParameters(params)](#HandlerTester+pathParameters) ⇒ <code>this</code> | ||
* [.run()](#HandlerTester+run) ⇒ <code>Promise.<Object></code> | ||
* [.verify()](#HandlerTester+verify) ⇒ <code>Promise.<Object></code> | ||
<a name="new_HandlerTester_new"></a> | ||
### new HandlerTester(handler, [statusCode], [httpMethod], [route], [api]) | ||
| Param | Type | Default | | ||
| --- | --- | --- | | ||
| handler | <code>function</code> | | | ||
| [statusCode] | <code>number</code> \| <code>null</code> | <code></code> | | ||
| [httpMethod] | <code>string</code> \| <code>null</code> | <code>null</code> | | ||
| [route] | <code>string</code> \| <code>null</code> | <code>null</code> | | ||
| [api] | <code>ApiBlueprint</code> \| <code>null</code> | <code></code> | | ||
<a name="HandlerTester+queryStringParameters"></a> | ||
### handlerTester.queryStringParameters(query) ⇒ <code>this</code> | ||
Sets query string | ||
**Kind**: instance method of [<code>HandlerTester</code>](#HandlerTester) | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| query | <code>Object</code> \| <code>null</code> | <code></code> | the query string | | ||
<a name="HandlerTester+body"></a> | ||
### handlerTester.body(body) ⇒ <code>this</code> | ||
Sets request body | ||
**Kind**: instance method of [<code>HandlerTester</code>](#HandlerTester) | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| body | <code>Object</code> \| <code>string</code> | <code></code> | request body | | ||
<a name="HandlerTester+headers"></a> | ||
### handlerTester.headers(headers) ⇒ <code>this</code> | ||
Set request headers | ||
**Kind**: instance method of [<code>HandlerTester</code>](#HandlerTester) | ||
| Param | Type | Default | | ||
| --- | --- | --- | | ||
| headers | <code>Object</code> \| <code>null</code> | <code></code> | | ||
<a name="HandlerTester+pathParameters"></a> | ||
### handlerTester.pathParameters(params) ⇒ <code>this</code> | ||
**Kind**: instance method of [<code>HandlerTester</code>](#HandlerTester) | ||
| Param | Type | Default | | ||
| --- | --- | --- | | ||
| params | <code>Object</code> \| <code>null</code> | <code></code> | | ||
<a name="HandlerTester+run"></a> | ||
### handlerTester.run() ⇒ <code>Promise.<Object></code> | ||
Send request | ||
**Kind**: instance method of [<code>HandlerTester</code>](#HandlerTester) | ||
<a name="HandlerTester+verify"></a> | ||
### handlerTester.verify() ⇒ <code>Promise.<Object></code> | ||
Send request | ||
**Kind**: instance method of [<code>HandlerTester</code>](#HandlerTester) |
@@ -5,3 +5,3 @@ 'use strict'; | ||
const ApiBlueprint = require('../../lib/apiBlueprint'); | ||
const ApiBlueprint = require('../../lib/ApiBlueprint'); | ||
@@ -8,0 +8,0 @@ describe('Api blueprint', () => { |
'use strict'; | ||
const { assert } = require('chai'); | ||
const path = require('path'); | ||
const ApiBlueprintLoader = require('../../lib/apiBlueprintLoader'); | ||
const loadApiBlueprint = require('../../lib/loadApiBlueprint'); | ||
@@ -11,3 +12,3 @@ describe('ApiBlueprintLoader', () => { | ||
beforeEach(() => { | ||
api = JSON.parse(ApiBlueprintLoader().code); | ||
api = loadApiBlueprint(path.resolve(process.cwd(), './apiBlueprint.apib')); | ||
}); | ||
@@ -43,3 +44,3 @@ | ||
it('should allow multiple responses for each endpoint', () => { | ||
const { responses } = api[4]; | ||
const { responses } = api[5]; | ||
@@ -50,5 +51,5 @@ assert.deepEqual(Object.keys(responses), ['200', '404', '422']); | ||
it('should parse all endpoints documented in api blueprint', () => { | ||
assert.lengthOf(api, 5); | ||
assert.lengthOf(api, 6); | ||
}); | ||
}); | ||
}); |
@@ -5,3 +5,3 @@ 'use strict'; | ||
const HandlerTester = require('../../lib/handlerTester'); | ||
const HandlerTester = require('../../lib/HandlerTester'); | ||
@@ -13,15 +13,16 @@ describe('HandlerTester', () => { | ||
beforeEach(() => { | ||
handler = () => ( | ||
Promise.resolve() | ||
.then(() => 'Resolved') | ||
.catch(() => 'Cathed') | ||
handler = (event, context, callback) => ( | ||
callback(null, { | ||
statusCode: 200, | ||
body: JSON.stringify({ | ||
username: 'mtakac', | ||
email: 'marek.takac@pragonauts.com' | ||
}) | ||
}) | ||
); | ||
tester = new HandlerTester(handler); | ||
tester = new HandlerTester(handler, 200); | ||
}); | ||
describe('constructor', () => { | ||
it('should load api blueprint', () => { | ||
assert.isNotEmpty(tester._api.actions); | ||
}); | ||
@@ -50,6 +51,22 @@ it('should set handler', () => { | ||
assert.equal(limit, 5); | ||
assert.equal(offset, 15); | ||
assert.strictEqual(limit, '5'); | ||
assert.strictEqual(offset, '15'); | ||
}); | ||
it('additional calls adds data', () => { | ||
tester.queryStringParameters({ added: 4 }); | ||
const { limit, offset, added } = tester._queryStringParameters; | ||
assert.strictEqual(limit, '5'); | ||
assert.strictEqual(offset, '15'); | ||
assert.strictEqual(added, '4'); | ||
}); | ||
it('null resets property', () => { | ||
tester.queryStringParameters(); | ||
assert.strictEqual(tester._queryStringParameters, null); | ||
}); | ||
it('should return instance of HandlerTester', () => { | ||
@@ -68,3 +85,3 @@ assert.equal(result.constructor.name, 'HandlerTester'); | ||
it('should set _body property', () => { | ||
const { statusCode, data } = tester._body; | ||
const { statusCode, data } = JSON.parse(tester._body); | ||
@@ -75,2 +92,14 @@ assert.equal(statusCode, 200); | ||
it('null resets property', () => { | ||
tester.body(); | ||
assert.strictEqual(tester._body, null); | ||
}); | ||
it('string is set as string', () => { | ||
tester.body('hello'); | ||
assert.strictEqual(tester._body, 'hello'); | ||
}); | ||
it('should return instance of HandlerTester', () => { | ||
@@ -94,2 +123,8 @@ assert.equal(result.constructor.name, 'HandlerTester'); | ||
it('null resets property', () => { | ||
tester.pathParameters(); | ||
assert.strictEqual(tester._pathParameters, null); | ||
}); | ||
it('should return instance of HandlerTester', () => { | ||
@@ -100,5 +135,14 @@ assert.equal(result.constructor.name, 'HandlerTester'); | ||
describe('expect', () => { | ||
describe('request', () => { | ||
it('verifies request', async () => { | ||
const res = await tester.verify(); | ||
const { body, statusCode } = res; | ||
assert.strictEqual(body.username, 'mtakac'); | ||
assert.strictEqual(statusCode, 200); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
214
386.36%38927
-65.73%13
8.33%726
-76.54%1
Infinity%+ Added
+ Added
- Removed
- Removed
Updated
Updated