问题
I read all the related mocha "describe" is not defined
posts but none of them seem to be suitable for my situation.
I use meteor and installed the "mocha": "^3.5.0"
package by npm
I have created a /test
folder in my meteor root directory.
and a sample test mochatest.js
var assert = require("assert"); // node.js core module
describe('Array', function(){
describe('#indexOf()', function(){
it('should return -1 when the value is not present', function(){
assert.equal(-1, [1,2,3].indexOf(4)); // 4 is not present in this array so indexOf returns -1
})
})
});
When i run mocha the test passes.
But when i start my normal server i get: ReferenceError: describe is not defined
.meteor/packages/meteor-tool/.1.3.5_1.1wj76e8++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:280
throw(ex);
^
ReferenceError: describe is not defined
at meteorInstall.test.mochatest.js (test/mochatest.js:3:1)
at fileEvaluate (packages/modules-runtime/.npm/package/node_modules/install/install.js:153:1)
at require (packages/modules-runtime/.npm/package/node_modules/install/install.js:82:1)
at project-i18n.js:6:1
at .meteor/local/build/programs/server/boot.js:297:10
at Array.forEach (native)
at Function._.each._.forEach (.meteor/packages/meteor-tool/.1.3.5_1.1wj76e8++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11)
at .meteor/local/build/programs/server/boot.js:133:5
I have a feeling that meteor wants to run the test on startup but can't find mocha stuff.
So what to do?
回答1:
You need to rename the /test
folder to /tests
.
From the official Meteor Testing Guide:
The Meteor build tool and the meteor test command ignore any files located in any
tests/
directory. This allows you to put tests in this directory that you can run using a test runner outside of Meteor’s built-in test tools and still not have those files loaded in your application.
You could also consider renaming your test files with extensions like:
*.test[s].*
, or *.spec[s].*
-- So your file could be named mocha.test.js
.
Such files are ignored by Meteor build tool as well.
回答2:
Looks like the test file is somehow invoked when the server runs. It should not be included anywhere in the server code. If you can't figure out what's happening, you can try to debug the server and put a breakpoint at the "describe" line - try to see how you got there.
来源:https://stackoverflow.com/questions/45543254/mocha-describe-is-not-defined