Skip to content

Commit

Permalink
Refactor test setup
Browse files Browse the repository at this point in the history
* Use shared global type definitions

* Fix non-relative pathing for imports

* Integrate test double library sinon as a global

* Rework expect to make it a global

* Integrate mocha-gherkin across all packages

* Rewrite loopback black-box test using mocha-gherkin syntax

* Remove unused .eslintignore in package loopback

* Simply bootstrapping tests

* Fix Application constructor and start methods
  • Loading branch information
simonhoibm authored and ritch committed Jan 27, 2017
1 parent 2a4d263 commit 1808d9f
Show file tree
Hide file tree
Showing 35 changed files with 224 additions and 116 deletions.
21 changes: 21 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
// Use IntelliSense to learn about possible Node.js debug attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceRoot}/app.js",
"cwd": "${workspaceRoot}"
},
{
"type": "node",
"request": "attach",
"name": "Attach to Process",
"port": 5858
}
]
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "./node_modules/typescript/lib"
}
48 changes: 48 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "lerna",
"isShellCommand": true,
"echo command": true,
"showOutput": "always",
"suppressTaskName": true,
"tasks": [
// Installing dependencies
{
"taskName": "Install dependencies",
"args": ["exec", "npm", "install"]
},
{
"taskName": "Install loopback dependencies",
"args": ["exec", "npm", "install", "--scope", "loopback"]
},
{
"taskName": "Install juggler dependencies",
"args": ["exec", "npm", "install", "--scope", "@loopback/juggler"]
},
{
"taskName": "Install remoting dependencies",
"args": ["exec", "npm", "install", "--scope", "@loopback/remoting"]
},
// Running tests
{
// run all tests for every package
"taskName": "test", // DO NOT CHANGE (used for vscode ctrl+shift+t keybinding)
"args": ["run", "test"],
"isTestCommand": true
},
{
"taskName": "Run loopback tests",
"args": ["run", "test", "--scope", "loopback"]
},
{
"taskName": "Run juggler tests",
"args": ["run", "test", "--scope", "@loopback/juggler"]
},
{
"taskName": "Run remoting tests",
"args": ["run", "test", "--scope", "@loopback/remoting"]
}
]
}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
{
"license": "MIT",
"devDependencies": {
"@types/mocha": "^2.2.38",
"@types/node": "^7.0.4",
"chai": "^3.5.0",
"dirty-chai": "^1.2.2",
"lerna": "^2.0.0-beta.32",
"request": "^2.79.0",
"request-promise": "^4.1.1",
"sinon": "^1.17.7",
"ts-node": "^2.0.0",
"tsconfig-paths": "^2.1.0",
"typescript": "^2.1.5"
},
"dependencies": {
Expand Down
5 changes: 3 additions & 2 deletions packages/juggler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
"main": "index.js",
"scripts": {
"black-box": "mocha --opts ../../test/mocha.opts.black-box",
"test": "npm run black-box && npm run white-box",
"test": "npm run white-box && npm run black-box",
"white-box": "mocha --opts ../../test/mocha.opts.white-box"
},
"author": "IBM",
"license": "MIT",
"devDependencies": {
"mocha": "^3.2.0"
"mocha": "^3.2.0",
"mocha-gherkin": "^0.2.0"
}
}
8 changes: 3 additions & 5 deletions packages/juggler/test/black-box/smoke.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {expect} from '../../../../test/expect';

suite('black-box smoke test', () => {
test('passes', () => {
describe('white-box smoke test', () => {
it('passes', () => {
expect(true).to.be.true();
});
});
});
8 changes: 3 additions & 5 deletions packages/juggler/test/white-box/smoke.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {expect} from '../../../../test/expect';

describe('white-box smoke test', () => {
it('passes', () => {
suite('white-box smoke test', () => {
test('passes', () => {
expect(true).to.be.true();
});
});
});
2 changes: 0 additions & 2 deletions packages/loopback/.eslintignore

This file was deleted.

3 changes: 1 addition & 2 deletions packages/loopback/.travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ language: node_js
node_js:
- "4"
- "6"
- "7"

- "7"
5 changes: 5 additions & 0 deletions packages/loopback/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export {
Application,
AppConfig,
AppState
} from './lib/application';
26 changes: 19 additions & 7 deletions packages/loopback/lib/application.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
import http = require('http');

import bluebird = require('bluebird');

export interface AppConfig {
port : number;
}

export enum AppState {
cold,
starting,
listening,
crashed,
stopped
}

export class Application {
constructor(public config : AppConfig) {
// get runtime to enforce AppConfig as AppConfig
constructor(public config?: AppConfig) {
if (config === undefined) {
this.config = {port: 3000};
}
}
public start() : Promise<void> {
let server = http.createServer((req, res) => {

public state: AppState = AppState.cold;

async start() {
this.state = AppState.starting;
const server = http.createServer((req, res) => {
res.end();
});
let listen = bluebird.promisify(server.listen, {context: server});

return listen(this.config.port);
const listen = bluebird.promisify(server.listen, {context: server});
await listen(this.config.port);
this.state = AppState.listening;
}
}
1 change: 0 additions & 1 deletion packages/loopback/loopback.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/loopback/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"author": "IBM",
"license": "MIT",
"devDependencies": {
"@types/mocha": "^2.2.38",
"mocha": "^3.2.0"
"mocha": "^3.2.0",
"mocha-gherkin": "^0.2.0"
}
}
25 changes: 25 additions & 0 deletions packages/loopback/test/black-box/bootstrapping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as util from 'loopback/test/support/util';

Feature('Bootstrapping',
'In order to serve up my API',
'As a user',
'I want to start the app', () => {
Scenario('with default configs', () => {
let app;
let client;

Given('an app', () => {
app = util.createApp();
});
And('a client', () => {
client = util.createClient(app);
});
When('the app is started (on port 3000 by default)', async () => {
await app.start();
});
Then('the app responds with HTTP 200 when a request is sent to GET /', async () => {
const result = await client.get('/')
expect(result.statusCode).to.equal(200);
});
});
});
6 changes: 0 additions & 6 deletions packages/loopback/test/black-box/bootstrapping/features.md

This file was deleted.

17 changes: 0 additions & 17 deletions packages/loopback/test/black-box/bootstrapping/index.ts

This file was deleted.

6 changes: 3 additions & 3 deletions packages/loopback/test/support/client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Application, AppConfig } from '../../loopback';
import { Application, AppConfig } from 'loopback';
import bluebird = require('bluebird');
import request = require('request-promise');

export class Client {
constructor(public app : Application) {

}

public get(path : string) : Promise<Result>{
Expand All @@ -17,7 +17,7 @@ export class Client {
return request(options)
.then((response) => {
return {
status: response.statusCode
statusCode: response.statusCode
};
});
}
Expand Down
6 changes: 0 additions & 6 deletions packages/loopback/test/support/container.ts

This file was deleted.

7 changes: 0 additions & 7 deletions packages/loopback/test/support/expect.ts

This file was deleted.

8 changes: 0 additions & 8 deletions packages/loopback/test/support/scenarios.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/loopback/test/support/support.d.ts

This file was deleted.

18 changes: 6 additions & 12 deletions packages/loopback/test/support/util.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { Application, AppConfig } from '../../loopback';
import { Application, AppConfig } from 'loopback';
import { Client } from './client';


class Util {
public createApp(config: AppConfig) : Application {
return new Application(config);
}
public createClient(app : Application) {
return new Client(app);
}
export function createApp(config?: AppConfig) : Application {
return new Application(config);
}



export default new Util;
export function createClient(app : Application) {
return new Client(app);
}
20 changes: 20 additions & 0 deletions packages/loopback/test/white-box/application.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {Application, AppState} from 'loopback';

suite('Application', () => {
suite('constructor(config?: AppConfig)', () => {
test('without config arg', () => {
const app = new Application();
expect(app.config).to.be.an('object');
expect(app.config.port).to.eql(3000);
});
})

suite('start()', () => {
test('when state is cold', async () => {
const app = new Application();
expect(app.state).to.equal(AppState.cold);
await app.start();
expect(app.state).to.equal(AppState.listening);
});
});
});
7 changes: 0 additions & 7 deletions packages/loopback/test/white-box/smoke.ts

This file was deleted.

5 changes: 3 additions & 2 deletions packages/remoting/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
"main": "index.js",
"scripts": {
"black-box": "mocha --opts ../../test/mocha.opts.black-box",
"test": "npm run black-box && npm run white-box",
"test": "npm run white-box && npm run black-box",
"white-box": "mocha --opts ../../test/mocha.opts.white-box"
},
"author": "IBM",
"license": "MIT",
"devDependencies": {
"mocha": "^3.2.0"
"mocha": "^3.2.0",
"mocha-gherkin": "^0.2.0"
}
}
8 changes: 3 additions & 5 deletions packages/remoting/test/black-box/smoke.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {expect} from '../../../../test/expect';

suite('black-box smoke test', () => {
test('passes', () => {
describe('black-box smoke test', () => {
it('passes', () => {
expect(true).to.be.true();
});
});
});
Loading

0 comments on commit 1808d9f

Please sign in to comment.