Skip to content

Commit

Permalink
feat(testlab): add createRestAppClient(), simplify usage in tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bajtos committed Sep 20, 2018
1 parent bd6c2ed commit d46d91a
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 82 deletions.
16 changes: 8 additions & 8 deletions docs/site/Implementing-features.shelved.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ Create `test/acceptance/product.acceptance.ts` with the following contents:
```ts
import {HelloWorldApp} from '../..';
import {RestBindings, RestServer} from '@loopback/rest';
import {expect, supertest} from '@loopback/testlab';
import {Client, expect, supertest} from '@loopback/testlab';

describe('Product (acceptance)', () => {
let app: HelloWorldApp;
let request: supertest.SuperTest<supertest.Test>;
let request: Client;

before(givenEmptyDatabase);
before(givenRunningApp);
Expand Down Expand Up @@ -84,13 +84,13 @@ describe('Product (acceptance)', () => {
}

async function givenRunningApp() {
app = new HelloWorldApp();
const server = await app.getServer(RestServer);
server.bind(RestBindings.PORT).to(0);
app = new HelloWorldApp({
rest: {
port: 0,
},
});
await app.start();

const port: number = await server.get(RestBindings.PORT);
request = supertest(`http://127.0.0.1:${port}`);
request = supertest(app.restServer.url);
}

