【问题标题】:Test specific file using Jest使用 Jest 测试特定文件
【发布时间】:2020-11-21 05:27:12
【问题描述】:

在我的项目文件夹中,我有很多子文件夹,其中包含 js 代码和 test.js 文件。我希望能够测试特定的文件。例如,假设在我们的项目文件夹中,我们有 'fib' 文件夹:

C:.
└───exercises
    └───fib
            fib-test.js
            index.js

现在,我从练习文件夹中执行 jest 命令:

jest fib\fib-test.js

我得到:

No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In C:\exercises
  62 files checked.
  testMatch: **/__tests__/**/*.[jt]s?(x), **/?(*.)+(spec|test).[tj]s?(x) - 26 matches
  testPathIgnorePatterns: \\node_modules\\ - 62 matches
  testRegex:  - 0 matches
Pattern: fib\fib-test.js - 0 matches

如果我只是开玩笑,我会运行所有测试。如果我将 fib 文件夹移出练习文件夹,它会按预期工作。以下是所有文件的代码:

index.js:

function fib(n) {}

module.exports = fib;

test.js:

const fib = require('./index');

test('Fib function is defined', () => {
  expect(typeof fib).toEqual('function');
});

test('calculates correct fib value for 1', () => {
  expect(fib(1)).toEqual(1);
});

test('calculates correct fib value for 2', () => {
  expect(fib(2)).toEqual(1);
});

test('calculates correct fib value for 3', () => {
  expect(fib(3)).toEqual(2);
});

test('calculates correct fib value for 4', () => {
  expect(fib(4)).toEqual(3);
});

test('calculates correct fib value for 15', () => {
  expect(fib(39)).toEqual(63245986);
});

package.json

{
  "name": "dev",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "jest": {
    "testEnvironment": "node"
  },
  "author": "",
  "license": "ISC"
}

我已经尝试了所有这些解决方案,但都没有成功:

  1. Run single test of a specific test suite in Jest
  2. How do I run a single test using Jest?
  3. How do I test a single file using Jest?

但是能够通过 --watch 标志运行 jest 命令并在正则表达式菜单中输入 fib\test.js 的相对路径来实现所需的结果。问题是不进入手表菜单怎么办?

【问题讨论】:

    标签: javascript testing jestjs


    【解决方案1】:

    TL;DR

    1. 将你的测试文件fib-test.js重命名为fib.test.js
    2. 运行jest fib.test.js
    3. volia, jest 将运行您指定的文件
    4. 您还可以测试您指定的任意数量的测试文件jest XXX.test.js YYY.test.js

    长篇大论

    我看到您的问题隐含着一些假设。如果您明确列出它们,您可以使问题更具体:

    1. 您在 Windows 机器上运行
    2. 您的项目根目录或jest.config.js 位于C:\exercises

    对于 (1),我手头没有 Windows 机器,请运行并验证我的解决方案。

    除了假设,错误发生在您的测试文件名:fib-test.jsjest 正在寻找 XXX.test.js 并且不会匹配和测试 XXX-test.js。您可以在错误消息中找到线索:

    testMatch: ... **/?(*.)+(spec|test).[tj]s?(x) ...
    

    将文件重命名为fib.test.js 后,jest fib.test.js 将搜索项目根目录或jest.config.js 所在位置的所有子文件夹;并匹配和测试特定的测试文件。

    我的jest版本:"ts-jest": "^27.0.3"

    小技巧#1

    再看完整的错误信息:

    testMatch: **/__tests__/**/*.[jt]s?(x), **/?(*.)+(spec|test).[tj]s?(x) - 26 matches
    

    您实际上可以将所有测试用例分组到<rootDir>/__tests__/ 并将__test__ 放入.gitignore,这样您的测试用例就可以保密而不是推送到 Git。

    小技巧#2

    因为jest 查看每个子文件夹,您实际上可以将文件夹名称放在测试文件之前:

    jest fib/fib.test.js本质上等同于jest fib.test.js

    不过,开玩笑看不出有什么理由让自己如此烦恼。

    【讨论】:

      猜你喜欢
      • 2019-03-09
      • 2018-02-23
      • 2019-11-24
      • 2017-04-27
      • 2022-01-22
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 2020-06-16
      相关资源
      最近更新 更多