当分离到不同的文件中时,测试失败

Test fails when separated into different files

本文关键字:测试 失败 文件 分离      更新时间:2024-03-23

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

当我按原样运行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

您可以编辑您的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 .",