分成不同文件时测试失败

Test fails when separated into different files

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

当我 运行 npm test 时,按原样对 ServiceAnotherService 运行 进行测试并通过。

但是如果我将 AnotherService 的测试套件移动到 anotherservice.test.jsservice.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

您可以编辑 package.json 文件和 运行 这个 :

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

简单地去了..

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