【发布时间】:2021-12-04 23:33:47
【问题描述】:
我正在关注这篇文章
我需要测试一个用 TypeScript 编写的 Azure 函数。我在同一个 test/ 文件夹中有以下文件。
DefaultContext.js
module.exports = {
log: jest.fn()
};
GetAnnouncements.test.ts
import httpTrigger from '../GetAnnouncements/index'
const context = require('defaultContext');
test('Get announcements', async () => {
const request = {
query: { name: 'Bill' }
};
await httpTrigger(context, request);
expect(context.log.mock.calls.length).toBe(1);
expect(context.res.body).toEqual('Hello Bill');
});
当我运行 npm test 时,第二行抛出错误
Cannot find module 'defaultContext' from 'tests/GetAnnouncements.test.ts'
However, Jest was able to find:
'./defaultContext.js'
You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'jsx', 'ts', 'tsx', 'json', 'node'].
这是什么意思?我该如何解决这个错误?
【问题讨论】:
标签: typescript azure-functions ts-jest