编程语言
首页 > 编程语言> > javascript – 在命令行上运行Mocha并包含文件

javascript – 在命令行上运行Mocha并包含文件

作者:互联网

我正在尝试使用Mocha设置一些JS单元测试,理想情况下,我想通过命令行运行它来反对一个网页. (TL:DR;在底部)

首先,我做了一些废话测试,以确认Array按预期工作,我直接从Mocha的页面http://visionmedia.github.io/mocha/#getting-started中提取,这按预期工作.

此时,为了增加赌注,我创建了一个新文件/src/cow.js:

/** This example is taken from https://nicolas.perriault.net/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);

以及我的测试文件/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 );
        })
    });
});

然后跑’摩卡’.此时我收到以下错误:

    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)

这显然不太理想.我认为这是因为test.js不知道cow.js.正如你所看到的,我已经使用了require,但是这似乎不起作用.

TL:DR;

那么,将这个文件(cow.js)添加到我的测试文件(test.js)的正确方法是什么?目前,除了mocha和nodeJS之外,我没有使用任何库

谢谢!

解决方法:

唯一缺少的是从require()引用Cow导出.

你只需要改变这一行:

require( "../src/cow.js" );

对此:

var Cow = require( "../src/cow.js" ).Cow;

标签:chai,javascript,unit-testing,node-js,mocha
来源: https://codeday.me/bug/20190728/1564780.html