javascript - 使用 Express 和 Grunt 进行 Mocha 测试

标签 javascript node.js mocha.js

我有一个项目,我的 Gruntfile 设置了 Express 服务器。现在我想开始使用 Mocha 进行测试,但它不断返回相同的错误。我真的不知道如何解决这个问题。

Gruntfile.js

// Express Config
    express: {
        options: {
          // Override defaults here
        },
        dev: {
            options: {
                script: 'app.js'
            }
        },
        test: {
            options: {
                port: 3001,
                script: 'test/testapp.js'
            }
        }
    },

    mocha: {
        all: {
            src: ['test/views/*.html'],
            options: {
                run: true
            }
        }
    },

    // Open Config
    open: {
        dev: {
            path: 'http://localhost:3000'
        },
        test: {
            path: 'http://localhost:3001'
        }
    },

//Test
grunt.registerTask('test', 'Run the tests.', [
    'clean:server',
    'jshint:test',
    'express:test',
    'open:test',
    'mocha'
]);

我的 testapp 文件加载 Express 服务器的实例。它看起来像这样:

测试/testapp.js:

var express = require('express');
var path = require('path');
var http = require('http');

var app = express();

app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('jade').renderFile);
app.use(express.static(path.join(__dirname, 'views')));

app.get('/', function (req, res) {
    req.render('index.html');
})

// start up server
var PORT = 3001;

var server = http.createServer( app ).listen( PORT, function() {
    console.log('Express server listening on port ' + PORT);
} );

这是 HTML 测试文件:

测试/views/index.html

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Mocha Spec Runner</title>
    <link rel="stylesheet" href="node_modules/mocha/mocha.css">
</head>
<body>
    <div id="mocha"></div>
    <script src="node_modules/mocha/mocha.js"></script>
    <script>mocha.setup('bdd')</script>
    <script src="node_modules/chai/chai.js"></script>
    <script>
        var assert = chai.assert;
        var expect = chai.expect;
        var should = chai.should();
    </script>

    <!-- include source files here... -->

    <!-- include spec files here... -->
    <script src="../spec/test.js"></script>

    <script>mocha.run()</script>
</body>
</html>

测试/spec/test.js

'use strict';

describe('Give it some context', function () {
  describe('maybe a bit more context here', function () {
    it('should run here few assertions', function () {

    });
  });
});

当我运行grunt test时,它给了我这个错误:

Running "clean:server" (clean) task
>> 0 paths cleaned.

Running "jshint:test" (jshint) task
>> 2 files lint free.

Running "express:test" (express) task
Starting background Express server
Express server listening on port 3001

Running "open:test" (open) task

Running "mocha:all" (mocha) task
Testing: test/views/index.html

Warning: PhantomJS timed out, possibly due to a missing Mocha run() call. Use --force to continue.

Aborted due to warnings.

谁能告诉我我在这里做错了什么?

最佳答案

分几个部分回答:

第 1 部分

describe not defined 

是由于 grunt 任务 express:test (grunt-express-server) 的配置错误造成的。 Gruntfile.js 中定义的脚本是tests.js。当尝试访问 localhost:3000 时发生错误。 通过在 gruntfile.js 中定位类似服务器的脚本来解决:test/testapp.js

第 2 部分

Warning: PhantomJS timed out, possibly due to a missing Mocha run() call. Use --force to continue.

加载 test/views/index.html 页面时出错。通过使用 grunt-mocha-phantomjs 而不是 grunt-mocha 解决

关于javascript - 使用 Express 和 Grunt 进行 Mocha 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27044416/

相关文章:

javascript - 使用 StackOverflow API 检索 JSON

javascript - D3 定位在 div 内

javascript - 我正在尝试显示与用户单击的语言类型相关的不同 Div

javascript高效解析css选择器

javascript - 在 Express Router (node.js) 中使用 for 循环

javascript - 用 Mocha 测试 promise

node.js - 带有React的Socket.io在出现5条消息后表现异常

node.js - 如何在 node.js 中手动安装 grunt.js?

javascript - 如何避免自定义指令的[Vue warn]?

node.js - 如何使用 sinon/mocha 模拟 npm 模块