Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit 2314488

Browse files
committed
Refactored existing test to use framework
1 parent 54e8778 commit 2314488

File tree

4 files changed

+63
-86
lines changed

4 files changed

+63
-86
lines changed

test/app_test.ts

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,54 @@
1-
import { web3Factory } from '@0x/dev-utils';
2-
import { Web3ProviderEngine } from '@0x/subproviders';
1+
import { APIOrder, PaginatedCollection } from '@0x/connect';
32
import * as HttpStatus from 'http-status-codes';
4-
import 'mocha';
53
import * as request from 'supertest';
64

7-
import { getAppAsync, getDefaultAppDependenciesAsync } from '../src/app';
8-
import * as config from '../src/config';
95
import { DEFAULT_PAGE, DEFAULT_PER_PAGE, SRA_PATH } from '../src/constants';
106

11-
import { setupDependenciesAsync, teardownDependenciesAsync } from './utils/deployment';
127
import { expect } from './utils/expect';
8+
import { TestCase, TestManager } from './utils/test_manager';
139

14-
let app: Express.Application;
10+
const API_HTTP_ADDRESS = 'http://localhost:3000';
1511

16-
let provider: Web3ProviderEngine;
12+
async function apiGetRequestAsync(url: string): Promise<request.Response> {
13+
return request(API_HTTP_ADDRESS).get(url);
14+
}
1715

18-
describe('app test', () => {
19-
before(async () => {
20-
const ganacheConfigs = {
21-
shouldUseInProcessGanache: false,
22-
shouldAllowUnlimitedContractSize: true,
23-
rpcUrl: config.ETHEREUM_RPC_URL,
24-
};
25-
provider = web3Factory.getRpcProvider(ganacheConfigs);
26-
await setupDependenciesAsync();
27-
const dependencies = await getDefaultAppDependenciesAsync(provider, config);
28-
// start the 0x-api app
29-
app = await getAppAsync({ ...dependencies }, config);
30-
});
31-
after(async () => {
32-
await teardownDependenciesAsync();
33-
// NOTE(jalextowle): The app should be torn down. I'm not going to worry
34-
// about it because this test will be refactored so this will be a non-issue.
35-
});
36-
it('should not be undefined', () => {
37-
expect(app).to.not.be.undefined();
38-
});
39-
it('should respond to GET /sra/orders', async () => {
40-
await request(app)
41-
.get(`${SRA_PATH}/orders`)
42-
.expect('Content-Type', /json/)
43-
.expect(HttpStatus.OK)
44-
.then(response => {
45-
expect(response.body.perPage).to.equal(DEFAULT_PER_PAGE);
46-
expect(response.body.page).to.equal(DEFAULT_PAGE);
47-
expect(response.body.total).to.equal(0);
48-
expect(response.body.records).to.deep.equal([]);
49-
});
50-
});
51-
});
16+
async function assertCorrectGetBodyAsync(
17+
expectedBody: PaginatedCollection<APIOrder>,
18+
actualResponse: request.Response,
19+
): Promise<boolean> {
20+
expect(actualResponse.type).to.match(/json/);
21+
expect(actualResponse.status).to.be.eq(HttpStatus.OK);
22+
expect(actualResponse.body).to.be.deep.eq(expectedBody);
23+
return true;
24+
}
25+
26+
const manager = new TestManager(
27+
{
28+
apiGetRequestAsync,
29+
},
30+
{
31+
assertCorrectGetBodyAsync,
32+
},
33+
);
34+
35+
const suite: TestCase[] = [
36+
{
37+
description: 'should respond to GET /sra/orders',
38+
action: {
39+
actionType: 'apiGetRequestAsync',
40+
input: `${SRA_PATH}/orders`,
41+
},
42+
assertion: {
43+
assertionType: 'assertCorrectGetBodyAsync',
44+
// This is the body of the expected HTTP request.
45+
input: {
46+
perPage: DEFAULT_PER_PAGE,
47+
page: DEFAULT_PAGE,
48+
total: 0,
49+
records: [],
50+
},
51+
},
52+
},
53+
];
54+
manager.executeTestSuite('app test', suite);

test/new_app_test.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

test/utils/deployment.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ export interface LoggingConfig {
2222

2323
/**
2424
* Sets up a 0x-api instance.
25-
* @param logConfig Whether or not the logs from the setup functions should
25+
* @param loggingConfig Whether or not the logs from the setup functions should
2626
* be printed.
2727
*/
28-
export async function setupApiAsync(logConfig: LoggingConfig = {}): Promise<void> {
28+
export async function setupApiAsync(loggingConfig: LoggingConfig = {}): Promise<void> {
2929
if (yarnStartProcess) {
3030
throw new Error('Old 0x-api instance has not been torn down');
3131
}
32-
await setupDependenciesAsync(logConfig.shouldPrintDependencyLogs || false);
32+
await setupDependenciesAsync(loggingConfig.shouldPrintDependencyLogs || false);
3333
yarnStartProcess = spawn('yarn', ['start'], {
3434
cwd: apiRootDir,
3535
});
36-
if (logConfig.shouldPrintApiLogs) {
36+
if (loggingConfig.shouldPrintApiLogs) {
3737
yarnStartProcess.stdout.on('data', chunk => {
3838
neatlyPrintChunk('[0x-api]', chunk);
3939
});
@@ -44,20 +44,20 @@ export async function setupApiAsync(logConfig: LoggingConfig = {}): Promise<void
4444
// Wait for the API to boot up
4545
// HACK(jalextowle): This should really be replaced by log-scraping, but it
4646
// does appear to work for now.
47-
await sleepAsync(2500); // tslint:disable-line:custom-no-magic-numbers
47+
await sleepAsync(5000); // tslint:disable-line:custom-no-magic-numbers
4848
}
4949

5050
/**
5151
* Tears down the old 0x-api instance.
52-
* @param logConfig Whether or not the logs from the teardown functions should
52+
* @param loggingConfig Whether or not the logs from the teardown functions should
5353
* be printed.
5454
*/
55-
export async function teardownApiAsync(logConfig: LoggingConfig = {}): Promise<void> {
55+
export async function teardownApiAsync(loggingConfig: LoggingConfig = {}): Promise<void> {
5656
if (!yarnStartProcess) {
5757
throw new Error('There is no 0x-api instance to tear down');
5858
}
5959
yarnStartProcess.kill();
60-
await teardownDependenciesAsync(logConfig.shouldPrintDependencyLogs || false);
60+
await teardownDependenciesAsync(loggingConfig.shouldPrintDependencyLogs || false);
6161
}
6262

6363
/**

test/utils/test_manager.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import 'mocha';
22

3-
import { setupApiAsync, teardownApiAsync } from './deployment';
3+
import { LoggingConfig, setupApiAsync, teardownApiAsync } from './deployment';
44
import { expect } from './expect';
55

66
// FIXME(jalextowle): Lock this down to only the actions that have been created.
77
export type ActionType = string;
88

99
// FIXME(jalextowle): Lock this down to only the assetions that have been created.
10+
// The TestManager should remain flexible, but not when it is
11+
// being used normally.
1012
export type AssertionType = string;
1113

1214
// tslint:disable-next-line:interface-over-type-literal
13-
export type ActionResult = {} | undefined;
15+
export type ActionResult = any;
1416

1517
export interface ActionInfo {
1618
actionType: ActionType;
@@ -20,7 +22,7 @@ export interface ActionInfo {
2022

2123
export interface AssertionInfo {
2224
assertionType: AssertionType;
23-
expectedResult: ActionResult;
25+
input: any;
2426
}
2527

2628
export interface TestCase {
@@ -30,12 +32,13 @@ export interface TestCase {
3032
}
3133

3234
type Action = (input: any) => Promise<any>;
33-
type Assertion = (expectedResult: ActionResult, actualResult: ActionResult) => boolean;
35+
type Assertion = (expectedResult: ActionResult, actualResult: ActionResult) => Promise<boolean>;
3436

3537
export class TestManager {
3638
constructor(
3739
protected _actionsAsync: Record<string, Action>,
3840
protected _assertionsAsync: Record<string, Assertion>,
41+
protected readonly _loggingConfig?: LoggingConfig,
3942
) {}
4043

4144
/**
@@ -56,12 +59,12 @@ export class TestManager {
5659
describe(description, () => {
5760
beforeEach(async () => {
5861
// Setup the 0x-api instance.
59-
await setupApiAsync();
62+
await setupApiAsync(this._loggingConfig);
6063
});
6164

6265
afterEach(async () => {
6366
// Teardown the 0x-api instance.
64-
await teardownApiAsync();
67+
await teardownApiAsync(this._loggingConfig);
6568
});
6669

6770
for (const testCase of testSuite) {
@@ -91,6 +94,6 @@ export class TestManager {
9194
if (!assertionAsync) {
9295
throw new Error('[test-manager] assertion is not registered');
9396
}
94-
return assertionAsync(assertion.expectedResult, actualResult);
97+
return assertionAsync(assertion.input, actualResult);
9598
}
9699
}

0 commit comments

Comments
 (0)