小编San*_*nez的帖子

在使用 jest/ts-jest 模拟打字稿中的类时,我收到“TypeError: "X".default is not a constructor。”

我正在使用打字稿中的 jest/ts-jest 编写单元测试,我正在尝试模拟一个类,但我收到了 TypeError。“storage_1.default 不是构造函数”。

这是我嘲笑班级的方式。

index.test.ts

import GetAdaptiveCard from '../../GetAdaptiveCard/index'

const mockGetAdaptiveCard = jest.fn();
jest.mock('../../utils/storage', () => {
    return jest.fn().mockImplementation(() => {
        return {
            getAdaptiveCard: mockGetAdaptiveCard
        }   
    })
})
test('http trigger should return adaptive card', async () => {
....
    await GetAdaptiveCard(context, req);//calls AdaptiveCardStorage. The class I am mocking in "../../utils/storage"
...
});

Run Code Online (Sandbox Code Playgroud)

索引.ts

import AdaptiveCardsStorage from '../utils/storage';
...
const storage: AdaptiveCardsStorage = new AdaptiveCardsStorage(); //This is where I get the TypeError
const adaptiveCard: string = await storage.getAdaptiveCard(userID, cardName); …
Run Code Online (Sandbox Code Playgroud)

mocking typescript jestjs ts-jest

1
推荐指数
1
解决办法
3012
查看次数

标签 统计

jestjs ×1

mocking ×1

ts-jest ×1

typescript ×1