【发布时间】:2019-05-11 15:17:41
【问题描述】:
我在official documentation、this blogpost 和this issue in jest repo 中一直在阅读有关此内容的信息。文档或博文都没有涵盖我要测试的场景。问题线程确实涵盖了它,但我无法使用那里提出的任何解决方案来通过测试。
您能否完成以下代码以使测试通过?
编辑:为了清楚起见,我想知道如何模拟导入的函数,以便在某些测试中仍然可以使用原始实现,而在其他测试中使用模拟。
这些
foo和bar函数可能看起来非常简单,实现中的一个小改动可以使测试通过,但我的问题是关于模拟。对于更多的上下文,我实际上是在尝试测试在同一个文件中定义的两个 saga,它们具有我不想运行的副作用。我仍然想分别对每个 sagas 进行单元测试
// a.js
export const foo = () => 'foo-' + bar()
export const bar = () => 'bar'
// a.test.js
import {
foo,
bar
} from './a'
describe('foo', () => {
it('should return foo-MOCKED_BAR', () => {
expect(foo()).toBe('foo-MOCKED_BAR')
})
it('should have the mocked implementation of bar', () => {
expect(bar()).toBe('MOCKED_BAR')
})
})
describe('bar', () => {
it('should have the original implementation of bar', () => {
expect(bar()).toBe('bar')
})
})
describe('foo and bar together', () => {
it('should have the original implementation of both', () => {
expect(foo()).toBe('foo-bar')
})
})
谢谢!
【问题讨论】:
标签: javascript unit-testing mocking jestjs es6-modules