Skip to content

Commit 4069d50

Browse files
author
hirsch88
committed
Add e2e example
1 parent 5ebc69a commit 4069d50

File tree

8 files changed

+119
-17
lines changed

8 files changed

+119
-17
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Try it!! We are happy to hear your feedback or any kind of new features.
4242
- **API Documentation** thanks to [swagger](http://swagger.io/).
4343
- **API Monitoring** thanks to [express-status-monitor](https://github.com/RafalWilinski/express-status-monitor).
4444
- **Integrated Testing Tool** thanks to [Jest](https://facebook.github.io/jest).
45+
- **E2E API Testing** thanks to [supertest](https://github.com/visionmedia/supertest).
4546
- **Basic Security Features** thanks to [Helmet](https://helmetjs.github.io/).
4647
- **Easy event dispatching** thanks to [event-dispatch](https://github.com/pleerock/event-dispatch).
4748
- **Fast Database Building** with simple migration from [TypeORM](https://github.com/typeorm/typeorm).
@@ -389,6 +390,7 @@ npm start db.seed
389390
| [Helmet](https://helmetjs.github.io/) | Helmet helps you secure your Express apps by setting various HTTP headers. It’s not a silver bullet, but it can help! |
390391
| [Auth0 API Documentation](https://auth0.com/docs/api/management/v2) | Authentification service |
391392
| [Jest](http://facebook.github.io/jest/) | Delightful JavaScript Testing Library for unit and e2e tests |
393+
| [supertest](https://github.com/visionmedia/supertest) | Super-agent driven library for testing node.js HTTP servers using a fluent API |
392394
| [swagger Documentation](http://swagger.io/) | API Tool to describe and document your api. |
393395
| [SQLite Documentation](https://www.sitepoint.com/getting-started-sqlite3-basic-commands/) | Getting Started with SQLite3 – Basic Commands. |
394396
| [GraphQL Documentation](http://graphql.org/graphql-js/) | A query language for your API. |

package-scripts.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ module.exports = {
119119
script: 'nps "test.integration --verbose"'
120120
},
121121
run: {
122+
// -i. Run all tests serially in the current process, rather than creating a worker pool of child processes that run tests. This can be useful for debugging.
122123
script: 'cross-env NODE_ENV=test jest --testPathPattern=integration -i'
123124
},
124125
},
@@ -127,7 +128,6 @@ module.exports = {
127128
script: series(
128129
'nps banner.test',
129130
'nps test.e2e.pretest',
130-
runInNewWindow(series('nps build', 'nps start')),
131131
'nps test.e2e.run'
132132
)
133133
},
@@ -136,11 +136,10 @@ module.exports = {
136136
},
137137
verbose: {
138138
script: 'nps "test.e2e --verbose"'
139+
},run: {
140+
// -i. Run all tests serially in the current process, rather than creating a worker pool of child processes that run tests. This can be useful for debugging.
141+
script: 'cross-env NODE_ENV=test jest --testPathPattern=e2e -i'
139142
},
140-
run: series(
141-
`wait-on --timeout 120000 http-get://localhost:3000/api/info`,
142-
'cross-env NODE_ENV=test jest --testPathPattern=e2e -i'
143-
)
144143
}
145144
},
146145
/**

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"@types/reflect-metadata": "0.0.5",
5151
"@types/request": "^2.0.8",
5252
"@types/serve-favicon": "^2.2.29",
53+
"@types/supertest": "^2.0.4",
5354
"@types/uuid": "^3.4.3",
5455
"@types/winston": "^2.3.7",
5556
"body-parser": "^1.18.2",
@@ -84,6 +85,7 @@
8485
"request": "^2.83.0",
8586
"routing-controllers": "^0.7.6",
8687
"serve-favicon": "^2.4.5",
88+
"supertest": "^3.0.0",
8789
"swagger-ui-express": "^2.0.10",
8890
"ts-node": "^3.3.0",
8991
"tslint": "^5.8.0",

src/app.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { eventDispatchLoader } from './loaders/eventDispatchLoader';
2727
bootstrapMicroframework({
2828
/**
2929
* Loader is a place where you can configure all your modules during microframework
30-
* bootstrap. All loaders are executed one by one in a sequential order.
30+
* bootstrap process. All loaders are executed one by one in a sequential order.
3131
*/
3232
loaders: [
3333
winstonLoader,

src/loaders/expressLoader.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ export const expressLoader: MicroframeworkLoader = (settings: MicroframeworkSett
3434
});
3535

3636
// Run application to listen on given port
37-
expressApp.listen(env.app.port);
37+
if (!env.isTest) {
38+
const server = expressApp.listen(env.app.port);
39+
settings.setData('express_server', server);
40+
}
3841

3942
// Here we can set the data for other loaders
4043
settings.setData('express_app', expressApp);

test/e2e/api/info.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Application } from 'express';
2+
import * as request from 'supertest';
3+
import { bootstrapApp } from '../utils/app';
4+
5+
describe('/api', () => {
6+
7+
let app: Application;
8+
beforeAll(async () => app = await bootstrapApp());
9+
10+
test('GET: / should return the api-version', async (done) => {
11+
const response = await request(app)
12+
.get('/api')
13+
.expect('Content-Type', /json/)
14+
.expect(200);
15+
16+
console.log(response);
17+
done();
18+
});
19+
20+
});

test/e2e/utils/app.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
3+
import { bootstrapMicroframework, Microframework } from 'microframework';
4+
import { Application } from 'express';
5+
import { expressLoader } from './../../../src/loaders/expressLoader';
6+
import { winstonLoader } from './../../../src/loaders/winstonLoader';
7+
import { typeormLoader } from './../../../src/loaders/typeormLoader';
8+
import { swaggerLoader } from './../../../src/loaders/swaggerLoader';
9+
import { monitorLoader } from './../../../src/loaders/monitorLoader';
10+
import { homeLoader } from './../../../src/loaders/homeLoader';
11+
import { publicLoader } from './../../../src/loaders/publicLoader';
12+
import { iocLoader } from './../../../src/loaders/iocLoader';
13+
import { graphqlLoader } from './../../../src/loaders/graphqlLoader';
14+
import { eventDispatchLoader } from './../../../src/loaders/eventDispatchLoader';
15+
16+
export const bootstrapApp = async (): Promise<Application> => {
17+
const framework = await bootstrapMicroframework({
18+
loaders: [
19+
winstonLoader,
20+
iocLoader,
21+
eventDispatchLoader,
22+
typeormLoader,
23+
expressLoader,
24+
homeLoader,
25+
// publicLoader,
26+
// graphqlLoader,
27+
],
28+
});
29+
return framework.settings.getData('express_app') as Application;
30+
};

yarn.lock

+56-10
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@
113113
"@types/express-serve-static-core" "*"
114114
"@types/mime" "*"
115115

116+
"@types/superagent@*":
117+
version "3.5.6"
118+
resolved "https://registry.yarnpkg.com/@types/superagent/-/superagent-3.5.6.tgz#9cf2632c075ba9e601f6a610aadc23992d02394c"
119+
dependencies:
120+
"@types/node" "*"
121+
122+
"@types/supertest@^2.0.4":
123+
version "2.0.4"
124+
resolved "https://registry.yarnpkg.com/@types/supertest/-/supertest-2.0.4.tgz#28770e13293365e240a842d7d5c5a1b3d2dee593"
125+
dependencies:
126+
"@types/superagent" "*"
127+
116128
"@types/uuid@^3.4.3":
117129
version "3.4.3"
118130
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-3.4.3.tgz#121ace265f5569ce40f4f6d0ff78a338c732a754"
@@ -868,7 +880,7 @@ component-bind@1.0.0:
868880
version "1.0.0"
869881
resolved "https://registry.yarnpkg.com/component-bind/-/component-bind-1.0.0.tgz#00c608ab7dcd93897c0009651b1d3a8e1e73bbd1"
870882

871-
component-emitter@1.2.1:
883+
component-emitter@1.2.1, component-emitter@^1.2.0:
872884
version "1.2.1"
873885
resolved "https://registry.yarnpkg.com/component-emitter/-/component-emitter-1.2.1.tgz#137918d6d78283f7df7a6b7c5a63e140e69425e6"
874886

@@ -960,6 +972,10 @@ cookie@0.3.1, cookie@^0.3.1:
960972
version "0.3.1"
961973
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.3.1.tgz#e7e0a1f9ef43b4c8ba925c5c5a96e806d16873bb"
962974

975+
cookiejar@^2.1.0:
976+
version "2.1.1"
977+
resolved "https://registry.yarnpkg.com/cookiejar/-/cookiejar-2.1.1.tgz#41ad57b1b555951ec171412a81942b1e8200d34a"
978+
963979
copyfiles@^1.2.0:
964980
version "1.2.0"
965981
resolved "https://registry.yarnpkg.com/copyfiles/-/copyfiles-1.2.0.tgz#a8da3ac41aa2220ae29bd3c58b6984294f2c593c"
@@ -1445,7 +1461,7 @@ express@^4.16.2:
14451461
utils-merge "1.0.1"
14461462
vary "~1.1.2"
14471463

1448-
extend@~3.0.0, extend@~3.0.1:
1464+
extend@^3.0.0, extend@~3.0.0, extend@~3.0.1:
14491465
version "3.0.1"
14501466
resolved "https://registry.yarnpkg.com/extend/-/extend-3.0.1.tgz#a755ea7bc1adfcc5a31ce7e762dbaadc5e636444"
14511467

@@ -1561,22 +1577,26 @@ forever-agent@~0.6.1:
15611577
version "0.6.1"
15621578
resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91"
15631579

1564-
form-data@~2.1.1:
1565-
version "2.1.4"
1566-
resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.1.4.tgz#33c183acf193276ecaa98143a69e94bfee1750d1"
1580+
form-data@^2.3.1, form-data@~2.3.1:
1581+
version "2.3.1"
1582+
resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.3.1.tgz#6fb94fbd71885306d73d15cc497fe4cc4ecd44bf"
15671583
dependencies:
15681584
asynckit "^0.4.0"
15691585
combined-stream "^1.0.5"
15701586
mime-types "^2.1.12"
15711587

1572-
form-data@~2.3.1:
1573-
version "2.3.1"
1574-
resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.3.1.tgz#6fb94fbd71885306d73d15cc497fe4cc4ecd44bf"
1588+
form-data@~2.1.1:
1589+
version "2.1.4"
1590+
resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.1.4.tgz#33c183acf193276ecaa98143a69e94bfee1750d1"
15751591
dependencies:
15761592
asynckit "^0.4.0"
15771593
combined-stream "^1.0.5"
15781594
mime-types "^2.1.12"
15791595

1596+
formidable@^1.1.1:
1597+
version "1.1.1"
1598+
resolved "https://registry.yarnpkg.com/formidable/-/formidable-1.1.1.tgz#96b8886f7c3c3508b932d6bd70c4d3a88f35f1a9"
1599+
15801600
forwarded@~0.1.2:
15811601
version "0.1.2"
15821602
resolved "https://registry.yarnpkg.com/forwarded/-/forwarded-0.1.2.tgz#98c23dab1175657b8c0573e8ceccd91b0ff18c84"
@@ -2774,7 +2794,7 @@ merge@^1.1.3:
27742794
version "1.2.0"
27752795
resolved "https://registry.yarnpkg.com/merge/-/merge-1.2.0.tgz#7531e39d4949c281a66b8c5a6e0265e8b05894da"
27762796

2777-
methods@~1.1.2:
2797+
methods@^1.1.1, methods@~1.1.2:
27782798
version "1.1.2"
27792799
resolved "https://registry.yarnpkg.com/methods/-/methods-1.1.2.tgz#5529a4d67654134edcc5266656835b0f851afcee"
27802800

@@ -2820,6 +2840,10 @@ mime@1.4.1:
28202840
version "1.4.1"
28212841
resolved "https://registry.yarnpkg.com/mime/-/mime-1.4.1.tgz#121f9ebc49e3766f311a76e1fa1c8003c4b03aa6"
28222842

2843+
mime@^1.4.1:
2844+
version "1.6.0"
2845+
resolved "https://registry.yarnpkg.com/mime/-/mime-1.6.0.tgz#32cd9e5c64553bd58d19a568af452acff04981b1"
2846+
28232847
mimic-fn@^1.0.0:
28242848
version "1.1.0"
28252849
resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-1.1.0.tgz#e667783d92e89dbd342818b5230b9d62a672ad18"
@@ -3501,7 +3525,7 @@ read-pkg@^2.0.0:
35013525
normalize-package-data "^2.3.2"
35023526
path-type "^2.0.0"
35033527

3504-
readable-stream@2.3.3, readable-stream@^2.0.2, readable-stream@^2.0.6, readable-stream@^2.1.4, readable-stream@^2.1.5:
3528+
readable-stream@2.3.3, readable-stream@^2.0.2, readable-stream@^2.0.5, readable-stream@^2.0.6, readable-stream@^2.1.4, readable-stream@^2.1.5:
35053529
version "2.3.3"
35063530
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.3.tgz#368f2512d79f9d46fdfc71349ae7878bbc1eb95c"
35073531
dependencies:
@@ -4051,6 +4075,28 @@ subarg@^1.0.0:
40514075
dependencies:
40524076
minimist "^1.1.0"
40534077

4078+
superagent@^3.0.0:
4079+
version "3.8.2"
4080+
resolved "https://registry.yarnpkg.com/superagent/-/superagent-3.8.2.tgz#e4a11b9d047f7d3efeb3bbe536d9ec0021d16403"
4081+
dependencies:
4082+
component-emitter "^1.2.0"
4083+
cookiejar "^2.1.0"
4084+
debug "^3.1.0"
4085+
extend "^3.0.0"
4086+
form-data "^2.3.1"
4087+
formidable "^1.1.1"
4088+
methods "^1.1.1"
4089+
mime "^1.4.1"
4090+
qs "^6.5.1"
4091+
readable-stream "^2.0.5"
4092+
4093+
supertest@^3.0.0:
4094+
version "3.0.0"
4095+
resolved "https://registry.yarnpkg.com/supertest/-/supertest-3.0.0.tgz#8d4bb68fd1830ee07033b1c5a5a9a4021c965296"
4096+
dependencies:
4097+
methods "~1.1.2"
4098+
superagent "^3.0.0"
4099+
40544100
supports-color@^0.2.0:
40554101
version "0.2.0"
40564102
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-0.2.0.tgz#d92de2694eb3f67323973d7ae3d8b55b4c22190a"

0 commit comments

Comments
 (0)