node.js - grunt mocha 未找到目标错误

标签 node.js gruntjs mocha.js chai

我正在尝试使用 grunt、mocha 和 chai 为 Node api 编写测试。我不知道如何需要其他库。我是 grunt、mocha、chai 和一般测试的初学者......

我的 Grunt 文件:

// Gruntfile.js
module.exports = function(grunt){
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),

    // Mocha Test
    mochaTest: {
          test: {
            options: {
              reporter: 'list',
              timeout: 15000
            },
            src: ['test/groupstest.js']
          }
        }
  });

  // Load grunt mocha task
  grunt.loadNpmTasks('grunt-mocha');
  grunt.loadNpmTasks('grunt-mocha-test');
};

文件结构:

├── package.json
├── Gruntfile.js
├── test
│   └── groupstest.js
└── ...

分组表:

'use strict';

var request = require('supertest');
var expect = require("chai").expect;

var app = require('../middleware/express');

describe('Routes', function(){
  describe('/groups - GET', function(){
  it('- should GET users', function(done){
    console.log(request(app).get);
    request(app)
      .get('/groups', function(err, res, body){

        ...

        done();
      });
  });
});

我正在模仿 app.js 文件中的 Express 应用程序。然后我正在测试 GET 路由,但它说“require”未定义。我知道如何解决这个问题吗?我感觉我真的很亲近。

<小时/>

更新: 所以我输入 grunt mochaTest 来测试它。问题是它超时了,我不明白为什么。

这是错误:

    Routes /groups - GET - should GET users: [Function]
  1) Routes /groups - GET - should GET users

  0 passing (15s)
  1 failing

  1) Routes /groups - GET - should GET users:
     Error: timeout of 15000ms exceeded. Ensure the done() callback is being called in this test.




Warning: Task "mochaTest:test" failed. Use --force to continue.

Aborted due to warnings.

最佳答案

您的问题与您使用 super 测试的方式有关。请参阅下面的代码片段作为示例。

it('- should GET users', function(done){
  request(app)
    .get('/groups')
    // you can add supertest expectations here
    .end(function(err, res){

      ...

      done();
    });
});

关于node.js - grunt mocha 未找到目标错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30431443/

相关文章:

node.js - 如何将 Promise 应用于 app.get?

node.js - AWS Lambda 函数不接受 Twilio 模块

node.js - 如何向 LAN 上的 Node.JS 服务器发送广播请求?

node.js - 如何避免 Mocha 测试用例超时?

node.js - 如何将文件上传到 Bluemix 中的对象存储(通过 nodejs)

javascript - grunt.js 配置中常用配置选项的继承

javascript - 我是否可以让父 Gruntfile.js 与子 Gruntfile.js 交互或运行任务?

javascript - Zombie.js pressButton 长回调延迟

javascript - AngularJS TypeScript 单元测试

git - 当我将我的项目发布到 GitHub 时,我应该如何处理 Grunt 的 node_modules 目录?