2016-11-23 1 views
0

내 라이브러리에 대한 승인 테스트를 만들었습니다. 라이브러리가 게시되면 아직 액세스 할 수없는 외부 API 서비스가 사용됩니다. 그래서 테스트 할 필수 JSON을 반환하는 mock 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

편집 : BENS 질문에 대한 답변에서, 여기에 내가

{ 
    "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에 두 대의 서버를 만들었습니다. 가짜 API가있는 그것이 제대로 수용 테스트를 통해 실행이 실행되면이

'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 포트입니다 하나 개의 서버는 포트 23661 하나 개의 서버에있는 라이브러리를 실행합니다 파이어 폭스 브라우저.

내가

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); 
      }); 
     } 
     }); 
}; 

답변

1

의 일부이다. 같은

뭔가 :

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 
+0

감사합니다, 그래서 '수용 테스트를 실행 NPM'명령으로 world.js에 내 테스트 서버를 시작하고, 내가 설정 한 곳은 world.js에있을 것입니다 : | 테스트 | 개발 일반적인 값은 생산이다 NODE_ENV? –

+0

아니요,'NODE_ENV = test npm run acceptance-test'를 실행하면됩니다. – Ben

+0

'acceptance-test'가 있습니다 : package.json에 "NODE_ENV = development cucumbers"가 있습니다. 이제'npm run acceptance-test'를 실행하면'NODE_ENV'가 'development'로 설정된 상태에서 테스트가 실행됩니다. 그러나 당신이 제공 한 if 문 앞에'console.log (process.env.NODE_ENV)'를 추가하면'undefined'가됩니다. –