Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(testlab): add createRestAppClient(), simplify usage in tests #1734

Merged
merged 1 commit into from
Sep 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
21 changes: 10 additions & 11 deletions examples/hello-world/test/acceptance/application.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
// 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,
givenHttpServerConfig,
} 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 All @@ -32,9 +31,9 @@ describe('Application', () => {

function givenAnApplication() {
app = new HelloWorldApplication({
rest: {
rest: givenHttpServerConfig({
port: 0,
},
}),
disableConsoleLog: true,
});
}
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
17 changes: 11 additions & 6 deletions examples/todo-list/test/acceptance/todo-list-todo.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
// 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,
givenHttpServerConfig,
} 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 +28,7 @@ describe('TodoListApplication', () => {
before(givenTodoRepository);
before(givenTodoListRepository);
before(() => {
client = createClientForHandler(app.requestHandler);
client = createRestAppClient(app);
});

beforeEach(async () => {
Expand Down Expand Up @@ -119,9 +124,9 @@ describe('TodoListApplication', () => {

async function givenRunningApplicationWithCustomConfiguration() {
app = new TodoListApplication({
rest: {
rest: givenHttpServerConfig({
port: 0,
},
}),
});

await app.boot();
Expand Down
15 changes: 10 additions & 5 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,28 @@
// 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,
givenHttpServerConfig,
} 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 Expand Up @@ -140,9 +145,9 @@ describe('TodoListApplication', () => {

async function givenRunningApplicationWithCustomConfiguration() {
app = new TodoListApplication({
rest: {
rest: givenHttpServerConfig({
port: 0,
},
}),
});

await app.boot();
Expand Down
15 changes: 10 additions & 5 deletions examples/todo-list/test/acceptance/todo.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,28 @@
// 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,
givenHttpServerConfig,
} 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 Expand Up @@ -138,9 +143,9 @@ describe('TodoListApplication', () => {

async function givenRunningApplicationWithCustomConfiguration() {
app = new TodoListApplication({
rest: {
rest: givenHttpServerConfig({
port: 0,
},
}),
});

await app.boot();
Expand Down
15 changes: 10 additions & 5 deletions examples/todo/test/acceptance/todo.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
// 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,
givenHttpServerConfig,
} from '@loopback/testlab';
import {TodoListApplication} from '../../src/application';
import {Todo} from '../../src/models/';
import {TodoRepository} from '../../src/repositories/';
Expand All @@ -18,7 +23,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 +35,7 @@ describe('TodoApplication', () => {

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

beforeEach(async () => {
Expand Down Expand Up @@ -170,9 +175,9 @@ describe('TodoApplication', () => {

async function givenRunningApplicationWithCustomConfiguration() {
app = new TodoListApplication({
rest: {
rest: givenHttpServerConfig({
port: 0,
},
}),
});

await app.boot();
Expand Down
14 changes: 9 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,11 @@
// 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,
givenHttpServerConfig,
TestSandbox,
} from '@loopback/testlab';
import {resolve} from 'path';
import {BooterApp} from '../fixtures/application';

Expand All @@ -22,8 +25,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 +40,9 @@ describe('controller booter acceptance tests', () => {
);

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

async function stopApp() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import {createClientForHandler, supertest} from '@loopback/testlab';
import {RestServer} from '@loopback/rest';
import {
Client,
createRestAppClient,
givenHttpServerConfig,
} 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 @@ -30,13 +30,9 @@ describe('PingController', () => {

function givenAnApplication() {
app = new <%= project.applicationName %>({
rest: {
rest: givenHttpServerConfig({
port: 0,
},
}),
});
}

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
Loading