简体   繁体   中英

Mock async function in imported module

How can I spy on an async function in an imported module?

jest.mock('snowflake-promise');
import { Snowflake } from 'snowflake-promise';

describe('Snowflake', () => {
    let snowflakeMocked: any;

    beforeEach(async () => {
        snowflakeMocked = Snowflake as jest.Mocked<typeof Snowflake>;         
    });

    test('Snowflake is...', async () => {
        jest.spyOn(Snowflake, 'execute').mockResolvedValue(new Promise<void>());

Argument of type '"execute"' is not assignable to parameter of type 'never'.

"snowflake-promise": "^4.2.0",
import { Snowflake } from 'snowflake-promise';
import { mockDeep } from 'jest-mock-extended';

describe('Snowflake', () => {
    let snowflakeMocked: DeepMockProxy<Snowflake>;

    beforeEach(async () => {
        snowflakeMocked = mockDeep<Snowflake>();         
    });

    test('Snowflake is...', async () => {
        snowflakeMocked.execute.mockResolvedValue(Promise<void>.resolve());
    });
}

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