网页 综合 更新时间 2026-05-07 18:52:38

Globals · Jest

自动蜘蛛 jestjs.io 正文约 4740 字
Globals · Jest
In your test files, Jest puts each of these methods and objects into the global environment. You don't have to require or import anything to use them. However, if you prefer explicit imports, you can do import {describe, expect, test} from '@jest/globals'.
The TypeScript examples from this page will only work as documented if you explicitly import Jest APIs:
Consult the Getting Started guide for details on how to setup Jest with TypeScript.
Runs a function after all the tests in this file have completed. If the function returns a promise or is a generator, Jest waits for that promise to resolve before continuing.
Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait before aborting. The default timeout is 5 seconds.
This is often useful if you want to clean up some global setup state that is shared across tests.
Here the afterAll ensures that cleanUpDatabase is called after all tests run.
If afterAll is inside a describe block, it runs at the end of the describe block.
If you want to run some cleanup after every test instead of after all tests, use afterEach instead.
Runs a function after each one of the tests in this file completes. If the function returns a promise or is a generator, Jest waits for that promise to resolve before continuing.
Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait before aborting. The default timeout is 5 seconds.
This is often useful if you want to clean up some temporary state that is created by each test.
Here the afterEach ensures that cleanUpDatabase is called after each test runs.
If afterEach is inside a describe block, it only runs after the tests that are inside this describe block.
If you want to run some cleanup just once, after all of the tests run, use afterAll instead.
Runs a function before any of the tests in this file run. If the function returns a promise or is a generator, Jest waits for that promise to resolve before running tests.
Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait before aborting. The default timeout is 5 seconds.
This is often useful if you want to set up some global state that will be used by many tests.
Here the beforeAll ensures that the database is set up before tests run. If setup was synchronous, you could do this without beforeAll. The key is that Jest will wait for a promise to resolve, so you can have asynchronous setup as well.
If beforeAll is inside a describe block, it runs at the beginning of the describe block.
If you want to run something before every test instead of before any test runs, use beforeEach instead.
Runs a function before each of the tests in this file runs. If the function returns a promise or is a generator, Jest waits for that promise to resolve before running the test.
Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait before aborting. The default timeout is 5 seconds.
This is often useful if you want to reset some global state that will be used by many tests.
Here the beforeEach ensures that the database is reset for each test.
If beforeEach is inside a describe block, it runs for each test in the describe block.
If you only need to run some setup code once, before any tests run, use beforeAll instead.
describe(name, fn) creates a block that groups together several related tests. For example, if you have a myBeverage object that is supposed to be delicious but not sour, you could test it with:
This isn't required - you can write the test blocks directly at the top level. But this can be handy if you prefer your tests to be organized into groups.
You can also nest describe blocks if you have a hierarchy of tests:
Use describe.each if you keep duplicating the same test suites with different data. describe.each allows you to write the test suite once and pass data in.
describe.each is available with two APIs:
table: Array of Arrays with the arguments that are passed into the fn for each row. If you pass in a 1D array of primitives, internally it will be mapped to a table i.e. [1, 2, 3] -> [[1], [2], [3]].
name: String the title of the test suite.
fn: Function the suite of tests to be run, this is the function that will receive the parameters in each row as function arguments.
Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait for each row before aborting. The default timeout is 5 seconds.
Also under the alias: fdescribe(name, fn)
You can use describe.only if you want to run only one describe block:
Also under the aliases: fdescribe.each(table)(name, fn) and fdescribe.each`table`(name, fn)
Use describe.only.each if you want to only run specific tests suites of data driven tests.
访问原文 返回搜索 https://jestjs.io/docs/30.4/api
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。