在命令行上运行Mocha并包含文件 [英] Running Mocha on the command line and Including a file

查看:110
本文介绍了在命令行上运行Mocha并包含文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I’m trying to setup some JS Unit tests using Mocha, and Ideally, I'd like to run this via the command line oppose to a web page. (TL:DR; at the bottom)

首先,我做了一些胡扯测试,以确认Array是否按预期工作,这是我直接从Mocha的页面 http://visionmedia.github.io/mocha/#getting-started ,它按预期工作.

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.

在这一点上,我接着创建了一个新文件/src/cow.js:

At this point, to up the stakes I then created a new file /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:

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)

这显然不理想.我认为这是因为test.js不了解cow.js.如您所见,我已经使用了require,但是这似乎不起作用.

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;

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

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

谢谢!

推荐答案

唯一缺少的是引用了require()中的Cow出口.

The only thing missing is making a reference to the Cow export from the require().

您只需更改此行:

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

对此:

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

这篇关于在命令行上运行Mocha并包含文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