Skip to content

Commit d75be77

Browse files
committed
feat(testlab): add createRestAppClient(), simplify usage in tests
Clean up all test code (including examples in the documentation) to use `Client` instead of `supertest.SuperTest<supertest.Test>`. Remove `createClientForRestServer` because it was not used anywhere and had the issue of not stopping the server after the test is done. Add a new helper `createRestAppClient` instead, rework acceptance tests (including the templates) to use this new helper.
1 parent bd6c2ed commit d75be77

File tree

15 files changed

+160
-94
lines changed

15 files changed

+160
-94
lines changed

docs/site/Implementing-features.shelved.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ Create `test/acceptance/product.acceptance.ts` with the following contents:
4040
```ts
4141
import {HelloWorldApp} from '../..';
4242
import {RestBindings, RestServer} from '@loopback/rest';
43-
import {expect, supertest} from '@loopback/testlab';
43+
import {Client, expect, supertest} from '@loopback/testlab';
4444

4545
describe('Product (acceptance)', () => {
4646
let app: HelloWorldApp;
47-
let request: supertest.SuperTest<supertest.Test>;
47+
let request: Client;
4848

4949
before(givenEmptyDatabase);
5050
before(givenRunningApp);
@@ -84,13 +84,13 @@ describe('Product (acceptance)', () => {
8484
}
8585

8686
async function givenRunningApp() {
87-
app = new HelloWorldApp();
88-
const server = await app.getServer(RestServer);
89-
server.bind(RestBindings.PORT).to(0);
87+
app = new HelloWorldApp({
88+
rest: {
89+
port: 0,
90+
},
91+
});
9092
await app.start();
91-
92-
const port: number = await server.get(RestBindings.PORT);
93-
request = supertest(`http://127.0.0.1:${port}`);
93+
request = supertest(app.restServer.url);
9494
}
9595

