Skip to content

Commit 55d59d4

Browse files
author
hirsch88
committed
add black-box testing
1 parent 063932b commit 55d59d4

File tree

6 files changed

+91
-11
lines changed

6 files changed

+91
-11
lines changed

README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,5 @@ The port will be displayed to you as `http://0.0.0.0:3000` (or if you prefer IPv
7373
## License
7474
[MIT](/LICENSE)
7575

76-
## ToDo's
77-
https://blog.risingstack.com/10-best-practices-for-writing-node-js-rest-apis/
78-
[ ] Add api doc - http://apidocjs.com
79-
[ ] Add black box testing https://www.npmjs.com/package/supertest
80-
81-
8276
---
8377
Made with ♥ by Gery Hirschfeld ([@GeryHirschfeld1](https://twitter.com/GeryHirschfeld1)) and [contributors](https://github.com/w3tecch/express-typescript-boilerplate/graphs/contributors)

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"serve": "./node_modules/.bin/nodemon --watch 'src/**/*.ts'",
1111
"serve:debug": "npm run serve -- --debug=6666",
1212
"lint": "./node_modules/.bin/gulp lint",
13-
"test": "NODE_ENV=test ./node_modules/.bin/jest --coverage",
13+
"test": "NODE_ENV=test ./node_modules/.bin/jest ./test/unit --coverage",
14+
"test:black-box": "NODE_ENV=test ./node_modules/.bin/jest ./test/black-box -i",
1415
"build": "./node_modules/.bin/gulp build",
1516
"clean": "./node_modules/.bin/gulp clean",
1617
"zip": "./node_modules/.bin/gulp zip",
@@ -62,6 +63,7 @@
6263
"@types/morgan": "^1.7.32",
6364
"@types/reflect-metadata": "0.0.5",
6465
"@types/request": "0.0.42",
66+
"@types/request-promise": "^4.1.33",
6567
"@types/serve-favicon": "^2.2.28",
6668
"@types/winston": "^2.3.1",
6769
"body-parser": "^1.17.1",
@@ -102,7 +104,8 @@
102104
"path": "^0.12.7",
103105
"pg": "^6.1.5",
104106
"reflect-metadata": "^0.1.10",
105-
"request": "^2.79.0",
107+
"request": "^2.81.0",
108+
"request-promise": "^4.2.1",
106109
"require-dir": "^0.3.1",
107110
"run-sequence": "^1.2.2",
108111
"serve-favicon": "^2.4.2",

test/black-box/home.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { api } from './lib/api';
2+
3+
4+
test('GET / - should return the api info as a json', async () => {
5+
const res = await api('GET', '/api');
6+
res.expectJson();
7+
res.expectStatusCode(200);
8+
9+
const body = res.getBody<any>();
10+
const pkg = require('../../package.json');
11+
expect(body.name).toBe(pkg.name);
12+
expect(body.version).toBe(pkg.version);
13+
expect(body.description).toBe(pkg.description);
14+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import * as _ from 'lodash';
2+
3+
4+
export class ApiResponeTest {
5+
constructor(private res: any) { }
6+
7+
getBody<T>(): T {
8+
return JSON.parse(this.res['body']);
9+
}
10+
11+
getHeaders<T>(): T {
12+
return this.res['headers'];
13+
}
14+
15+
expectStatusCode(code: number): ApiResponeTest {
16+
expect(this.res['statusCode']).toBe(code);
17+
return this;
18+
}
19+
20+
expectJson(): ApiResponeTest {
21+
expect(this.getHeaders()['content-type']).toContain('json');
22+
return this;
23+
}
24+
25+
expectData(keys: string[]): ApiResponeTest {
26+
const a = keys.sort();
27+
const b = Object.keys(this.getBody()).sort();
28+
expect(_.isEqual(a, b)).toBeTruthy();
29+
return this;
30+
}
31+
}

test/black-box/lib/api.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require('dotenv').config();
2+
import * as request from 'request-promise';
3+
import { OptionsWithUri } from 'request-promise';
4+
import { ApiResponeTest } from './ApiResponeTest';
5+
6+
7+
export const api = async (method: string, path: string, options: OptionsWithUri = { uri: '' }) => {
8+
options.method = method;
9+
options.uri = `${process.env.APP_HOST}:${process.env.APP_PORT}${path}`;
10+
options.resolveWithFullResponse = true;
11+
return new ApiResponeTest(await request(options));
12+
};

yarn.lock

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,14 @@
111111
version "0.0.5"
112112
resolved "https://registry.yarnpkg.com/@types/reflect-metadata/-/reflect-metadata-0.0.5.tgz#9c042bfa9803d577aad4f57dfbca4b7cae4286fe"
113113

114-
"@types/request@0.0.42":
114+
"@types/request-promise@^4.1.33":
115+
version "4.1.33"
116+
resolved "https://registry.yarnpkg.com/@types/request-promise/-/request-promise-4.1.33.tgz#b0cde534f80ebae026eb5fa470e45a192682ca1e"
117+
dependencies:
118+
"@types/bluebird" "*"
119+
"@types/request" "*"
120+
121+
"@types/request@*", "@types/request@0.0.42":
115122
version "0.0.42"
116123
resolved "https://registry.yarnpkg.com/@types/request/-/request-0.0.42.tgz#e47a53bf0b130464854fb693297746a0c0479c31"
117124
dependencies:
@@ -820,7 +827,7 @@ block-stream@*:
820827
dependencies:
821828
inherits "~2.0.0"
822829

823-
bluebird@^3.1.1, bluebird@^3.4.3, bluebird@^3.4.6:
830+
bluebird@^3.1.1, bluebird@^3.4.3, bluebird@^3.4.6, bluebird@^3.5.0:
824831
version "3.5.0"
825832
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.0.tgz#791420d7f551eea2897453a8a77653f96606d67c"
826833

@@ -4625,6 +4632,21 @@ replace-ext@^1.0.0:
46254632
version "1.0.0"
46264633
resolved "https://registry.yarnpkg.com/replace-ext/-/replace-ext-1.0.0.tgz#de63128373fcbf7c3ccfa4de5a480c45a67958eb"
46274634

4635+
request-promise-core@1.1.1:
4636+
version "1.1.1"
4637+
resolved "https://registry.yarnpkg.com/request-promise-core/-/request-promise-core-1.1.1.tgz#3eee00b2c5aa83239cfb04c5700da36f81cd08b6"
4638+
dependencies:
4639+
lodash "^4.13.1"
4640+
4641+
request-promise@^4.2.1:
4642+
version "4.2.1"
4643+
resolved "https://registry.yarnpkg.com/request-promise/-/request-promise-4.2.1.tgz#7eec56c89317a822cbfea99b039ce543c2e15f67"
4644+
dependencies:
4645+
bluebird "^3.5.0"
4646+
request-promise-core "1.1.1"
4647+
stealthy-require "^1.1.0"
4648+
tough-cookie ">=2.3.0"
4649+
46284650
request@^2.79.0, request@^2.81.0:
46294651
version "2.81.0"
46304652
resolved "https://registry.yarnpkg.com/request/-/request-2.81.0.tgz#c6928946a0e06c5f8d6f8a9333469ffda46298a0"
@@ -4954,6 +4976,10 @@ stack-trace@0.0.x:
49544976
version "1.3.1"
49554977
resolved "https://registry.yarnpkg.com/statuses/-/statuses-1.3.1.tgz#faf51b9eb74aaef3b3acf4ad5f61abf24cb7b93e"
49564978

4979+
stealthy-require@^1.1.0:
4980+
version "1.1.1"
4981+
resolved "https://registry.yarnpkg.com/stealthy-require/-/stealthy-require-1.1.1.tgz#35b09875b4ff49f26a777e509b3090a3226bf24b"
4982+
49574983
stream-combiner@~0.0.4:
49584984
version "0.0.4"
49594985
resolved "https://registry.yarnpkg.com/stream-combiner/-/stream-combiner-0.0.4.tgz#4d5e433c185261dde623ca3f44c586bcf5c4ad14"
@@ -5216,7 +5242,7 @@ touch@1.0.0, touch@^1.0.0:
52165242
dependencies:
52175243
nopt "~1.0.10"
52185244

5219-
tough-cookie@^2.0.0, tough-cookie@^2.3.2, tough-cookie@~2.3.0:
5245+
tough-cookie@>=2.3.0, tough-cookie@^2.0.0, tough-cookie@^2.3.2, tough-cookie@~2.3.0:
52205246
version "2.3.2"
52215247
resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.3.2.tgz#f081f76e4c85720e6c37a5faced737150d84072a"
52225248
dependencies:

0 commit comments

Comments
 (0)