Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 来自npm脚本的jest正则表达式_Javascript_Node.js_Regex_Jestjs - Fatal编程技术网

Javascript 来自npm脚本的jest正则表达式

Javascript 来自npm脚本的jest正则表达式,javascript,node.js,regex,jestjs,Javascript,Node.js,Regex,Jestjs,我想执行两个不同的测试环境, 一个用于集成测试,第二个用于单元测试 文件夹结构如下所示: __tests__/ut/p/a/t/h/file.test.ts __tests__/it/p/a/t/h/file.test.ts "test:ut": "jest \"(__tests__/ut/.*\\.test)\\.(tsx?|ts?)$\"", "test:it": "jest \"(__tests__/it/.*\\.test)\\.(tsx?|ts?)$\"", 我提出了以下正则表达式

我想执行两个不同的测试环境,
一个用于集成测试,第二个用于单元测试

文件夹结构如下所示:

__tests__/ut/p/a/t/h/file.test.ts
__tests__/it/p/a/t/h/file.test.ts
"test:ut": "jest \"(__tests__/ut/.*\\.test)\\.(tsx?|ts?)$\"",
"test:it": "jest \"(__tests__/it/.*\\.test)\\.(tsx?|ts?)$\"",
我提出了以下正则表达式:

(\uu测试\uu/ut/*\.test)\.(tsx?\ts?)$

(\uu测试\uu/it/*\.test)\.(tsx?\ts?)$

我的脚本如下所示:

__tests__/ut/p/a/t/h/file.test.ts
__tests__/it/p/a/t/h/file.test.ts
"test:ut": "jest \"(__tests__/ut/.*\\.test)\\.(tsx?|ts?)$\"",
"test:it": "jest \"(__tests__/it/.*\\.test)\\.(tsx?|ts?)$\"",
但当我运行
纱线测试:ut
时,我得到:

yarn run v1.3.2
$ jest "(__tests__/ut/.*\.test)\.(tsx?|ts?)$"
No tests found
In /home/dev/sample
  20 files checked.
  testMatch: **/__tests__/**/*.js?(x),**/?(*.)(spec|test).js?(x) - 0 matches
  testPathIgnorePatterns: /node_modules/ - 20 matches
Pattern: (__tests__/ut/.*\.test)\.(tsx?|ts?)$ - 0 matches
Done in 0.49s.
不过,如果我在json配置文件中使用相同的模式,它就可以工作。
我想这与我如何执行
玩笑有关

我怎样才能修好它

编辑

执行此命令:

“test:ut”:“jest-cjest.json\”(\uu-tests\uu\\\/ut\\/.\\\.test)\\。(tsx?;ts?$\”,

未找到
测试的结果

但如果我将regex放在配置文件中,它确实可以工作:

{
  "testRegex": "(__tests__\\/ut\\/.*\\.test)\\.(tsx?|ts?)$",
  "transform": {
    "^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
  },
  "moduleFileExtensions": [
    "ts",
    "tsx",
    "js",
    "json",
    "jsx"
  ]
}
{
“testRegex”:“(\uu tests\uu\\\/ut\\/.\\\\.test)\\\(tsx?;ts?)$”,
“转变”:{
“^.+\.tsx?$”:“/node\u modules/ts jest/preprocessor.js”
},
“moduleFileExtensions”:[
“ts”,
“tsx”,
“js”,
“json”,
“jsx”
]
}

作为
npm脚本执行它
我只是从配置文件中注释掉了
testRegex

我来到这里时遇到了一个类似的问题,最终对我有效的是Jest CLI选项。因此,对于您的问题,您只有两个测试位置,在执行
it
测试时,我将排除
/ut/
文件夹,反之亦然:

"test:ut": "jest --testPathIgnorePatterns \"/(it)/\"",
"test:it": "jest --testPathIgnorePatterns \"/(ut)/\"",
另外,如果有帮助,我没有在Jest配置中使用
testRegex
,而是:

"testMatch": [
  "/__tests__/.*\\.(ts|tsx|js)",
  "**/?(*.)(spec|test).ts?(x)"
],

没有太多关于jest本身的知识,为什么
testMatch
匹配“.js”或“.jsx”文件,而您的正则表达式匹配的是TypeScript?如果正则表达式仅与初始testMatch收集的内容相匹配,那么在集合中就不会有任何要匹配的ts文件。只是一个猜测。第二个不同的注释是,您的测试是否在输出中的“/home/dev/sample/\uuuuu tests\uuuuuuu/…”中?您可以假设,如果testPathIgnorePatterns有“20个已检查文件”和“20个匹配项”,则可能是其他问题(不过,还是一个猜测。可能20个ignore目录匹配与检查的20个文件是分开的)。我不知道为什么要搜索
js
文件……我在问题中添加了一些东西