javascript jest.mock 中的 Jest 'TypeError: is not a function'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51957794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:45:41  来源:igfitidea点击:

Jest 'TypeError: is not a function' in jest.mock

javascriptunit-testingmockingjestjs

提问by glenrothes

I'm writing a Jest mock, but seem to have a problem when defining a mocked function outside of the mock itself.

我正在编写一个 Jest 模拟,但在模拟本身之外定义模拟函数时似乎有问题。

I have a class:

我有一堂课:

myClass.js    

class MyClass {
  constructor(name) {
    this.name = name;
  }

  methodOne(val) {
    return val + 1;
 }

  methodTwo() {
    return 2;
  }
}

export default MyClass;

And a file using it:

和一个使用它的文件:

testSubject.js

import MyClass from './myClass';

const classInstance = new MyClass('Fido');

const testSubject = () => classInstance.methodOne(1) + classInstance.name;

export default testSubject;

And the test: testSubject.test.js

和测试:testSubject.test.js

import testSubject from './testSubject';

const mockFunction = jest.fn(() => 2)

jest.mock('./myClass', () => () => ({
    name: 'Name',
    methodOne: mockFunction,
    methodTwo: jest.fn(),
}))


describe('MyClass tests', () => {
    it('test one', () => {
        const result = testSubject()

        expect(result).toEqual('2Name')
    })
})

However, I get the following error:

但是,我收到以下错误:

TypeError: classInstance.methodOne is not a function

If I instead write:

如果我改为写:

...
methodOne: jest.fn(() => 2)

Then the test passes no problem.

然后测试通过没有问题。

Is there a way of defining this outside of the mock itself?

有没有办法在模拟本身之外定义它?

Thanks!

谢谢!

回答by Nick Hall

In my case, I had to mock a node module. I'm using React and Redux in ES6, with Jest and Enzyme for unit tests.

就我而言,我必须模拟一个节点模块。我在 ES6 中使用 React 和 Redux,并使用 Jest 和 Enzyme 进行单元测试。

In the file I'm using, and writing a test for, I'm importing the node modules as default:

在我正在使用并为其编写测试的文件中,我将默认导入节点模块:

import nodeModulePackate from 'nodeModulePackage';

So I needed to mock it as a default since I kept getting the error (0, _blah.default) is not a function.. My solution was to do:

所以我需要将它模拟为默认值,因为我一直收到错误(0, _blah.default) is not a function.。我的解决方案是这样做:

jest.mock('nodeModulePackage', () => jest.fn(() => {}));

In my case, I just needed to override the function and make it return an empty object.

就我而言,我只需要覆盖该函数并使其返回一个空对象。

If you need to call a function on that node module, you'll do the following:

如果您需要在该节点模块上调用函数,您将执行以下操作:

jest.mock('nodeModulePackage', () => ({ doSomething: jest.fn(() => return 'foo') }));

Hopefully this helps someone :)

希望这对某人有所帮助:)

回答by glenrothes

I figured this out. It is to do with hoisting, see: Jest mocking reference error

我想通了这一点。与提升有关,参见:Jest mocking 参考错误

The reason it had worked in a previous test where I had done it was because I was the testSubject was itself a class. This meant that when the testSubject was instantiated it was after the variable declaration in the test file, so the mock had access to use it.

它在我之前完成的测试中起作用的原因是因为我是 testSubject 本身就是一个类。这意味着当 testSubject 被实例化时,它是在测试文件中的变量声明之后,所以模拟可以使用它。

So in the above case it was never going to work.

因此,在上述情况下,它永远不会起作用。