ReferenceError : describe가 정의되지 않은 NodeJ
일부 엔드 포인트를 정의하고을 사용하여 테스트를 수행하려고합니다 nodejs
. 에서 server.js
I 있습니다 :
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);
그리고 func1.js
:
var testend = function(req, res) {
serialPort.write("1", function(err, results) {
serialPort.write("2" + "\n", function(err, results) {
});
});
});
exports.testend = testend;
이제이 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();
});
});
});
그러나 내가 실행할 node test.js
때이 오류가 발생합니다 :
describe ( '계정', 함수 () { ^ ReferenceError : 설명이 정의되지 않았습니다 객체에서. (/test/test.js:9:1) Module._compile (module.js : 456 : 26)에서 Object.Module._extensions..js에서 (module.js : 474 : 10) Module.load (module.js : 356 : 32)에서 Function.Module._load에서 (module.js : 312 : 12) Function.Module.runMain에서 (module.js : 497 : 10) 시작할 때 (node.js : 119 : 16) node.js : 906 : 3에서
문제를 어떻게 해결할 수 있습니까?
Assuming you are testing via mocha
, you have to run your tests using the mocha
command instead of the node
executable.
So if you haven't already, make sure you do npm install mocha -g
. Then just run mocha
in your project's root directory.
if you are using vscode, want to debug your files
I used tdd
before, it throw ReferenceError: describe is not defined
But, when I use bdd
, it works!
waste half day to solve it....
{
"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"
},
To run tests with node/npm without installing Mocha globally, you can do this:
• Install Mocha locally to your project (npm install mocha --save-dev
)
• Optionally install an assertion library (npm install chai --save-dev
)
• In your package.json
, add a section for scripts
and target the mocha binary
"scripts": {
"test": "node ./node_modules/mocha/bin/mocha"
}
• Put your spec files in a directory named /test
in your root directory
• In your spec files, import the assertion library
var expect = require('chai').expect;
• You don't need to import mocha, run mocha.setup
, or call mocha.run()
• Then run the script from your project root:
npm test
You can also do like this:
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)
})
})
Reference: http://mochajs.org/#require
OP asked about running from node
not from mocha
. This is a very common use case, see Using Mocha Programatically
This is what injected describe and it into my tests.
mocha.ui('bdd').run(function (failures) {
process.on('exit', function () {
process.exit(failures);
});
});
I tried tdd
like in the docs, but that didn't work, bdd worked though.
i have this error when using "--ui tdd". remove this or using "--ui bdd" fix problem.
Use any Javascript testing framework to run it
npm install jest -g
jest ./**/**/demo.spec.js
npm install mocha -g
mocha ./**/**/demo.spec.js
npm install jasmine-node -g
jasmine-node ./**/**/demo.spec.js
참고URL : https://stackoverflow.com/questions/28400459/referenceerror-describe-is-not-defined-nodejs
'Programing' 카테고리의 다른 글
WPF-CommandBindings를 통해 명령이 'CanExecute'를 다시 평가하도록하는 방법 (0) | 2020.07.07 |
---|---|
UIBarButtonItem 이미지는 얼마나 커야합니까? (0) | 2020.07.07 |
Keras의 Tensorboard 콜백은 어떻게 사용합니까? (0) | 2020.07.07 |
함수 호출 전의 @ 문자 (0) | 2020.07.07 |
함수가 SQL 데이터베이스에 존재하는지 확인하는 방법 (0) | 2020.07.07 |