A new way to consume Jest - @jest/globals
Jest has relied on globals popularized by the Jasmine testing framework and others such as describe
, it
, test
and expect
. While this is a widely used pattern, many people have expressed their preference for importing testing related functions instead. To support this use case, we added a new package @jest/globals
that can be used to import testing functions: import {expect, jest, test} from '@jest/globals';
.
Caveats:
- Currently the globals still exist in the environment but we will introduce a mode to disable globals in the future. Similarly, you cannot use
const jest = require('@jest/globals')
as you’ll get declaration errors because thejest
variable is still a global for now. - There is currently no way to add custom matchers to the TypeScript definitions when using globals like this.
- While this allows running tests without globals, it does not allow running tests without Jest’s test runner at this time.