我已经为我的库创建了验收测试。当库发布时,它将使用尚未访问的外部 API 服务。所以我创建了一个模拟 json api 服务器,它返回测试所需的 JSON。为了让测试通过,我需要做的是在发出请求的文件中手动更改 API url。
我想知道是否有一种方法可以在运行验收测试时使用模拟 API url,并在不运行验收测试时恢复到实时 URL。下面是带有实时 URL 的代码片段。
return fetch('http://liveURL.com/api')
.then(response => {
if (response.status === 200) {
return response.json()
.then(myResponse => {
var theResponse = myResponse.id;
return theResponse;
});
} else {
return response.json().then(error => {
throw new Error(error.error);
});
}
});
仅在运行验收测试时我想做的是更改 url,' http://liveURL.com/api ',我收到来自 ' http://localhost:3000/api 的请求'.
我正在使用的模拟服务器可以在这里找到:https://github.com/typicode/json-server
编辑:为了回答 Bens 的问题,这是我的 package.json,我试图在其中设置 NODE_ENV
{
"name": "my-lib",
"version": "0.1.0",
"description": "",
"main": "lib/myLib",
"private": true,
"scripts": {
"lint": "gulp lint",
"pretest": "npm run lint",
"test": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter spec \"test/unit/**/*.spec.js\"",
"acceptance-test": "NODE_ENV=test cucumberjs",
"build": "gulp build"
},
"author": "Me",
"license": "ISC",
"devDependencies": {
"babel-preset-es2015": "^6.16.0",
"babel-register": "^6.16.3",
"babelify": "^7.3.0",
"browserify": "^13.1.0",
"chai": "^3.5.0",
"chai-as-promised": "^5.3.0",
"config-browserify": "^1.0.5",
"cors": "^2.8.1",
"cucumber": "^0.10.3",
"eslint": "^3.0.1",
"eslint-teamcity": "^1.1.0",
"express": "^4.14.0",
"gulp": "^3.9.1",
"gulp-eslint": "^3.0.1",
"gulp-sourcemaps": "^1.6.0",
"gulp-uglify": "^2.0.0",
"isomorphic-fetch": "^2.2.1",
"istanbul": "v1.1.0-alpha.1",
"jsdom": "^9.8.3",
"json-server": "^0.9.1",
"mocha": "^2.5.3",
"mocha-jsdom": "^1.1.0",
"mocha-teamcity-reporter": ">=0.0.1",
"nock": "^9.0.0",
"node-localstorage": "^1.3.0",
"portscanner": "^2.1.0",
"proxyquire": "^1.7.10",
"selenium-server": "^2.53.0",
"selenium-webdriver": "^2.53.2",
"semver": "^5.3.0",
"serve-static": "^1.11.1",
"sinon": "^1.17.4",
"sinon-chai": "^2.8.0",
"vinyl-buffer": "^1.0.0",
"vinyl-source-stream": "^1.1.0",
"watchify": "^3.7.0"
},
"dependencies": {
"config": "^1.21.0"
}
}
我在 world.js 中创建了两个服务器。一台运行该库的服务器位于端口 23661 上,另一台服务器上的假 API 位于端口 3000 上。这可以在下面看到
'use strict';
const SeleniumServer = require('selenium-webdriver/remote').SeleniumServer;
const webdriver = require('selenium-webdriver');
const server = new SeleniumServer(require('selenium-server').path, {port: 4444});
const serveStatic = require('serve-static');
const express = require('express');
const chaiAsPromised = require('chai-as-promised');
const chai = require('chai');
const jsonServer = require('json-server');
const cors = require('cors');
chai.should();
chai.use(chaiAsPromised);
const testServer = jsonServer.create();
const router = jsonServer.router('db.json');
testServer.use(cors());
testServer.options('*', cors());
testServer.use(router);
testServer.listen(3000);
const app = express();
const httpServerPort = 23661;
app.use(cors());
app.options('*', cors());
app.use(serveStatic('dist'));
app.use(serveStatic(__dirname + '/../page'));
app.use(serveStatic('node_modules/sinon/pkg'));
app.listen(httpServerPort);
server.start();
运行时,它会在 Firefox 浏览器中正确运行验收测试。
我尝试调用的文件
process.env.NODE_ENV
位于我的最小化库中,调用 myLib.min.js。
这是未压缩文件的一部分
'use strict';
const fetch = require('isomorphic-fetch');
module.exports = id => {
var apiUrl = 'http://liveurl.com/api/' + id;
if (process.env.NODE_ENV === 'development') {
apiUrl = 'http://localhost:3000/api';
}
console.log(process.env.NODE_ENV);
return fetch(apiUrl)
.then(response => {
if (response.status === 200) {
return response.json()
.then(myResponse => {
var theResponse = myResponse.id;
return theResponse;
});
} else {
return response.json().then(error => {
throw new Error(error.error);
});
}
});
};
最佳答案
这通常可以通过使用 environment variables 来解决.
类似于:
var apiUrl = 'http://localhost:3000/api'
if (process.env.NODE_ENV === 'production') {
apiUrl = 'http://liveurl.com/api';
}
return fetch(apiUrl)
.then(response => {
//... and so on
然后您将像这样启动您的应用程序(或运行您的测试),为 NODE_ENV 填写适当的环境名称。常见的值为生产|测试|开发:
NODE_ENV=production node ./path/to/app.js
关于javascript - Node JS 验收测试的开发 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40774291/