[英]how to mock a named import in jest?
I used to have a set up like so:我曾经有这样的设置:
export class MyClass {
}
export default new MyClass()
then I would do: import myclass from 'libraries/myclass'
然后我会做: import myclass from 'libraries/myclass'
now I change it to:现在我将其更改为:
export const myclass = new MyClass()
and importing like so import { myclass } from 'libraries/myclass'
which seems to work in my code并像这样import { myclass } from 'libraries/myclass'
这似乎在我的代码中工作
however all my jest tests are failing但是我所有的笑话测试都失败了
I'm currently doing:我目前正在做:
jest.mock('libraries/myclass', () => ({
myclassfunction: jest.fn(),
}))
I've tried changing to:我尝试更改为:
jest.mock('./myclass.js', () => (
{
...(jest.requireActual('./myclass.js')),
myclassfunc: () => {}
}
))
but it still fails and when I console.log(myclass)
it is coming through as undefined但它仍然失败,当我console.log(myclass)
它通过未定义
You can use jest automock featurein your test, do you need to use the factory to mock it?您可以在测试中使用 jest automock 功能,是否需要使用工厂来模拟它?
import { myclass } from 'libraries/myclass';
jest.mock('libraries/myclass');
// myclass is now a mock
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.