私は自分のライブラリの受け入れテストを作成しました。ライブラリが公開されると、まだアクセスできない外部APIサービスが使用されます。だから私はテストするために必要なJSONを返す擬似JSON APIサーバを作成しました。テストをパスするために必要なことは、要求が行われたファイルでAPI URLを手動で変更することです。ノードJS受け入れテスト用の開発API
受け入れテストを実行するときに模擬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);
});
}
});
は、「http://liveURL.com/api」、私は「http://localhost:3000/api」への要求を取得していますURLを変更です。
私が使用していますモックサーバは、ここで見つけることができます:https://github.com/typicode/json-server
EDIT:ベンズ質問への回答では、ここで私が
{
"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"
}
}
I NODE_ENVを設定しようとしています私のpackage.jsonでありますworld.jsに私の2台のサーバを作成しました。その上でライブラリを実行するための1台のサーバがポート23661上にあり、偽のAPIがどの上にある1台のサーバがこれは
'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();
これが実行されると、それはで正しく受け入れテストを通る下に見ることができる3000ポートですFirefoxのブラウザ。
私は
process.env.NODE_ENV
を呼び出すしようとしているファイルには、私の最小化ライブラリコールmyLib.min.jsです。ここで
これは、一般的にenvironment variablesの使用によって解決されるunminifiedファイル
'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);
});
}
});
};
おかげで、だから私は「受け入れテストを実行NPM」コマンドとworld.jsで私のテストサーバーを始めていますので、私は設定どこにworld.jsで次のようになります。|テスト|開発一般的な値は、生産されていますNODE_ENV? –
いいえ、あなたは 'NODE_ENV = test npm run acceptance-test'を実行します。 – Ben
私は' 'acceptance-test ''を持っています:" NODE_ENV = development cucumbers "'を私のpackage.jsonに入れてください。 'npm run acceptance-test'を実行すると、' NODE_ENV'が 'development 'に設定されたテストが実行されます。しかし、あなたが指定したif文の前に 'console.log(process.env.NODE_ENV)'を自分のコードに追加すると、 'undefined'と表示されます。 –