admin 管理员组文章数量: 1086019
I’m trying to setup some JS Unit tests using Mocha, and Ideally, I'd like to run this via the mand line oppose to a web page. (TL:DR; at the bottom)
First I did some bullshit test to confirm that Array worked as expected which I pulled directly from Mocha's page and this worked as expected.
At this point, to up the stakes I then created a new file /src/cow.js :
/** This example is taken from /**/
(function(exports) {
"use strict";
function Cow(name) {
this.name = name || "Anon cow";
}
exports.Cow = Cow;
Cow.prototype = {
greets: function(target) {
if (!target)
throw new Error("missing target");
return this.name + " greets " + target;
}
};
})(this);
along with my test file /test/test.js:
var chai = require("chai"),
expect = chai.expect;
require( "../src/cow.js");
describe( "Cow", function(){
describe( "constructor", function(){
it( "should have a default name", function(){
var cow = new Cow();
expect( cow.name).to.equal( "Anon cow");
});
it( "show use provided name", function(){
var name = "duck Cow",
cow = new Cow( name );
expect( cow.name).to.equal( name );
})
});
});
and ran 'mocha'. At which point I receive the following errors:
1) Cow constructor should have a default name:
ReferenceError: Cow is not defined
at Context.<anonymous> (C:\Users\myUserName\\jsUnitTesting\helloWorld\test\test.js:10:27)
at Test.Runnable.run (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:211:32)
at Runner.runTest (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:358:10)
at C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:404:12
at next (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:284:14)
at C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:293:7
at next (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:237:23)
at Object._onImmediate (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:261:5)
at processImmediate [as _immediateCallback] (timers.js:330:15)
2) Cow constructor show use provided name:
ReferenceError: Cow is not defined
at Context.<anonymous> (C:\Users\myUserName\\jsUnitTesting\helloWorld\test\test.js:16:27)
at Test.Runnable.run (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:211:32)
at Runner.runTest (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:358:10)
at C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:404:12
at next (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:284:14)
at C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:293:7
at next (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:237:23)
at Object._onImmediate (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:261:5)
at processImmediate [as _immediateCallback] (timers.js:330:15)
This is obviously less than ideal. I figure this is because test.js has no idea about cow.js. As you can see I've used the require, however this does not seem to work.
TL:DR;
So, what is the proper way to add this file (cow.js) to my test file (test.js)? Currently, I'm not using any libs aside from just mocha and nodeJS
Thanks!
I’m trying to setup some JS Unit tests using Mocha, and Ideally, I'd like to run this via the mand line oppose to a web page. (TL:DR; at the bottom)
First I did some bullshit test to confirm that Array worked as expected which I pulled directly from Mocha's page http://visionmedia.github.io/mocha/#getting-started and this worked as expected.
At this point, to up the stakes I then created a new file /src/cow.js :
/** This example is taken from https://nicolas.perriault/code/2013/testing-frontend-javascript-code-using-mocha-chai-and-sinon/**/
(function(exports) {
"use strict";
function Cow(name) {
this.name = name || "Anon cow";
}
exports.Cow = Cow;
Cow.prototype = {
greets: function(target) {
if (!target)
throw new Error("missing target");
return this.name + " greets " + target;
}
};
})(this);
along with my test file /test/test.js:
var chai = require("chai"),
expect = chai.expect;
require( "../src/cow.js");
describe( "Cow", function(){
describe( "constructor", function(){
it( "should have a default name", function(){
var cow = new Cow();
expect( cow.name).to.equal( "Anon cow");
});
it( "show use provided name", function(){
var name = "duck Cow",
cow = new Cow( name );
expect( cow.name).to.equal( name );
})
});
});
and ran 'mocha'. At which point I receive the following errors:
1) Cow constructor should have a default name:
ReferenceError: Cow is not defined
at Context.<anonymous> (C:\Users\myUserName\\jsUnitTesting\helloWorld\test\test.js:10:27)
at Test.Runnable.run (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:211:32)
at Runner.runTest (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:358:10)
at C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:404:12
at next (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:284:14)
at C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:293:7
at next (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:237:23)
at Object._onImmediate (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:261:5)
at processImmediate [as _immediateCallback] (timers.js:330:15)
2) Cow constructor show use provided name:
ReferenceError: Cow is not defined
at Context.<anonymous> (C:\Users\myUserName\\jsUnitTesting\helloWorld\test\test.js:16:27)
at Test.Runnable.run (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:211:32)
at Runner.runTest (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:358:10)
at C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:404:12
at next (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:284:14)
at C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:293:7
at next (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:237:23)
at Object._onImmediate (C:\Users\myUserName\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:261:5)
at processImmediate [as _immediateCallback] (timers.js:330:15)
This is obviously less than ideal. I figure this is because test.js has no idea about cow.js. As you can see I've used the require, however this does not seem to work.
TL:DR;
So, what is the proper way to add this file (cow.js) to my test file (test.js)? Currently, I'm not using any libs aside from just mocha and nodeJS
Thanks!
Share Improve this question asked Nov 19, 2013 at 23:29 whombawhomba 3776 silver badges13 bronze badges2 Answers
Reset to default 5The only thing missing is making a reference to the Cow export from the require().
You'll just have to change this line:
require( "../src/cow.js" );
To this:
var Cow = require( "../src/cow.js" ).Cow;
You can always specify files at runtime
-r, --require <name> require the given module
mocha -r ../src/cow.js yourcode.js
本文标签: javascriptRunning Mocha on the command line and Including a fileStack Overflow
版权声明:本文标题:javascript - Running Mocha on the command line and Including a file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744032521a2521768.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论