【问题标题】:Import Jest into regular file将 Jest 导入常规文件
【发布时间】:2019-09-28 17:46:58
【问题描述】:

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

【问题讨论】:

  • 你为什么要这样做? like helpers 旨在设置要测试的代码使用的数据
  • 这就是我想做的。但我需要始终相同的模拟函数,因此将它们放入外部模块以供重用是有意义的。

标签: javascript node.js typescript jestjs


【解决方案1】:

是的,这是可能的。使用 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

【讨论】:

    猜你喜欢
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 2020-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多