async function givenProduct(data: Object) {
Expand Down
12 changes: 7 additions & 5 deletions docs/site/Testing-your-application.md
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ Here is an example of an acceptance test:

```ts
import {HelloWorldApplication} from '../..';
import {expect, createClientForHandler, Client} from '@loopback/testlab';
import {Client, createRestAppClient, expect} from '@loopback/testlab';
import {givenEmptyDatabase, givenProduct} from '../helpers/database.helpers';
import {RestServer, RestBindings} from '@loopback/rest';
import {testdb} from '../fixtures/datasources/testdb.datasource';
Expand Down Expand Up @@ -870,14 +870,16 @@ describe('Product (acceptance)', () => {
});

async function givenRunningApp() {
app = new HelloWorldApplication();
app = new HelloWorldApplication({
rest: {
port: 0,
},
});
app.dataSource(testdb);
const server = await app.getServer(RestServer);
server.bind(RestBindings.PORT).to(0);
await app.boot();
await app.start();

client = createClientForHandler(server.handleHttp);
client = createRestAppClient(app);
}
});
```
Expand Down
12 changes: 3 additions & 9 deletions examples/hello-world/test/acceptance/application.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,17 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {createClientForHandler, expect, supertest} from '@loopback/testlab';
import {RestServer} from '@loopback/rest';
import {Client, createRestAppClient, expect} from '@loopback/testlab';
import {HelloWorldApplication} from '../../src/application';

describe('Application', () => {
let app: HelloWorldApplication;
let client: supertest.SuperTest<supertest.Test>;
let server: RestServer;
let client: Client;

before(givenAnApplication);
before(async () => {
await app.start();
server = await app.getServer(RestServer);
});

before(() => {
client = createClientForHandler(server.requestHandler);
client = createRestAppClient(app);
});
after(async () => {
await app.stop();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {supertest} from '@loopback/testlab';
import {Client, createRestAppClient, expect} from '@loopback/testlab';
import {SoapCalculatorApplication} from '../../src/application';
import {expect} from '@loopback/testlab';

describe('Application', function() {
let app: SoapCalculatorApplication;
let client: supertest.SuperTest<supertest.Test>;
let client: Client;

// tslint:disable-next-line:no-invalid-this
this.timeout(30000);
Expand All @@ -14,7 +13,7 @@ describe('Application', function() {
before(async () => {
await app.boot();
await app.start();
client = supertest(app.restServer.url);
client = createRestAppClient(app);
});

after(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {createClientForHandler, expect, supertest} from '@loopback/testlab';
import {Client, createRestAppClient, expect} from '@loopback/testlab';
import {TodoListApplication} from '../../src/application';
import {Todo, TodoList} from '../../src/models/';
import {TodoRepository, TodoListRepository} from '../../src/repositories/';
import {TodoListRepository, TodoRepository} from '../../src/repositories/';
import {givenTodo, givenTodoList} from '../helpers';

describe('TodoListApplication', () => {
let app: TodoListApplication;
let client: supertest.SuperTest<supertest.Test>;
let client: Client;
let todoRepo: TodoRepository;
let todoListRepo: TodoListRepository;

Expand All @@ -23,7 +23,7 @@ describe('TodoListApplication', () => {
before(givenTodoRepository);
before(givenTodoListRepository);
before(() => {
client = createClientForHandler(app.requestHandler);
client = createRestAppClient(app);
});

beforeEach(async () => {
Expand Down
6 changes: 3 additions & 3 deletions examples/todo-list/test/acceptance/todo-list.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
// License text available at https://opensource.org/licenses/MIT

import {EntityNotFoundError} from '@loopback/repository';
import {createClientForHandler, expect, supertest} from '@loopback/testlab';
import {Client, createRestAppClient, expect} from '@loopback/testlab';
import {TodoListApplication} from '../../src/application';
import {TodoList} from '../../src/models/';
import {TodoListRepository} from '../../src/repositories/';
import {givenTodoList} from '../helpers';

describe('TodoListApplication', () => {
let app: TodoListApplication;
let client: supertest.SuperTest<supertest.Test>;
let client: Client;
let todoListRepo: TodoListRepository;

before(givenRunningApplicationWithCustomConfiguration);
after(() => app.stop());

before(givenTodoListRepository);
before(() => {
client = createClientForHandler(app.requestHandler);
client = createRestAppClient(app);
});

beforeEach(async () => {
Expand Down
6 changes: 3 additions & 3 deletions examples/todo-list/test/acceptance/todo.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
// License text available at https://opensource.org/licenses/MIT

import {EntityNotFoundError} from '@loopback/repository';
import {createClientForHandler, expect, supertest} from '@loopback/testlab';
import {Client, createRestAppClient, expect} from '@loopback/testlab';
import {TodoListApplication} from '../../src/application';
import {Todo} from '../../src/models/';
import {TodoRepository} from '../../src/repositories/';
import {givenTodo} from '../helpers';

describe('TodoListApplication', () => {
let app: TodoListApplication;
let client: supertest.SuperTest<supertest.Test>;
let client: Client;
let todoRepo: TodoRepository;

before(givenRunningApplicationWithCustomConfiguration);
after(() => app.stop());

before(givenTodoRepository);
before(() => {
client = createClientForHandler(app.requestHandler);
client = createRestAppClient(app);
});

beforeEach(async () => {
Expand Down
6 changes: 3 additions & 3 deletions examples/todo/test/acceptance/todo.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// License text available at https://opensource.org/licenses/MIT

import {EntityNotFoundError} from '@loopback/repository';
import {createClientForHandler, expect, supertest} from '@loopback/testlab';
import {Client, createRestAppClient, expect} from '@loopback/testlab';
import {TodoListApplication} from '../../src/application';
import {Todo} from '../../src/models/';
import {TodoRepository} from '../../src/repositories/';
Expand All @@ -18,7 +18,7 @@ import {

describe('TodoApplication', () => {
let app: TodoListApplication;
let client: supertest.SuperTest<supertest.Test>;
let client: Client;
let todoRepo: TodoRepository;

let cachingProxy: HttpCachingProxy;
Expand All @@ -30,7 +30,7 @@ describe('TodoApplication', () => {

before(givenTodoRepository);
before(() => {
client = createClientForHandler(app.requestHandler);
client = createRestAppClient(app);
});

beforeEach(async () => {
Expand Down
12 changes: 7 additions & 5 deletions packages/boot/test/acceptance/controller.booter.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Client, createClientForHandler, TestSandbox} from '@loopback/testlab';
import {RestServer} from '@loopback/rest';
import {createRestAppClient, TestSandbox} from '@loopback/testlab';
import {resolve} from 'path';
import {BooterApp} from '../fixtures/application';

Expand All @@ -22,8 +21,7 @@ describe('controller booter acceptance tests', () => {
await app.boot();
await app.start();

const server: RestServer = await app.getServer(RestServer);
const client: Client = createClientForHandler(server.requestHandler);
const client = createRestAppClient(app);

// Default Controllers = /controllers with .controller.js ending (nested = true);
await client.get('/one').expect(200, 'ControllerOne.one()');
Expand All @@ -38,7 +36,11 @@ describe('controller booter acceptance tests', () => {
);

const MyApp = require(resolve(SANDBOX_PATH, 'application.js')).BooterApp;
app = new MyApp();
app = new MyApp({
rest: {
port: 0,
},
});
}

async function stopApp() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import {createClientForHandler, supertest} from '@loopback/testlab';
import {RestServer} from '@loopback/rest';
import {Client, createRestAppClient} from '@loopback/testlab';
import {<%= project.applicationName %>} from '../..';

describe('PingController', () => {
let app: <%= project.applicationName %>;
let server: RestServer;
let client: supertest.SuperTest<supertest.Test>;
let client: Client;

before(givenAnApplication);

before(givenARestServer);

before(async () => {
await app.boot();
await app.start();
});

before(() => {
client = createClientForHandler(server.requestHandler);
client = createRestAppClient(app);
});

after(async () => {
Expand All @@ -35,8 +31,4 @@ describe('PingController', () => {
},
});
}

async function givenARestServer() {
server = await app.getServer(RestServer);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('Starting the application', () => {

it('starts an HTTP server (using RestApplication)', async () => {
const app = new RestApplication();
app.bind(RestBindings.PORT).to(0);
app.restServer.bind(RestBindings.PORT).to(0);
app.handler(sequenceHandler);
await startServerCheck(app);
});
Expand Down
10 changes: 5 additions & 5 deletions packages/rest/test/acceptance/coercion/coercion.acceptance.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {supertest, createClientForHandler, sinon} from '@loopback/testlab';
import {RestApplication, get, param} from '../../..';
import {Client, createRestAppClient, sinon} from '@loopback/testlab';
import {get, param, RestApplication} from '../../..';

describe('Coercion', () => {
let app: RestApplication;
let client: supertest.SuperTest<supertest.Test>;
let client: Client;
let spy: sinon.SinonSpy;

before(givenAClient);
Expand Down Expand Up @@ -91,9 +91,9 @@ describe('Coercion', () => {
});

async function givenAClient() {
app = new RestApplication();
app = new RestApplication({rest: {port: 0}});
app.controller(MyController);
await app.start();
client = createClientForHandler(app.requestHandler);
client = createRestAppClient(app);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@

import {ControllerClass} from '@loopback/core';
import {model, property} from '@loopback/repository';
import {createClientForHandler, supertest} from '@loopback/testlab';
import {Client, createRestAppClient} from '@loopback/testlab';
import {
RestApplication,
api,
getJsonSchema,
jsonToSchemaObject,
post,
requestBody,
RestApplication,
} from '../../..';
import {aBodySpec} from '../../helpers';

describe('Validation at REST level', () => {
let app: RestApplication;
let client: supertest.SuperTest<supertest.Test>;
let client: Client;

@model()
class Product {
Expand Down Expand Up @@ -152,10 +152,10 @@ describe('Validation at REST level', () => {
}

async function givenAnAppAndAClient(controller: ControllerClass) {
app = new RestApplication();
app = new RestApplication({rest: {port: 0}});
app.controller(controller);
await app.start();

client = createClientForHandler(app.requestHandler);
client = createRestAppClient(app);
}
});
Loading

0 comments on commit d46d91a

Please sign in to comment.