简体   繁体   English

使用Karma运行时,使用Immutable.js的Mocha测试失败

[英]Mocha tests using Immutable.js are failing when runned with Karma

here is some context. 这是一些背景。 I'm working on a project using React and Immutable.js, written with ES6. 我正在使用ES6编写的使用React和Immutable.js的项目。 I use Babel and webpack. 我使用Babel和webpack。

I wrote some unit tests using Mocha, Chai and jsdom so they could be executed outside a browser. 我使用Mocha,Chai和jsdom编写了一些单元测试,以便可以在浏览器之外执行它们。

The problem is that some of the components are using things like requiring images. 问题在于某些组件正在使用诸如要求图像之类的东西。 This stuff is handled by webpack through the specific loader. 这些东西由webpack通过特定的加载器处理。

So when running the tests in the terminal, they fail because of these unexepected requires. 因此,在终端中运行测试时,由于这些意外需求,测试将失败。

I found how to fix this by using Karma (leaving behind the ability to run the tests outside a browser) and compiling the sources before running the tests and make it so the webpack config just ignores the image loader (using null-loader). 我发现了如何通过使用Karma(不具备在浏览器外部运行测试的能力)并在运行测试并使之编译之前编译源代码来解决此问题的方法,以便Webpack配置仅忽略图像加载器(使用null加载器)。

At that point, the tests are running via Karma but some of them are failing whereas they are passing when they are runned via the terminal (I commented the lines where there was the require stuff, just for the purpose of the test). 那时,测试正在通过Karma运行,但是其中一些失败,而通过终端运行时却通过了测试(我只是出于测试目的,对有需要的东西的行进行了注释)。

The test that are failing are all related with Immutable.js meaning that I'm trying to test the equality of two Immutable objects. 失败的测试都与Immutable.js有关,这意味着我正在尝试测试两个Immutable对象的相等性。

Here is an exemple of a test : 这是一个测试的例子:

it('handles SET_STATE', () => {
    const initialState = Map();
    const action = {
        type : 'SET_STATE',
        state : Map({
            vote : Map({
                pair : List.of('Trainspotting', '28 Days Later'),
                tally : Map({ 'Trainspotting' : 1 })
            })
        })
    };

    const nextState = reducer(initialState, action);

    expect(nextState).to.equal(fromJS({
        vote: {
            pair: ['Trainspotting', '28 Days Later'],
            tally: { 'Trainspotting': 1 }
        }
    }));
});

The failure gives something like that : 失败给出了这样的东西:

1) handles SET_STATE
     reducer
     AssertionError: expected { Object (size, _root, ...) } to equal { Object (size, _root, ...) }
    at Context.<anonymous> (/Users/boris_louboff/Labs/VotingApp/voting-client/test/tests.bundle.js:36413:42 <- webpack:///test/reducer.spec.js:21:29)

All the other tests which are not testing things related to Immutable are passing. 其他所有未测试与不可变相关的测试都通过了。

If someone have any idea on what could solve this that'd be great ! 如果有人对解决这个问题有任何想法,那就太好了! Thank you. 谢谢。

I finally found what the problem was !! 我终于找到了问题所在!

The expectation to.equal seems to behave differently depending on the environnement (Node or a browser). 根据环境(节点或浏览器)的不同,期望相等的行为似乎有所不同。

const map1 = Map({a: 1, b: 2});
const map2 = Map({a: 1, b: 2});

// In Node
expect(map1).to.equal(map2) // true

// In a browser
expect(map1).to.equal(map2) // false

The solution is to use the Immutable.js API .is() 解决方案是使用Immutable.js API .is()

expect(Immutable.is(map1, map2)).to.be.true // true in both Node and browser !

While your suggestion works, this is the part of code needed to solve the problem: 当您的建议有效时,这是解决问题所需的代码部分:

import chai from 'chai';
import chaiImmutable from 'chai-immutable';

chai.use(chaiImmutable);

After adding it, expect(map1).to.equal(map2) // true works in karma. 添加后, expect(map1).to.equal(map2) // true可在业力中使用。 But I didn't find a way to include this in all my tests file like you can do with mocha with command 但是我没有找到一种方法可以像我可以通过命令使用mocha一样将其包含在我的所有测试文件中

mocha --compilers js:babel-core/register --require ./test/test_helper.js\"test/**/*@(.js|.jsx)\"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM