【问题标题】:How to mock specific function in a module如何模拟模块中的特定功能
【发布时间】:2018-07-29 04:01:43
【问题描述】:

假设我在module.js 中有一个方法bestFunction,如下所示:

//module.js

export const bestFunction = x => { 
    return x + 1
}

如果我的代码本身是这样导入的,我该如何模拟 bestFunction

import { bestFunction } from './module.js'

【问题讨论】:

  • 这篇文章非常令人困惑,因为标题说 un-mock,但问题是关于 mocking。

标签: reactjs tdd jestjs enzyme


【解决方案1】:

在你的测试中:

  import * as depends from './module.js';

    describe("when testing..", () => {
        beforeEach(() => {
            depends.bestFunction = jest.fn().mockImplementation(() => 22); 
        })

        it ('doStuff', () => {
            expect(objectUnderTest.foo()).toEqual('22 red balloons');

        });
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-07
    • 1970-01-01
    • 2019-07-16
    • 2019-08-28
    • 2018-01-18
    • 1970-01-01
    相关资源
    最近更新 更多