首页 > 解决方案 > 正则表达式:javascript 字符串 ^(?!\\[functional\\]).+$ 文件上的负匹配器无法排除 [功能]

问题描述

在 package.json 我有 2 个脚本命令:

    "test:unit": "jest --watch --testNamePattern='^(?!\\[functional\\]).+$'",
    "test:functional": "jest --watch --testNamePattern='\\[functional\\]'",

复制^(?!\\[functional\\]).+$https://regex101.com/,它与下面的参数 1 中的测试字符串不匹配describe()

  describe("[functional] live tests", () => {

当更改为([functional]).+$时,模式匹配。\我必须在每一端删除一对以删除.json文件的转义符(我认为)。

这是我npm run test:unit在项目根目录中运行时看到的内容:

// the functional test runs (not desired)
$ npm run test:unit

 functions/src/classes/__tests__/Functional.test.ts:30:47 - error TS2339: Property 'submit' does not exist on type 'Element'.

    30         await emailForm.evaluate(form => form.submit());
                                                     ~~~~~~

 RUNS  ...s/__tests__/Functional.test.ts
Test Suites: 1 failed, 1 skipped, 3 passed, 4 of 5 total
Tests:       2 skipped, 16 passed, 18 total
Snapshots:   0 total
Time:        8.965s, estimated 27s
Ran all test suites with tests matching "^(?!\[functional\]).+$".

Active Filters: test name /^(?!\[functional\]).+$/

功能测试没有构建出来,它解释了语法错误,在这里并不重要。关键问题是为什么没有跳过测试。

我相信这个问题与正则表达式负匹配器有关。没有唯一匹配测试的正匹配器!具有或嵌套在描述块中[functional]

$ npm run test:functional

Test Suites: 1 failed, 4 skipped, 1 of 5 total
Active Filters: test name /\[functional\]/

任何人都知道为什么负正则表达式模式在 期间失败npm run test:unit

标签: regex

解决方案


我没有使用正则表达式修复,而是将单元测试脚本上的标志更改为忽略,然后复制 [功能] 的匹配模式:

"test:unit": "jest --watch --testIgnorePattern='\\[functional\\]'",
"test:functional": "jest --watch --testNamePattern='\\[functional\\]'",

推荐阅读