简体   繁体   中英

Mock module export function with jest

I have a file lib.ts :

export const getValue() { return 'original value'; }

export const callGetValue() { return getValue(); }

And a test file lib.spec.ts :

import * as lib from './lib';

// ...
    it('works', () => {
        jest.spyOn(lib, 'getValue').mockImplementation( () => 'new value');
        expect(lib.callGetValue()).toBe('new value'); // it's not!
    });
// ...

I want to mock getValue() and make it return 'new value' . It's not. Why?!

不幸的是,这实际上是不可能的,请参阅https://github.com/facebook/jest/issues/936中的讨论

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM