Skip to content

Commit f448931

Browse files
Fabian Wilesalexeagle
authored andcommitted
feat(examples): add nestjs test
1 parent d48f237 commit f448931

File tree

7 files changed

+1025
-18
lines changed

7 files changed

+1025
-18
lines changed

examples/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,12 @@ example_integration_test(
133133
example_integration_test(
134134
name = "examples_nestjs",
135135
bazel_commands = [
136-
"build ...",
136+
"test ...",
137137
# Test cross-platform build
138138
"build --platforms=@build_bazel_rules_nodejs//toolchains/node:linux_amd64 //src:docker",
139139
],
140140
npm_packages = {
141+
"//packages/jasmine:npm_package": "@bazel/jasmine",
141142
"//packages/typescript:npm_package": "@bazel/typescript",
142143
},
143144
owners = [

examples/nestjs/package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
{
22
"private": true,
33
"dependencies": {
4+
"@bazel/bazel": "^2.0.0",
5+
"@bazel/ibazel": "^0.11.0",
6+
"@bazel/jasmine": "^1.0.0",
47
"@nestjs/common": "6.5.2",
58
"@nestjs/core": "6.5.2",
69
"@nestjs/platform-express": "6.5.2",
10+
"@types/jasmine": "^3.5.0",
11+
"@types/supertest": "^2.0.8",
712
"minimist": "1.2.0",
813
"reflect-metadata": "0.1.13",
9-
"rxjs": "6.5.2"
14+
"rxjs": "6.5.2",
15+
"supertest": "^4.0.2"
1016
},
1117
"devDependencies": {
1218
"@bazel/typescript": "^1.0.1",
1319
"@types/node": "12.6.3",
1420
"typescript": "3.5.3"
1521
},
1622
"scripts": {
17-
"test": "bazel build ... && bazel build --platforms=@build_bazel_rules_nodejs//toolchains/node:linux_amd64 //src:docker"
23+
"test": "bazel test ... && bazel build --platforms=@build_bazel_rules_nodejs//toolchains/node:linux_amd64 //src:docker"
1824
}
1925
}

examples/nestjs/src/BUILD.bazel

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,37 @@
1414

1515
package(default_visibility = ["//visibility:public"])
1616

17+
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary")
18+
load("@npm_bazel_jasmine//:index.bzl", "jasmine_node_test")
1719
load("@npm_bazel_typescript//:index.bzl", "ts_library")
1820

1921
ts_library(
2022
name = "app",
21-
srcs = glob(["*.ts"]),
23+
srcs = glob(
24+
["*.ts"],
25+
exclude = ["*.spec.ts"],
26+
),
2227
deps = [
2328
"@npm//@nestjs/common",
2429
"@npm//@nestjs/core",
30+
"@npm//@nestjs/platform-express",
2531
"@npm//@types/node",
2632
"@npm//tslib",
2733
],
2834
)
2935

30-
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary")
36+
ts_library(
37+
name = "test_lib",
38+
srcs = glob(["*.spec.ts"]),
39+
deps = [
40+
":app",
41+
"@npm//@nestjs/common",
42+
"@npm//@types/jasmine",
43+
"@npm//@types/supertest",
44+
"@npm//jasmine",
45+
"@npm//supertest",
46+
],
47+
)
3148

3249
# bazel run //src:server -- --port=4000
3350
# TODO(gregmagolan): add test for nest server with protractor rule once it is brought in
@@ -37,12 +54,18 @@ nodejs_binary(
3754
":app",
3855
"@npm//@nestjs/common",
3956
"@npm//@nestjs/core",
40-
"@npm//@nestjs/platform-express",
4157
"@npm//minimist",
4258
],
4359
entry_point = ":main.ts",
4460
)
4561

62+
jasmine_node_test(
63+
name = "test",
64+
deps = [
65+
":test_lib",
66+
],
67+
)
68+
4669
load("@io_bazel_rules_docker//nodejs:image.bzl", "nodejs_image")
4770

4871
# bazel build --platforms=@build_bazel_rules_nodejs//toolchains/node:linux_amd64 //src:docker

examples/nestjs/src/app.controller.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import {Controller, Get} from '@nestjs/common';
22

33
@Controller()
44
export class AppController {
5-
@Get()
6-
getGreeting(): string {
7-
return 'Hello world!';
5+
@Get('hello')
6+
getGreeting(): {message: string} {
7+
return {message: 'Hello world!'};
88
}
99
}

examples/nestjs/src/main.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {INestApplication} from '@nestjs/common';
2+
import * as request from 'supertest';
3+
4+
import {bootstrap} from './main';
5+
6+
describe('App', () => {
7+
let server: INestApplication;
8+
9+
beforeAll(async () => {
10+
server = await bootstrap(3000);
11+
});
12+
afterAll(async () => {
13+
await server.close();
14+
})
15+
16+
it(`GET /`, () => {
17+
return request(server.getHttpServer()).get('/hello').expect(200).expect({
18+
message: 'Hello world!'
19+
});
20+
});
21+
});

examples/nestjs/src/main.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import {Logger} from '@nestjs/common';
1+
import {INestApplication, Logger} from '@nestjs/common';
22
import {NestFactory} from '@nestjs/core';
3+
import {ExpressAdapter} from '@nestjs/platform-express';
34

45
import {AppModule} from './app.module';
56

6-
async function bootstrap(port: number) {
7-
const app = await NestFactory.create(AppModule);
7+
export async function bootstrap(port: number): Promise<INestApplication> {
8+
const app = await NestFactory.create(AppModule, new ExpressAdapter());
89
await app.listen(port);
910
Logger.log(`Application served at http://localhost:${port}`);
11+
return app;
1012
}
1113

1214
if (require.main === module) {

0 commit comments

Comments
 (0)