1. upgrade to jest 28
The main dependencies and versions of jest in package.json
after the upgrade are as follows.
Compared to the dependencies associated with jest@27, you need to add the jest-environment-jsdom
library dependency separately if you need a jsdom execution environment, in addition to the updated version.
2. jest 28 error problems and solution reference
2.1 SyntaxError: Unexpected token ’export'
The main error message reference is as follows.
chalk, uuid and other libraries that are adapted to the latest esm scheme basically report this error. The main reason is that jest uses jsdom to execute with the browser-esm version of the default export, and the default transform of jest 28 filters the contents of the node_modules directory.
The current workaround can be as follows.
Method 1: Specify the entry for the commonjs version of the corresponding package in the moduleNameMapper
configuration. Example.
Method 2: Modify the jest transform filtering rules to allow transformations to be performed for these unusual dependencies. example.
Hint: Method 2 uses browser scheme, uuid library will also report an error because the current version of jsdom does not support crypto.getRandomValues
.
Option 3: mock unimportant dependency libraries. If the dependency is not heavily used and has no impact on the test logic, you can just mock it. For example, mock for uuid.
2.2 setTimeout: Matcher error: received value must be a mock or spy function
Many use cases involving setTimeout calls report errors as follows.
|
|
A look at the test code reveals a ts type exception. The original writeup was
|
|
Just change the incoming parameters to the following.
|
|