Typeerror: replaceAll is not a Function

Asked

Viewed 211 times

2

I am developing a system in Electron that works well, but when running the tests in Jest I get the following error:

 console.error
    Error: Uncaught [TypeError: value.replaceAll is not a function]
        at reportException (C:\Users\rafae\Desktop\VisualData\pdv\node_modules\jsdom\lib\jsdom\living\helpers\runtime-script-errors.js:62:24)
        at innerInvokeEventListeners (C:\Users\rafae\Desktop\VisualData\pdv\node_modules\jsdom\lib\jsdom\living\events\EventTarget-impl.js:333:9)
        at invokeEventListeners (C:\Users\rafae\Desktop\VisualData\pdv\node_modules\jsdom\lib\jsdom\living\events\EventTarget-impl.js:274:3)
        ...

My code is this::

let index = 0;
const value = 'Alguma string';
return value.replaceAll('.', match => index++ === 0 ? match : '');

I am using version 26.0.1 of Jest and 14.5.4 of Node. Electron is version 11.4.7, Chrome 87.0.4280.141.

  • Why this problem occurs if on my system is working well?
  • How can I fix this in Jest without using .replace with Regexp?
  • Rafael, aren’t the parameters changed? regex/match should come first I think.. see documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll

  • The syntax is const newStr = str.replaceAll(regexp|substr, newSubstr|function). In case I used str.replaceAll(substr, function), where substr is the string to be replaced by the function. The function replaces all points . with the exception of the first, so the index++ === 0. I didn’t use Regexp.

  • To those who voted against, please say what is wrong with the question or what can be improved. I sought to put all necessary details without leaving the extensive question :)

1 answer

3


The problem

This problem happens because the String.prototype.replaceAll is a new method, added to ES2021. In addition to older browsers, older versions of Node.js (v14 or earlier) do not support this method, which is why the test in Jest fails.

You can see the browser support table on Can I Use:

Tabela do Can I Use

The Shim of that method can be obtained in the package string.prototype.replaceall.

The solution

Support for browsers

To allow browsers to run this code without problem, you need to perform polyfill, including the Shim and executing the method .shim() once.

  1. Install the package as a project dependency with npm i string.prototype.replaceall or yarn add string.prototype.replaceall;

  2. Add the polyfill:

import replaceAllInserter from 'string.prototype.replaceall';

replaceAllInserter.shim();

Support for Node.js and Jest

Update the Node.js

If you can update Node.js, you can update it to a newer version, using version 8.5 of V8, which is when the .replaceAll was implemented.

According to the list available on the Node.js website, no version uses V8.5, but from Node.js 15.0.0 version 8.6 of V8 is used. Then upgrade to Node.js for v15 or higher.

Using the developing polyfill

Since Electron is working, since Chrome version 87 already has the method implementation, you can use the polyfill only in the development environment, as mentioned in that Issue.

  1. Install the package as a development dependency with npm i -D string.prototype.replaceall or yarn add -D string.prototype.replaceall;

  2. Modify your Jest configuration by adding a new file to the property setupFilesAfterEnv:

{
  "jest": {
    "setupFilesAfterEnv": ["<rootDir>/jestSetup.js"]
  }
}
  1. In the archive jestSetup.js, add the code of polyfill:
import replaceAllInserter from 'string.prototype.replaceall';

replaceAllInserter.shim();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.