簡體   English   中英

在摩卡測試中找不到柴出口

[英]Chai exports are not found in Mocha test

我創建了簡單的摩卡測試。 當使用Node“assert”模塊時,它完美地工作。 我從命令行運行它(Mocha作為全局節點模塊安裝):

$ mocha myTest.js

․

1 test complete (6 ms)

該腳本如下所示:

var assert = require("assert")
describe('Array', function(){
    describe('#indexOf()', function(){
        it('should return -1 when the value is not present', function(){
            assert.equal(-1, [1,2,3].indexOf(5));
            assert.equal(-1, [1,2,3].indexOf(0));
        })
    })
})

好吧,我試圖添加Chai而不是斷言庫。 我先安裝它:

npm install chai

因此,目錄node_modules已在我的項目中創建。 到目前為止很棒。 然后,我改變了腳本使用Chai:

var chai = require("chai");

describe('Array', function(){
    describe('#indexOf()', function(){
        it('should return -1 when the value is not present', function(){
            [1,2,3].indexOf(5).should.equal(-1);
            expect([1,2,3].indexOf(5)).to.equal(-1);
            assert.equal([1,2,3].indexOf(5),-1);
        })
    })
});

它不起作用,Mocha測試失敗並出現TypeError:

TypeError: Cannot call method 'equal' of undefined

我認為Chai沒有定義,所以它是未定義的。

這怎么可能?

如何讓我的測試與Chai一起運行? 我試圖在全球范圍內安裝Chai而沒有任何效果。 我也用-r chai運行腳本也沒有效果。

顯然,Chai模塊已加載,但未定義變量(Object.prototype屬性)。 我怎樣才能解決這個問題?

var expect = require('chai').expect;

這將使你的expect電話工作。 但是,你還有一個完全來自不同庫的should調用,所以改變

[1,2,3].indexOf(5).should.equal(-1);

expect([1,2,3].indexOf(5)).to.equal(-1);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM