【问题标题】:Second Mocha test not running第二次摩卡测试没有运行
【发布时间】:2021-02-15 14:31:27
【问题描述】:

我在 Windows 操作系统中设置了一个 Express API,其中包含几个测试,第一个测试在 wrongo/test/app_test.js 内成功运行:

const assert = require('assert');
const request = require('supertest');
const app = require('../app');

describe('the express app', () => {
  it('handles a GET request to /api', done => {
   request(app).get('/api').end((err, response) => {
     assert(response.body.how === 'dee');
     done();
   });
  });
});

我后来在wrongo/test/controllers/users_controller_test.js开发了另一个测试:

const assert = require('assert');
const request = require('supertest');
const app = require('../../app');

describe('Users controller', () => {
  it('Post to /api/users creates a new user', done => {
   request(app).post('/api/users').send({ email: 'test@test.com' }).end(() => {
     done();
   });
  });
});

它根本不运行第二个测试,我没有从users_controller.js 文件中获取我的控制台日志:

module.exports = {
  greeting(req, res) {
    res.send({ how: 'dee' });
  },

  create(req, res) {
    console.log(req.body);
    res.send({ how: 'dee' });
  }
};

我不相信我有任何语法错误,不知道为什么 Mocha 不会只运行第二个测试。

【问题讨论】:

    标签: node.js express mocha.js supertest


    【解决方案1】:

    如果将 Mocha 嵌套在 test/ 文件夹内的文件夹中,似乎找不到测试,但是当我将测试文件移动到主 test/ 文件夹时,Mocha 能够运行测试。原因是,如果您查看package.json 内部的"scripts" 对象,则其中缺少--recursive

    "test": "nodemon --exec mocha -R min"

    但我不得不以这种方式编写测试脚本,因为当我收到 --recursive 时,Windows 10 一直给我一个错误。

    【讨论】:

      猜你喜欢
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      • 1970-01-01
      相关资源
      最近更新 更多