N34*_*N34 130 testing rest node.js
我正在尝试定义一些端点并使用进行测试nodejs.在server.js我有:
var express = require('express');
var func1 = require('./func1.js');
var port = 8080;
var server = express();
server.configure(function(){
server.use(express.bodyParser());
});
server.post('/testend/', func1.testend);
Run Code Online (Sandbox Code Playgroud)
并在func1.js:
var testend = function(req, res) {
serialPort.write("1", function(err, results) {
serialPort.write("2" + "\n", function(err, results) {
});
});
});
exports.testend = testend;
Run Code Online (Sandbox Code Playgroud)
现在,test.js我正在尝试使用此端点:
var should = require('should');
var assert = require('assert');
var request = require('supertest');
var http = require('http');
var app = require('./../server.js');
var port = 8080;
describe('Account', function() {
var url = "http://localhost:" + port.toString();
it('test starts', function(done) {
request(url).post('/testend/')
// end handles the response
.end(function(err, res) {
if (err) {
throw err;
}
res.body.error.should.type('string');
done();
});
});
});
Run Code Online (Sandbox Code Playgroud)
但是,当我运行时,node test.js我收到此错误:
describe('Account', function() {
^
ReferenceError: describe is not defined
at Object. (/test/test.js:9:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
我该如何解决这个问题?
msc*_*dex 174
假设您正在测试mocha,您必须使用mocha命令而不是node可执行文件来运行测试.
所以如果你还没有,请确保你这样做npm install mocha -g.然后只需mocha在项目的根目录中运行.
tof*_*fee 69
如果您使用的是vscode,则要调试文件
我tdd以前用过ReferenceError: describe is not defined
但是,当我使用时bdd,它有效!
浪费半天时间解决...
{
"type": "node",
"request": "launch",
"name": "Mocha Tests",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"-u",
"bdd",// set to bdd, not tdd
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/test/**/*.js"
],
"internalConsoleOptions": "openOnSessionStart"
},
Run Code Online (Sandbox Code Playgroud)
can*_*era 45
要在不全局安装Mocha的情况下使用node/npm运行测试,您可以执行以下操作:
•在本地安装Mocha到您的项目(npm install mocha --save-dev)
•(可选)安装断言库(npm install chai --save-dev)
•在你的中package.json,为scriptsmocha二进制文件添加一个部分并定位它
"scripts": {
"test": "node ./node_modules/mocha/bin/mocha"
}
Run Code Online (Sandbox Code Playgroud)
•将spec文件放在/test根目录中指定的目录中
•在spec文件中,导入断言库
var expect = require('chai').expect;
Run Code Online (Sandbox Code Playgroud)
•您并不需要进口摩卡,运行mocha.setup,或致电mocha.run()
•然后从项目根目录运行脚本:
npm test
Run Code Online (Sandbox Code Playgroud)
sub*_*777 17
你也可以这样做:
var mocha = require('mocha')
var describe = mocha.describe
var it = mocha.it
var assert = require('chai').assert
describe('#indexOf()', function() {
it('should return -1 when not present', function() {
assert.equal([1,2,3].indexOf(4), -1)
})
})
Run Code Online (Sandbox Code Playgroud)
参考:http://mochajs.org/#require
OP 询问node是否从mocha. 这是一个非常常见的用例,请参阅以编程方式使用 Mocha
这就是将描述和它注入到我的测试中的内容。
mocha.ui('bdd').run(function (failures) {
process.on('exit', function () {
process.exit(failures);
});
});
Run Code Online (Sandbox Code Playgroud)
我tdd像在文档中一样尝试过,但这不起作用,但 bdd 起作用了。
对于 Jest 你必须"jest": true添加.eslintrc
{
"env": {
"browser": true,
"es6": true,
"jest": true
},
...
Run Code Online (Sandbox Code Playgroud)