Javascript 使用flowjs在“import*”上分配模拟函数
假设我有以下代码:Javascript 使用flowjs在“import*”上分配模拟函数,javascript,types,mocking,flowtype,covariant,Javascript,Types,Mocking,Flowtype,Covariant,假设我有以下代码: export const exampleFunc = () => {} 然后在测试中使用,如: import * as exampleAll from '../../path/to/example' describe('Example', () => { exampleAll.exampleFunc = jest.fn() }) 这无法通过以下方式进行类型检查: 无法将jest.fn()分配给exampleAll.exampleFunc,因为属性ex
export const exampleFunc = () => {}
然后在测试中使用,如:
import * as exampleAll from '../../path/to/example'
describe('Example', () => {
exampleAll.exampleFunc = jest.fn()
})
这无法通过以下方式进行类型检查:
无法将jest.fn()分配给exampleAll.exampleFunc,因为属性exampleFunc不可写
44│ const mockStore = configureStore([reduxThunk])
45│
46│ describe('[Redux action] ExampleAction()', () => {
47│ exampleAll.exampleFunc = jest.fn()
48│ beforeEach(() => {
49│ exampleAll.exampleFunc.mockReturnValue(() => () => {})
50│ })
为什么它不可写?我如何在使用flow的同时进行这样的模拟呢
谢谢 我认为进口被视为只读。您可以将导入复制到另一个对象,
const testExamples={…exampleAll,exampleFunc:jest.fn()}代码>我尝试了你的解决方案,但没有解决问题。exampleAll
模块的函数exampleFunc
未成功模拟。是否找到解决方案?我遇到了同样的问题。最终我停止了对测试的打字检查。但我很想听听是否有人想出了解决办法!你能用jest.spyOn(exampleAll,'exampleFunc')
监视函数吗?