9696
async function givenProduct(data: Object) {

docs/site/Testing-your-application.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ Here is an example of an acceptance test:
833833

834834
```ts
835835
import {HelloWorldApplication} from '../..';
836-
import {expect, createClientForHandler, Client} from '@loopback/testlab';
836+
import {Client, createRestAppClient, expect} from '@loopback/testlab';
837837
import {givenEmptyDatabase, givenProduct} from '../helpers/database.helpers';
838838
import {RestServer, RestBindings} from '@loopback/rest';
839839
import {testdb} from '../fixtures/datasources/testdb.datasource';
@@ -870,14 +870,16 @@ describe('Product (acceptance)', () => {
870870
});
871871

872872
async function givenRunningApp() {
873-
app = new HelloWorldApplication();
873+
app = new HelloWorldApplication({
874+
rest: {
875+
port: 0,
876+
},
877+
});
874878
app.dataSource(testdb);
875-
const server = await app.getServer(RestServer);
876-
server.bind(RestBindings.PORT).to(0);
877879
await app.boot();
878880
await app.start();
879881

880-
client = createClientForHandler(server.handleHttp);
882+
client = createRestAppClient(app);
881883
}
882884
});
883885
```

examples/hello-world/test/acceptance/application.acceptance.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,22 @@
33
// This file is licensed under the MIT License.
44
// License text available at https://opensource.org/licenses/MIT
55

6-
import {createClientForHandler, expect, supertest} from '@loopback/testlab';
7-
import {RestServer} from '@loopback/rest';
6+
import {
7+
Client,
8+
createRestAppClient,
9+
expect,
10+
givenHttpServerConfig,
11+
} from '@loopback/testlab';
812
import {HelloWorldApplication} from '../../src/application';
913

1014
describe('Application', () => {
1115
let app: HelloWorldApplication;
12-
let client: supertest.SuperTest<supertest.Test>;
13-
let server: RestServer;
16+
let client: Client;
1417

1518
before(givenAnApplication);
1619
before(async () => {
1720
await app.start();
18-
server = await app.getServer(RestServer);
19-
});
20-
21-
before(() => {
22-
client = createClientForHandler(server.requestHandler);
21+
client = createRestAppClient(app);
2322
});
2423
after(async () => {
2524
await app.stop();
@@ -32,9 +31,9 @@ describe('Application', () => {
3231

3332
function givenAnApplication() {
3433
app = new HelloWorldApplication({
35-
rest: {
34+
rest: givenHttpServerConfig({
3635
port: 0,
37-
},
36+
}),
3837
disableConsoleLog: true,
3938
});
4039
}

examples/soap-calculator/test/acceptance/application.acceptance.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import {supertest} from '@loopback/testlab';
1+
import {Client, createRestAppClient, expect} from '@loopback/testlab';
22
import {SoapCalculatorApplication} from '../../src/application';
3-
import {expect} from '@loopback/testlab';
43

54
describe('Application', function() {
65
let app: SoapCalculatorApplication;
7-
let client: supertest.SuperTest<supertest.Test>;
6+
let client: Client;
87

98
// tslint:disable-next-line:no-invalid-this
109
this.timeout(30000);
@@ -14,7 +13,7 @@ describe('Application', function() {
1413
before(async () => {
1514
await app.boot();
1615
await app.start();
17-
client = supertest(app.restServer.url);
16+
client = createRestAppClient(app);
1817
});
1918

2019
after(async () => {

examples/todo-list/test/acceptance/todo-list-todo.acceptance.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@
33
// This file is licensed under the MIT License.
44
// License text available at https://opensource.org/licenses/MIT
55

6-
import {createClientForHandler, expect, supertest} from '@loopback/testlab';
6+
import {
7+
Client,
8+
createRestAppClient,
9+
expect,
10+
givenHttpServerConfig,
11+
} from '@loopback/testlab';
712
import {TodoListApplication} from '../../src/application';
813
import {Todo, TodoList} from '../../src/models/';
9-
import {TodoRepository, TodoListRepository} from '../../src/repositories/';
14+
import {TodoListRepository, TodoRepository} from '../../src/repositories/';
1015
import {givenTodo, givenTodoList} from '../helpers';
1116

1217
describe('TodoListApplication', () => {
1318
let app: TodoListApplication;
14-
let client: supertest.SuperTest<supertest.Test>;
19+
let client: Client;
1520
let todoRepo: TodoRepository;
1621
let todoListRepo: TodoListRepository;
1722

@@ -23,7 +28,7 @@ describe('TodoListApplication', () => {
2328
before(givenTodoRepository);
2429
before(givenTodoListRepository);
2530
before(() => {
26-
client = createClientForHandler(app.requestHandler);
31+
client = createRestAppClient(app);
2732
});
2833

2934
beforeEach(async () => {
@@ -119,9 +124,9 @@ describe('TodoListApplication', () => {
119124

120125
async function givenRunningApplicationWithCustomConfiguration() {
121126
app = new TodoListApplication({
122-
rest: {
127+
rest: givenHttpServerConfig({
123128
port: 0,
124-
},
129+
}),
125130
});
126131

127132
await app.boot();

examples/todo-list/test/acceptance/todo-list.acceptance.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,28 @@
44
// License text available at https://opensource.org/licenses/MIT
55

66
import {EntityNotFoundError} from '@loopback/repository';
7-
import {createClientForHandler, expect, supertest} from '@loopback/testlab';
7+
import {
8+
Client,
9+
createRestAppClient,
10+
expect,
11+
givenHttpServerConfig,
12+
} from '@loopback/testlab';
813
import {TodoListApplication} from '../../src/application';
914
import {TodoList} from '../../src/models/';
1015
import {TodoListRepository} from '../../src/repositories/';
1116
import {givenTodoList} from '../helpers';
1217

1318
describe('TodoListApplication', () => {
1419
let app: TodoListApplication;
15-
let client: supertest.SuperTest<supertest.Test>;
20+
let client: Client;
1621
let todoListRepo: TodoListRepository;
1722

1823
before(givenRunningApplicationWithCustomConfiguration);
1924
after(() => app.stop());
2025

2126
before(givenTodoListRepository);
2227
before(() => {
23-
client = createClientForHandler(app.requestHandler);
28+
client = createRestAppClient(app);
2429
});
2530

2631
beforeEach(async () => {
@@ -140,9 +145,9 @@ describe('TodoListApplication', () => {
140145

141146
async function givenRunningApplicationWithCustomConfiguration() {
142147
app = new TodoListApplication({
143-
rest: {
148+
rest: givenHttpServerConfig({
144149
port: 0,
145-
},
150+
}),
146151
});
147152

148153
await app.boot();

examples/todo-list/test/acceptance/todo.acceptance.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,28 @@
44
// License text available at https://opensource.org/licenses/MIT
55

66
import {EntityNotFoundError} from '@loopback/repository';
7-
import {createClientForHandler, expect, supertest} from '@loopback/testlab';
7+
import {
8+
Client,
9+
createRestAppClient,
10+
expect,
11+
givenHttpServerConfig,
12+
} from '@loopback/testlab';
813
import {TodoListApplication} from '../../src/application';
914
import {Todo} from '../../src/models/';
1015
import {TodoRepository} from '../../src/repositories/';
1116
import {givenTodo} from '../helpers';
1217

1318
describe('TodoListApplication', () => {
1419
let app: TodoListApplication;
15-
let client: supertest.SuperTest<supertest.Test>;
20+
let client: Client;
1621
let todoRepo: TodoRepository;
1722

1823
before(givenRunningApplicationWithCustomConfiguration);
1924
after(() => app.stop());
2025

2126
before(givenTodoRepository);
2227
before(() => {
23-
client = createClientForHandler(app.requestHandler);
28+
client = createRestAppClient(app);
2429
});
2530

2631
beforeEach(async () => {
@@ -138,9 +143,9 @@ describe('TodoListApplication', () => {
138143

139144
async function givenRunningApplicationWithCustomConfiguration() {
140145
app = new TodoListApplication({
141-
rest: {
146+
rest: givenHttpServerConfig({
142147
port: 0,
143-
},
148+
}),
144149
});
145150

146151
await app.boot();

examples/todo/test/acceptance/todo.acceptance.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
// License text available at https://opensource.org/licenses/MIT
55

66
import {EntityNotFoundError} from '@loopback/repository';
7-
import {createClientForHandler, expect, supertest} from '@loopback/testlab';
7+
import {
8+
Client,
9+
createRestAppClient,
10+
expect,
11+
givenHttpServerConfig,
12+
} from '@loopback/testlab';
813
import {TodoListApplication} from '../../src/application';
914
import {Todo} from '../../src/models/';
1015
import {TodoRepository} from '../../src/repositories/';
@@ -18,7 +23,7 @@ import {
1823

1924
describe('TodoApplication', () => {
2025
let app: TodoListApplication;
21-
let client: supertest.SuperTest<supertest.Test>;
26+
let client: Client;
2227
let todoRepo: TodoRepository;
2328

2429
let cachingProxy: HttpCachingProxy;
@@ -30,7 +35,7 @@ describe('TodoApplication', () => {
3035

3136
before(givenTodoRepository);
3237
before(() => {
33-
client = createClientForHandler(app.requestHandler);
38+
client = createRestAppClient(app);
3439
});
3540

3641
beforeEach(async () => {
@@ -170,9 +175,9 @@ describe('TodoApplication', () => {
170175

171176
async function givenRunningApplicationWithCustomConfiguration() {
172177
app = new TodoListApplication({
173-
rest: {
178+
rest: givenHttpServerConfig({
174179
port: 0,
175-
},
180+
}),
176181
});
177182

178183
await app.boot();

packages/boot/test/acceptance/controller.booter.acceptance.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
// This file is licensed under the MIT License.
44
// License text available at https://opensource.org/licenses/MIT
55

6-
import {Client, createClientForHandler, TestSandbox} from '@loopback/testlab';
7-
import {RestServer} from '@loopback/rest';
6+
import {
7+
createRestAppClient,
8+
givenHttpServerConfig,
9+
TestSandbox,
10+
} from '@loopback/testlab';
811
import {resolve} from 'path';
912
import {BooterApp} from '../fixtures/application';
1013

@@ -22,8 +25,7 @@ describe('controller booter acceptance tests', () => {
2225
await app.boot();
2326
await app.start();
2427

25-
const server: RestServer = await app.getServer(RestServer);
26-
const client: Client = createClientForHandler(server.requestHandler);
28+
const client = createRestAppClient(app);
2729

2830
// Default Controllers = /controllers with .controller.js ending (nested = true);
2931
await client.get('/one').expect(200, 'ControllerOne.one()');
@@ -38,7 +40,9 @@ describe('controller booter acceptance tests', () => {
3840
);
3941

4042
const MyApp = require(resolve(SANDBOX_PATH, 'application.js')).BooterApp;
41-
app = new MyApp();
43+
app = new MyApp({
44+
rest: givenHttpServerConfig({port: 0}),
45+
});
4246
}
4347

4448
async function stopApp() {
Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
import {createClientForHandler, supertest} from '@loopback/testlab';
2-
import {RestServer} from '@loopback/rest';
1+
import {
2+
Client,
3+
createRestAppClient,
4+
givenHttpServerConfig,
5+
} from '@loopback/testlab';
36
import {<%= project.applicationName %>} from '../..';
47

58
describe('PingController', () => {
69
let app: <%= project.applicationName %>;
7-
let server: RestServer;
8-
let client: supertest.SuperTest<supertest.Test>;
10+
let client: Client;
911

1012
before(givenAnApplication);
1113

12-
before(givenARestServer);
13-
1414
before(async () => {
1515
await app.boot();
1616
await app.start();
1717
});
1818

1919
before(() => {
20-
client = createClientForHandler(server.requestHandler);
20+
client = createRestAppClient(app);
2121
});
2222

2323
after(async () => {
@@ -30,13 +30,9 @@ describe('PingController', () => {
3030

3131
function givenAnApplication() {
3232
app = new <%= project.applicationName %>({
33-
rest: {
33+
rest: givenHttpServerConfig({
3434
port: 0,
35-
},
35+
}),
3636
});
3737
}
38-
39-
async function givenARestServer() {
40-
server = await app.getServer(RestServer);
41-
}
4238
});

0 commit comments

Comments
 (0)