javascript - 将 Jest 导入常规文件

标签 javascript node.js typescript jestjs

是否可以将 Jest 导入到一个文件中,该文件不会作为测试运行且不包含测试用例(例如,用于测试的辅助模块)?

最佳答案

是的,这是可能的。将 Jest 作为开发依赖项,jest 在测试环境中可用。您可以在帮助文件中创建模拟的 Jest 函数并将它们导入到您的测试中。

测试文件:

import { exampleFunc } from '../helper';

test('exampleFunc/0', () => {
  console.log(exampleFunc());
});

辅助文件:

export const exampleFunc = jest.fn().mockReturnValue('mocked return');

测试运行:

$ jest
 PASS  __tests__/index.test.ts
  ✓ exampleFunc/0 (12ms)

  console.log __tests__/index.test.ts:4
    mocked return

关于javascript - 将 Jest 导入常规文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56084002/

相关文章:

javascript - 为什么进度条与倒计时分开工作

node.js - ChatGPT 按 block 读取 PDF

javascript - Expressjs passport-local 无法注销

javascript - 从继承自基类型的类列表中返回类型 T 的项 - typescript

javascript - FTP错误: 202 Command not implemented

javascript - 如何知道查询中缺少哪些参数?

javascript - 不能在 npm 包中使用别名

javascript - Mongo - 多个引用对象数组中的值的总和

typescript - 是否可以从对象中提取函数参数类型作为元组?

javascript - ionic 2 - 将句子字符串转换为单词数组