Testing jest忽略除1个包以外的所有节点模块

Testing jest忽略除1个包以外的所有节点模块,testing,jestjs,babeljs,Testing,Jestjs,Babeljs,我使用jest进行测试,目前我可以忽略节点\ U模块。然而,问题是在node_模块中有一个包我想用babel传输。现在我忽略的模式如下所示: testPathIgnorePatterns: ['./node_modules/'], // one way to do it testpathIgnorePatterns: ['/node_modules\/(?!my-package).+\.js$'] // another way to do it - this regex seems simp

我使用jest进行测试,目前我可以忽略节点\ U模块。然而,问题是在node_模块中有一个包我想用babel传输。现在我忽略的模式如下所示:

testPathIgnorePatterns: ['./node_modules/'],
// one way to do it
testpathIgnorePatterns: ['/node_modules\/(?!my-package).+\.js$']

// another way to do it - this regex seems simpler to me
testpathIgnorePatterns: ['/node_modules\/(?!my-package)(.*)']
transformIgnorePatterns: ['/node_modules\/(?!my-package)(.*)']

如果我的包被称为“我的包”,我如何才能使testPathIgnorePatterns在节点_模块中不忽略它

TL;DR-将此配置键与此正则表达式一起使用:

transformIgnorePatterns: ['/node_modules\/(?!my-package)(.*)']

这是一个由两部分组成的答案

第1部分

testPathIgnorePatterns
键接受一个字符串数组/regex,因此我可以传递一个regex,该regex将匹配除我想要的模块之外的所有节点模块,如下所示:

testPathIgnorePatterns: ['./node_modules/'],
// one way to do it
testpathIgnorePatterns: ['/node_modules\/(?!my-package).+\.js$']

// another way to do it - this regex seems simpler to me
testpathIgnorePatterns: ['/node_modules\/(?!my-package)(.*)']
transformIgnorePatterns: ['/node_modules\/(?!my-package)(.*)']
这基本上是说,忽略除我的包之外的所有节点模块。然而,这里需要注意的是,
testPathIgnorePatterns
并不确定模块是否应该由babel传输。此键用于jest了解测试文件的位置。基本上,使用这个键,您告诉jest‘不要在
节点模块
中查找扩展名为
.test.js
的文件’,因为您不想运行导入的节点模块包的测试

因此,实际上,您希望完全忽略此设置,因为jest将自动将其默认为
['/node\u modules/']
,或者如果您希望添加其他要忽略的路径,则需要:

testpathIgnorePatterns: ['other/file/path', '/node_modules/']
第二部分

根据第1部分的上下文,放置正则表达式以允许jest从节点模块传输特定包的正确位置是:
transformIgnorePatterns
。所以看起来是这样的:

testPathIgnorePatterns: ['./node_modules/'],
// one way to do it
testpathIgnorePatterns: ['/node_modules\/(?!my-package).+\.js$']

// another way to do it - this regex seems simpler to me
testpathIgnorePatterns: ['/node_modules\/(?!my-package)(.*)']
transformIgnorePatterns: ['/node_modules\/(?!my-package)(.*)']
这样,jest将不会传输除“我的包”之外的任何节点模块包。我相信上面提到的两个键的默认值都是“node_modules”,这意味着在
testPath
transform