javascript - 使用 Jest 测试真实文件系统返回空结果
问题描述
如果文件内容与预期结果匹配,我正在使用 Jest(在 Node.js 环境中与 TypeScript 结合使用)来测试真实文件系统(不是模拟)。
我的测试如下所示:
test("getting a existing template", async () =>{
await get_template(template_name).then((template)=>{
console.log('template: ', template);
expect(template).toBe(user_input_template);
});
});
函数 get_template() 是这样实现的:
export async function get_template(template_name: string) {
let path = join(__dirname, `../templates/template-data/${template_name}.hbs`);
return await read_template(path).then((data) => {
console.log('common.ts: ',data);
return data;
});
}
和 read_template() 函数如下:
export async function read_template(path: string){
return readFileSync(path).toString();
}
像这样手动调用get_template()
函数时:
await get_template('template_name');`
接收到正确的输出字符串。但是当通过 Jest 测试环境调用该函数时,只返回一个空字符串。
我是否选择了错误的测试方法,我是否在测试这个异步函数时犯了一个明显的错误,或者这是否是 Jest 的问题?
解决方案
推荐阅读
- react-native - 在 React Naitve 中安排许多推送通知
- python - 替换数据框中字典类型列中的文本
- python - 运行时错误:CUDA 内存不足。我试过三种方法
- r - 尝试过滤每个条件的两个观察值
- r - 如何执行计算 chr 和 dbl
- reactjs - 是否可以将 GraphQL 查询拆分为单独的反应查询缓存
- macos - 我无法在 Mac 的根目录中运行脚本来创建 zip 文件
- sqlalchemy - 关系表中的 SqlAlchemy 访问列
- amazon-cloudformation - 如何为多个现有资源编写CloudFormation模板文件,实现一键部署
- android - 从与当地印度语言(旁遮普语)相关的 Playstore 下载时的 Android 特殊行为