下面是一个示例,说明如何仅在 e2e 测试运行时自动启动和停止单独的节点服务器。包含一个简单的快速模拟服务器脚本作为示例 API。
量角器.conf.js
const {SpecReporter} = require('jasmine-spec-reporter');
const forever = require('forever-monitor');
const child = new (forever.Monitor)('index.js', {
max: 10,
silent: false,
args: ["--port", "3001"],
sourceDir: 'mock-server'
});
let startResolve;
let stopResolve;
const startPromise = new Promise((resolve) => startResolve = resolve);
const stopPromise = new Promise((resolve) => stopResolve = resolve);
child.on('start', function () {
console.info('Forever started mocks.');
startResolve();
});
child.on('restart', function () {
console.info('Forever restarting mocks for ' + child.times + ' time');
});
child.on('exit:code', function (code) {
if (code) {
console.info('Forever exit mocks with code ' + code);
} else {
console.info('Forever exited mocks.');
}
stopResolve();
});
exports.config = {
allScriptsTimeout: 11000,
specs: [
'./e2e/**/*.e2e-spec.ts'
],
capabilities: {
'browserName': 'chrome'
},
directConnect: true,
baseUrl: 'http://localhost:4200/',
framework: 'jasmine',
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 30000,
print: function () {
}
},
beforeLaunch: function () {
child.start();
require('ts-node').register({
project: 'e2e/tsconfig.e2e.json'
});
return startPromise;
},
onPrepare() {
jasmine.getEnv().addReporter(new SpecReporter({spec: {displayStacktrace: true}}));
},
onCleanUp() {
child.stop();
return stopPromise;
}
};
模拟服务器/index.js
// npm install --save express
// npm install --save body-parser
// npm install --save minimist
const express = require('express');
const bodyParser = require('body-parser');
const minimist = require('minimist');
const API_DELAY = 0;
const app = express();
app.use(bodyParser.json({limit: '50mb'}));
// Turn on CORS for browser testing.
app.use(function (req, res, next) {
let accessHeaderInReq = false;
if (req.headers.origin) {
res.header('Access-Control-Allow-Origin', req.headers.origin);
accessHeaderInReq = true;
}
if (req.headers['access-control-request-method']) {
res.header('Access-Control-Allow-Methods', req.headers['access-control-request-method']);
accessHeaderInReq = true;
}
if (req.headers['access-control-request-headers']) {
res.header('Access-Control-Allow-Headers', req.headers['access-control-request-headers']);
accessHeaderInReq = true;
}
if (accessHeaderInReq) {
res.header('Access-Control-Max-Age', 60 * 60 * 24 * 365);
}
// Intercept OPTIONS method for angular preflight checks.
if (accessHeaderInReq && req.method === 'OPTIONS') {
return res.sendStatus(200);
}
else {
next();
}
});
app.get('/api/foo', function (req, res, next) {
console.info('GET - returning foo', req.body);
setTimeout(() => {
res.json({
foo: "bar"
});
}, API_DELAY);
});
const argv = minimist(process.argv.slice(2));
const port = argv.port || 3000;
console.log("Starting express on port", port);
app.listen(port);
对于持续集成环境,您可以安装模拟服务器 node_modules 而无需更改目录,如下所示:
npm --prefix ./mock-server install ./mock-server