不可变 returns undefined with jestjs

Immutable returns undefined with jestjs

我只是做了一个简单的测试: npm i babel-cli babel-jest immutable jest-cli babel-preset-2015 并进行简单测试:

import {List} from 'immutable'

describe('it works',()=>{
  it('should just work',()=>{
    let li = List.of(1,3)
    expect(li.size).toBe(2)
  })
})

非常简单,但测试给出了这个:

- TypeError: Cannot read property 'size' of undefined

虽然 List 已定义并且是函数。

你知道这是为什么吗?

只需在第一行加上jest.unmock('immutable')即可。

根据文档:

By default, Jest automatically makes all calls to require() return a mocked version of the real module – so we need to tell Jest not to mock the file we want to test or else require('../fetchCurrentUser') will return a mock.