使用 supertest 启动和停止服务器
Start and stop server with supertest
我有以下服务器 class :
import express, { Request, Response } from 'express';
export default class Server {
server: any;
exp: any;
constructor() {
this.exp = express();
this.exp.get('/', (_req: Request, res: Response) => {
res.json('works');
});
}
start(): void {
this.server = this.exp.listen(3000);
}
stop(): void {
this.server.close();
}
}
我正在使用 supertest 进行端到端测试。我希望在所有测试之前启动我的应用程序并在测试完成后停止它。
使用 beforAll 和 afterAll 很容易做到这一点,我只需一次实例化服务器 class 并调用启动和关闭方法。
但是因为我有 10 多个控制器要测试,所以我想避免在每个测试文件期间启动和停止服务器。
我在文档中找到 setupFiles 和 setupFilesAfterEnv 但我无法停止服务器,因为实例不是 "shared"在两个文件中。
这是 1 个测试文件的示例:
import supertest from 'supertest';
describe('Album Test', () => {
let app: App;
beforeAll(async (done) => {
app = new App();
await app.setUp(); // database connection (not mentionned in the preivous example)
done();
});
afterAll(async (done) => {
await app.close();
app.server.stop();
done();
});
const api = supertest('http://localhost:3000');
it('Hello API Request', async () => {
const result = await api.get('/v1/user');
expect(result.status).toEqual(200);
...
});
});
这完全没问题,但我在每个测试文件中复制了 beforeAll 和 afterAll 方法。有没有办法只声明一次?
谢谢
您可以使用 setupFiles to set up test fixtures globally. You can assign variables that you want to use in multiple test files to Node.js global 对象。
例如
app.ts
:
import express, { Request, Response } from 'express';
export default class Server {
server: any;
exp: any;
constructor() {
this.exp = express();
this.exp.get('/', (_req: Request, res: Response) => {
res.json('works');
});
}
start(): void {
this.server = this.exp.listen(3000);
}
stop(): void {
this.server.close();
}
}
app.setup.js
:
const App = require('./app').default;
beforeAll(() => {
global.app = new App();
global.app.exp.set('test setup', 1);
console.log('app setup');
});
afterAll(() => {
console.log('app stop');
});
jest.config.js
:
module.exports = {
preset: 'ts-jest/presets/js-with-ts',
testEnvironment: 'node',
setupFilesAfterEnv: [
'./jest.setup.js',
'/Users/ldu020/workspace/github.com/mrdulin/react-apollo-graphql-starter-kit/Whosebug/61659975/app.setup.js',
],
testMatch: ['**/?(*.)+(spec|test).[jt]s?(x)'],
verbose: true,
};
a.controller.test.js
:
describe('controller a', () => {
it('should pass', () => {
console.log('test setup:', global.app.exp.get('test setup'));
expect(1 + 1).toBe(2);
});
});
b.controller.test.js
:
describe('controller b', () => {
it('should pass', () => {
console.log('test setup:', global.app.exp.get('test setup'));
expect(1 + 1).toBe(2);
});
});
单元测试结果:
PASS Whosebug/61659975/a.controller.test.js
controller a
✓ should pass (5ms)
console.log
app setup
at Object.<anonymous> (Whosebug/61659975/app.setup.js:6:11)
console.log
app setup
at Object.<anonymous> (Whosebug/61659975/app.setup.js:6:11)
console.log
test setup: 1
at Object.<anonymous> (Whosebug/61659975/b.controller.test.js:3:13)
console.log
test setup: 1
at Object.<anonymous> (Whosebug/61659975/a.controller.test.js:3:13)
console.log
app stop
at Object.<anonymous> (Whosebug/61659975/app.setup.js:10:11)
console.log
app stop
at Object.<anonymous> (Whosebug/61659975/app.setup.js:10:11)
PASS Whosebug/61659975/b.controller.test.js
controller b
✓ should pass (3ms)
Test Suites: 2 passed, 2 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 6.749s, estimated 12s
试一试它有效
const supertest = require('supertest')
const app = require('../../app')
describe('int::app', function(){
let request = null
let server = null
before(function(done){
server = app.listen(done)
request = supertest.agent(server)
})
after(function(done){
server.close(done)
})
it('should get /api/v1/laps/85299', function(){
return request.get('/api/v1/laps/85299')
.expect(200, { data: {} })
})
})
我有以下服务器 class :
import express, { Request, Response } from 'express';
export default class Server {
server: any;
exp: any;
constructor() {
this.exp = express();
this.exp.get('/', (_req: Request, res: Response) => {
res.json('works');
});
}
start(): void {
this.server = this.exp.listen(3000);
}
stop(): void {
this.server.close();
}
}
我正在使用 supertest 进行端到端测试。我希望在所有测试之前启动我的应用程序并在测试完成后停止它。
使用 beforAll 和 afterAll 很容易做到这一点,我只需一次实例化服务器 class 并调用启动和关闭方法。
但是因为我有 10 多个控制器要测试,所以我想避免在每个测试文件期间启动和停止服务器。
我在文档中找到 setupFiles 和 setupFilesAfterEnv 但我无法停止服务器,因为实例不是 "shared"在两个文件中。
这是 1 个测试文件的示例:
import supertest from 'supertest';
describe('Album Test', () => {
let app: App;
beforeAll(async (done) => {
app = new App();
await app.setUp(); // database connection (not mentionned in the preivous example)
done();
});
afterAll(async (done) => {
await app.close();
app.server.stop();
done();
});
const api = supertest('http://localhost:3000');
it('Hello API Request', async () => {
const result = await api.get('/v1/user');
expect(result.status).toEqual(200);
...
});
});
这完全没问题,但我在每个测试文件中复制了 beforeAll 和 afterAll 方法。有没有办法只声明一次?
谢谢
您可以使用 setupFiles to set up test fixtures globally. You can assign variables that you want to use in multiple test files to Node.js global 对象。
例如
app.ts
:
import express, { Request, Response } from 'express';
export default class Server {
server: any;
exp: any;
constructor() {
this.exp = express();
this.exp.get('/', (_req: Request, res: Response) => {
res.json('works');
});
}
start(): void {
this.server = this.exp.listen(3000);
}
stop(): void {
this.server.close();
}
}
app.setup.js
:
const App = require('./app').default;
beforeAll(() => {
global.app = new App();
global.app.exp.set('test setup', 1);
console.log('app setup');
});
afterAll(() => {
console.log('app stop');
});
jest.config.js
:
module.exports = {
preset: 'ts-jest/presets/js-with-ts',
testEnvironment: 'node',
setupFilesAfterEnv: [
'./jest.setup.js',
'/Users/ldu020/workspace/github.com/mrdulin/react-apollo-graphql-starter-kit/Whosebug/61659975/app.setup.js',
],
testMatch: ['**/?(*.)+(spec|test).[jt]s?(x)'],
verbose: true,
};
a.controller.test.js
:
describe('controller a', () => {
it('should pass', () => {
console.log('test setup:', global.app.exp.get('test setup'));
expect(1 + 1).toBe(2);
});
});
b.controller.test.js
:
describe('controller b', () => {
it('should pass', () => {
console.log('test setup:', global.app.exp.get('test setup'));
expect(1 + 1).toBe(2);
});
});
单元测试结果:
PASS Whosebug/61659975/a.controller.test.js
controller a
✓ should pass (5ms)
console.log
app setup
at Object.<anonymous> (Whosebug/61659975/app.setup.js:6:11)
console.log
app setup
at Object.<anonymous> (Whosebug/61659975/app.setup.js:6:11)
console.log
test setup: 1
at Object.<anonymous> (Whosebug/61659975/b.controller.test.js:3:13)
console.log
test setup: 1
at Object.<anonymous> (Whosebug/61659975/a.controller.test.js:3:13)
console.log
app stop
at Object.<anonymous> (Whosebug/61659975/app.setup.js:10:11)
console.log
app stop
at Object.<anonymous> (Whosebug/61659975/app.setup.js:10:11)
PASS Whosebug/61659975/b.controller.test.js
controller b
✓ should pass (3ms)
Test Suites: 2 passed, 2 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 6.749s, estimated 12s
试一试它有效
const supertest = require('supertest')
const app = require('../../app')
describe('int::app', function(){
let request = null
let server = null
before(function(done){
server = app.listen(done)
request = supertest.agent(server)
})
after(function(done){
server.close(done)
})
it('should get /api/v1/laps/85299', function(){
return request.get('/api/v1/laps/85299')
.expect(200, { data: {} })
})
})