When mocking a class in typescript using jest/ts-jest I am getting "TypeError: "X".default is not a constructor."
When mocking a class in typescript using jest/ts-jest I am getting "TypeError: "X".default is not a constructor."
我正在使用 typescript 中的 jest/ts-jest 编写单元测试,我正在尝试模拟 class 但我遇到了 TypeError。 "storage_1.default is not a constructor"。
这是我如何嘲笑 class。
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"
...
});
index.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);
utils/storage.ts
export default class AdaptiveCardsStorage {
/**
*
* Class to interface with adaptive cards storage.
*
*/
private tableService: storage.TableService;
private tableName: string = Config["TABLE_STORAGE_TABLE_NAME"];
public constructor() {
this.tableService = storage.createTableService();
}
public async getAdaptiveCard(userID: string, cardName: string): Promise<string> {
return new Promise((resolve, reject) => {
this.tableService.retrieveEntity<any>(this.tableName, userID, cardName, (err, result) => {
if (!err) {
resolve(result.Content["_"]);
} else {
reject(err);
}
});
});
}
}
这就是我得到的。
TypeError: storage_1.default is not a constructor
const handleRequest = async function (userID: string, cardName: string, context: Context) {
const storage: AdaptiveCardsStorage = new AdaptiveCardsStorage();
| ^
const adaptiveCard: string = await storage.getAdaptiveCard(userID, cardName);
at GetAdaptiveCard/index.ts:19:
at dist/GetAdaptiveCard/index.js:7:71
at Object.<anonymous>.__awaiter (dist/GetAdaptiveCard/index.js:3:12)
at handleRequest (dist/GetAdaptiveCard/index.js:29:
at Object.<anonymous> (GetAdaptiveCard/index.ts:9:29)
at dist/GetAdaptiveCard/index.js:7:71
因为您的代码使用的是 ES2015 import
s,所以它期望您的模块的 default
导出可用,但在模拟中您没有提供。修复非常简单:
jest.mock('../../utils/storage', () => {
return { // need to add this nested `default` property
default: jest.fn().mockImplementation(() => {
return {
getAdaptiveCard: mockGetAdaptiveCard
}
})
}
})
作为对 Dmitriy 的回应,请记住,如果您以 export class AdaptiveCardsStorage
的方式导出 class,您将在模拟中反映该命名空间。
jest.mock('../../utils/storage', () => {
return { // need to add this nested `default` property
AdaptiveCardsStorage: jest.fn().mockImplementation(() => {
return {
getAdaptiveCard: mockGetAdaptiveCard
}
})
}
})
我正在使用 typescript 中的 jest/ts-jest 编写单元测试,我正在尝试模拟 class 但我遇到了 TypeError。 "storage_1.default is not a constructor"。
这是我如何嘲笑 class。
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"
...
});
index.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);
utils/storage.ts
export default class AdaptiveCardsStorage {
/**
*
* Class to interface with adaptive cards storage.
*
*/
private tableService: storage.TableService;
private tableName: string = Config["TABLE_STORAGE_TABLE_NAME"];
public constructor() {
this.tableService = storage.createTableService();
}
public async getAdaptiveCard(userID: string, cardName: string): Promise<string> {
return new Promise((resolve, reject) => {
this.tableService.retrieveEntity<any>(this.tableName, userID, cardName, (err, result) => {
if (!err) {
resolve(result.Content["_"]);
} else {
reject(err);
}
});
});
}
}
这就是我得到的。
TypeError: storage_1.default is not a constructor
const handleRequest = async function (userID: string, cardName: string, context: Context) {
const storage: AdaptiveCardsStorage = new AdaptiveCardsStorage();
| ^
const adaptiveCard: string = await storage.getAdaptiveCard(userID, cardName);
at GetAdaptiveCard/index.ts:19:
at dist/GetAdaptiveCard/index.js:7:71
at Object.<anonymous>.__awaiter (dist/GetAdaptiveCard/index.js:3:12)
at handleRequest (dist/GetAdaptiveCard/index.js:29:
at Object.<anonymous> (GetAdaptiveCard/index.ts:9:29)
at dist/GetAdaptiveCard/index.js:7:71
因为您的代码使用的是 ES2015 import
s,所以它期望您的模块的 default
导出可用,但在模拟中您没有提供。修复非常简单:
jest.mock('../../utils/storage', () => {
return { // need to add this nested `default` property
default: jest.fn().mockImplementation(() => {
return {
getAdaptiveCard: mockGetAdaptiveCard
}
})
}
})
作为对 Dmitriy 的回应,请记住,如果您以 export class AdaptiveCardsStorage
的方式导出 class,您将在模拟中反映该命名空间。
jest.mock('../../utils/storage', () => {
return { // need to add this nested `default` property
AdaptiveCardsStorage: jest.fn().mockImplementation(() => {
return {
getAdaptiveCard: mockGetAdaptiveCard
}
})
}
})