gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 11:22:23 26 4
gpt4 key购买 nike

我有一个项目,我的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com