当前位置:网站首页>JEST 单元测试说明 config.json
JEST 单元测试说明 config.json
2022-06-11 11:28:00 【Michael18811380328】
JEST 单元测试配置文件说明
最近在做一个 JS + TS 项目,使用 JEST 做测试,我本地的配置文件如下
module.exports = {
roots: [
'<rootDir>/test',
],
testRegex: 'test/(.+)\\.test\\.(jsx?|tsx?)$',
transform: {
'^.+\\.[jt]sx?$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
};
参数简单说明
- roots 测试文件的位置(字符串或者数组)
- testRegex 测试文件匹配的文件格式(正则表达式)这里匹配 .test.jsx .test.tsx 的文件
- transform 转换器(编译器):是一个对象,key 是正则表达式(满足的文件),值是采用的编译器(转换器)ts-test 这个库
- moduleFileExtensions 这个是支持的文件类型
参数具体说明
roots [array<string>]
Default: ["<rootDir>"]
Jest用于搜索文件的目录的路径列表
A list of paths to directories that Jest should use to search for files in.
There are times where you only want Jest to search in a single sub-directory (such as cases where you have a src/ directory in your repo), but prevent it from accessing the rest of the repo.
Note: While rootDir is mostly used as a token to be re-used in other configuration options, roots is used by the internals of Jest to locate test files and source files. This applies also when searching for manual mocks for modules from node_modules (__mocks__ will need to live in one of the roots).
Note: By default, roots has a single entry <rootDir> but there are cases where you may want to have multiple roots within one project, for example roots: ["<rootDir>/src/", "<rootDir>/tests/"].
testRegex [string | array<string>]
Default: (/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$
Jest用于检测测试文件的模式
The pattern or patterns Jest uses to detect test files.
By default it looks for .js, .jsx, .ts and .tsx files inside of __tests__ folders, as well as any files with a suffix of .test or .spec (e.g. Component.test.js or Component.spec.js). It will also find files called test.js or spec.js. See also [testMatcharray], but note that you cannot specify both options.
The following is a visualization of the default regex:
├── __tests__
│ └── component.spec.js # test
│ └── anything # test
├── package.json # not test
├── foo.test.js # test
├── bar.spec.jsx # test
└── component.js # not test
Note: testRegex will try to detect test files using the absolute file path, therefore, having a folder with a name that matches it will run all the files as tests
transform[object<string, pathToTransformer | [pathToTransformer, object]>]
Default: {"\\.[jt]sx?$": "babel-jest"}
A map from regular expressions to paths to transformers. A transformer is a module that provides a synchronous function for transforming source files.
从正则表达式到转换器的路径的映射。 转换器是提供同步功能以转换源文件的模块。例如,如果您希望能够在节点尚不支持的模块或测试中使用新的语言功能,则可以插入许多将JavaScript的未来版本编译为当前版本的编译器之一。
For example, if you wanted to be able to use a new language feature in your modules or tests that aren’t yet supported by node, you might plug in one of many compilers that compile a future version of JavaScript to a current one. Example: see the examples/typescript example or the webpack tutorial.
Examples of such compilers include:
- Babel
- TypeScript
- async-to-gen
- To build your own please visit the Custom Transformer section
You can pass configuration to a transformer like {filePattern: ['path-to-transformer', {options}]} For example, to configure babel-jest for non-default behavior, {"\\.js$": ['babel-jest', {rootMode: "upward"}]}
Note: a transformer is only run once per file unless the file has changed. During the development of a transformer it can be useful to run Jest with --no-cache to frequently delete Jest’s cache.
Note: when adding additional code transformers, this will overwrite the default config and babel-jest is no longer automatically loaded. If you want to use it to compile JavaScript or Typescript, it has to be explicitly defined by adding {"\\.[jt]sx?$": "babel-jest"} to the transform property. See babel-jest plugin
moduleFileExtensions[array<string>]
Default: ["js", "json", "jsx", "ts", "tsx", "node"]
An array of file extensions your modules use. If you require modules without specifying a file extension, these are the extensions Jest will look for, in left-to-right order.
We recommend placing the extensions most commonly used in your project on the left, so if you are using TypeScript, you may want to consider moving “ts” and/or “tsx” to the beginning of the array.
模块使用的文件扩展名数组。 如果您需要模块而未指定文件扩展名,则这些是Jest将按从左到右的顺序查找的扩展名。
我们建议将项目中最常用的扩展名放在左侧,因此,如果您使用的是TypeScript,则可能需要考虑将“ ts”和/或“ tsx”移动到数组的开头。
参考文档
其他的配置可以参考官方文档

边栏推荐
- Etcd introduction
- 统计出现次数最多的前K个字符串
- Gerber文件在PCB制造中的作用
- 17.5 concept, usage, deadlock demonstration and solution of mutex
- Want to be iron man? It is said that many big men use it to get started
- Publish WordPress database cache plug-in: DB cache reloaded 3.1
- 找到自己的优势,才能干活不累,事半功倍!
- Template engine - thymeleaf
- 《公司理财师专业能力》笔记
- Smart sidebar plug-in: Mo widgets
猜你喜欢

Appearance mode -- it has been used in various packages for a long time!

2022 | framework for Android interview -- Analysis of the core principles of binder, handler, WMS and AMS!

Intermediate web development engineer, interview questions + Notes + project practice

SpingBoot+Quartrz生产环境的应用支持分布式、自定义corn、反射执行多任务

Display of receiving address list 【 project mall 】

JS prototype. The find () method has no effect on the object array. It is urgent...

Use pydub to modify the bit rate of the wav file, and an error is reported: c:\programdata\anaconda3\lib\site packages\pydub\utils py:170: RuntimeWarning:
![[file upload vulnerability 06] server file content detection and bypass experiment + image horse production method (based on upload-labs-14 shooting range)](/img/30/79516390c2b2b50a224eaa84a0c1c9.jpg)
[file upload vulnerability 06] server file content detection and bypass experiment + image horse production method (based on upload-labs-14 shooting range)

MYCAT sub database and sub table

全国多年太阳辐射空间分布数据1981-2022年、气温分布数据、蒸散量数据、蒸发量数据、降雨量分布数据、日照数据、风速数据
随机推荐
统计出现次数最多的前K个字符串
Use yolov3 to train yourself to make datasets and get started quickly
No category parents插件帮你去掉分类链接中的category前缀
MyCat-分库分表
web开发选型,web开发毕业谁
设置默认收货地址【项目 商城】
Node connects to MySQL database and writes fuzzy query interface
JS merge two objects (interview questions)
Can't you be free without wealth?
Count the top k strings with the most occurrences
Appearance mode -- it has been used in various packages for a long time!
WordPress landing page customization plug-in recommendation
Weekly Postgres world news 2022w08
[C language] anonymous/unnamed struct & Union
快速搭建ELK7.3
NFT digital collection system platform construction
在WordPress媒体库中创建文件夹
2019年书单
Let WordPress support registered users to upload custom avatars
js合并两个对象(面试题)