网页
综合
更新时间 2026-11-14 16:21:31
Jestの設定 · Jest
自动蜘蛛
jestjs.io
正文约 4910 字
Jestの哲学はデフォルトでうまく動作することですが、時にはより細かい設定が必要になることもあります。
設定は、専用の JavaScript、TypeScript、または JSON ファイルで定義することをおすすめします。 The file will be discovered automatically, if it is named jest.config.js|ts|mjs|mts|cjs|cts|json. You can use --config flag to pass an explicit path to the file.
出力される構成は、常に JSON 形式でシリアライズ可能でなければならないことに注意してください。
To read TypeScript configuration files Jest by default requires ts-node. You can override this behavior by adding a @jest-config-loader docblock at the top of the file. Currently, ts-node and esbuild-register is supported. Make sure ts-node or the loader you specify is installed.
You can also pass options to the loader, for instance to enable transpileOnly.
このオプションにより、Jestはインポートされたすべてのモジュールを自動的にモックします。 テストで使用されるすべてのモジュールは、APIのインターフェースを維持したまま代替の実装を持つことになります。
Node modules are automatically mocked when you have a manual mock in place (e.g.: __mocks__/lodash.js). More info here.
Node.js core modules, like fs, are not mocked by default. They can be mocked explicitly, like jest.mock('fs').
デフォルトでは、Jest はすべてのテストを実行し、完了時にすべてのエラーをコンソールに生成します。 The bail config option can be used here to have Jest stop running tests after n failures. Setting bail to true is the same as setting bail to 1.
Default: "/tmp/<path>"
Jestがキャッシュする依存情報を格納するディレクトリを指定します。
Jest attempts to scan your dependency tree once (up-front) and cache it in order to ease some of the filesystem churn that needs to happen while running tests. この設定オプションによりJestがディスク上にキャッシュを格納する場所を指定できます。
各テストの実行前に、モックコール、インスタンス、コンテキスト、結果を自動的にクリアします。 Equivalent to calling jest.clearAllMocks() before each test. このオプションは与えられたモックの実装を削除することはしません。
テスト実行中にカバレッジ情報を取得するかどうかを指定します。 カバレッジ取得を指定して実行される全てのファイルについて書き換えるので、テストが大幅に遅くなることがあります。
Jest ships with two coverage providers: babel (default) and v8. See the coverageProvider option for more details.
The babel and v8 coverage providers use /* istanbul ignore next */ and /* c8 ignore next */ comments to exclude lines from coverage reports, respectively. For more information, you can view the istanbuljs documentation and the c8 documentation.
An array of glob patterns indicating a set of files for which coverage information should be collected. ファイルが指定されたglobパターンに一致すれば、ファイルにテストが存在せずテストスイートが必要としないファイルであってもカバレッジ情報を収集します。
This will collect coverage information for all the files inside the project's rootDir, except the ones that match **/node_modules/** or **/vendor/**.
Each glob pattern is applied in the order they are specified in the config. For example ["!**/__tests__/**", "**/*.js"] will not exclude __tests__ because the negation is overwritten with the second pattern. In order to make the negated glob work in this example it has to come after **/*.js.
This option requires collectCoverage to be set to true or Jest to be invoked with --coverage.
以下のようなカバレッジ出力が表示されている場合は、
Most likely your glob patterns are not matching any files. Refer to the micromatch documentation to ensure your globs are compatible.
The directory where Jest should output its coverage files.
Default: ["/node_modules/"]
An array of regexp pattern strings that are matched against all file paths before executing the test. If the file path matches any of the patterns, coverage information will be skipped.
These pattern strings match against the full path. Use the <rootDir> string token to include the path to your project's root directory to prevent it from accidentally ignoring all of your files in different environments that may have different root directories. Example: ["<rootDir>/build/", "<rootDir>/node_modules/"].
Indicates which provider should be used to instrument code for coverage. Allowed values are babel (default) or v8.
Default: ["clover", "json", "lcov", "text"]
A list of reporter names that Jest uses when writing coverage reports. Any istanbul reporter can be used.
Setting this option overwrites the default values. Add "text" or "text-summary" to see a coverage summary in the console output.
Additional options can be passed using the tuple form. For example, you may hide coverage report lines for all fully-covered files:
For more information about the options object shape refer to CoverageReporterWithOptions type in the type definitions.
This will be used to configure minimum threshold enforcement for coverage results. Thresholds can be specified as global, as a glob, and as a directory or file path. If thresholds aren't met, jest will fail.
If a threshold is set to a positive number, it will be interpreted as the minimum percentage of coverage required.
If a threshold is set to a negative number, it will be treated as the maximum number of uncovered items allowed.
For example, with the following configuration jest will fail if there is less than 80% branch, line, and function coverage, or if there are more than 10 uncovered statements:
Global threshold for lines.
Global threshold for functions.
Global threshold for statements.
Global threshold for branches.
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。