You can use Babel for transpiling your code (to ES5) and also allow it to inject polyfills where needed.
In your case, using .replaceAll
method fails because, as explained in other answers, the tests are running on a Node version which does not support that method.
I do not advice doing that the other answerers say, because they are simply fixing this specific problem, but you might ran into similar problems if you'll use other methods not supported by your current Node version.
To guarantee never to have such problems, Babel is used alongside core-js
.
Here's an important quote from this page:
https://jestjs.io/docs/getting-started#using-babel
babel-jest
is automatically installed when installing Jest and will automatically transform files if a babel configuration exists in your project.
You would also need to install a Babel preset for vue
so it would compile you vue-specific files: https://www.npmjs.com/package/@vue/babel-preset-app
So, in your root folder, look for the Babel configuration file, or create one: babel.config.js
(used to be .babelrc.js
or a .json
file).
Use this configuration:
- make sure to update your
core-js
package and explicitly mention the minor version when configuring it for Babel (see below)
- must activate the
useBuiltIns
setting in @babel/preset-env
for core-js polyfills to work.
- the
targets
setting should also be specified, since your tests run on Node (and not your browser). You can also set up a .browserlist
file for that.
module.exports = {
presets: [
['@babel/preset-env', {
"corejs": "3.26",
"useBuiltIns": "usage",
"targets": { "node": "current" }
}],
'@babel/preset-react'],
plugins: [
"@babel/plugin-transform-runtime"
]
};
Interesting Read:
Running Mocha 6 ES6 tests with Babel 7, how to set up?