开玩笑地忽略除1个软件包以外的所有node_modules

时间:2019-09-24 18:23:01

标签: testing jestjs babel

我使用笑话进行测试,目前我可以忽略node_modules。但是问题是,我想与babel一起转译node_modules中的1个软件包。现在,我被忽略的模式看起来像这样:

testPathIgnorePatterns: ['./node_modules/'],

如果我的程序包被称为“ my-package”,我该怎么做,以便testPathIgnorePatterns不会在node_modules内部忽略它?

1 个答案:

答案 0 :(得分:0)

这是一个两部分的答案。

第1部分

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)(.*)']

这基本上是说,忽略除my-package之外的所有node_modules。但是,这里要注意的是,testPathIgnorePatterns不能确定模块是否应通过babel进行编译。该密钥是为了让您知道您的测试文件在哪里。基本上,使用此密钥,是在告诉您“不要在node_modules中查找扩展名为.test.js的文件”,因为您不想运行导入的node_module软件包的测试。

因此,实际上,您想完全忽略此设置,因为jest会自动将其默认设置为['/node_modules/'],或者,如果您想添加其他路径来忽略,则需要:

testpathIgnorePatterns: ['other/file/path', '/node_modules/']

第2部分

鉴于第1部分中的上下文,放置正则表达式以允许开玩笑地从node_modules转换特定程序包的正确位置是:transformIgnorePatterns。所以看起来像这样:

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

这样,除了“ my-package”以外,jest不会翻译任何node_modules软件包。我相信上述两个键的默认值为'node_modules'-意思是,在testPathtransform

中,node_modules都会被自动忽略。

Jest docs for these configuration settings