分成不同文件时测试失败

时间:2016-04-08 14:39:32

标签: javascript node.js unit-testing mocha

目前,我在同一个文件中ServiceAnotherService都有测试套件,service.test.js

当我按npm test运行时,ServiceAnotherService的测试运行正常并通过。

但如果我将AnotherService的测试套件移至anotherservice.test.js,则service.test.js的测试失败,这很奇怪。

任何人都知道为什么?

来自package.json ..

的摘录
  "scripts": {
    "start": "node app.js",
    "test": "mocha && jshint .",

service.test.js内的代码..

// this works fine
describe('Service', function() {
  describe('delete()', function() {
    it('should respond with a 401 as we are supplying user and no password', function(done) {
      request(app).delete('/item/0033cb8e-86a4-4dc1-9c2e-a07ca226d43f/')
        .set('x-header-db', 'db')
        .set('x-header-db-host', 'host')
        .set('x-header-db-credname', 'uname')
        .set('x-header-db-credpass', '')
        .expect(401)
        .end(done);
    });
  });
});

// this works here, but if moved to another file the test above fails
describe('AnotherService', function() {
  describe('delete()', function() {
    it('should respond with a 401 as we are supplying user and no password', function(done) {
      request(app).delete('/item/0033cb8e-86a4-4dc1-9c2e-a07ca226d43f/')
        .set('x-header-db', 'db')
        .set('x-header-db-host', 'host')
        .set('x-header-db-credname', 'uname')
        .set('x-header-db-credpass', '')
        .expect(401)
        .end(done);
    });
  });
});

目录列表的片段

├── package.json
├── README.md
├── src
│   ├── serivce.js
│   └── anotherservice.js
└── test
    ├── service.test.js
    └── anotherservice.test.js

2 个答案:

答案 0 :(得分:0)

您可以编辑package.json文件并运行:

"scripts": {
"start": "node app.js",
"test": "mocha && jshint ./*.test.js" //this will execute all the files with suffix test.js

答案 1 :(得分:0)

单纯地去......

"scripts": {
    "start": "node app.js",
    "test": "mocha ./service.test.js && mocha ./anotherservice.test.js && jshint .",