开玩笑:TypeError:replaceAll 不是函数

leo*_*ess 21 javascript node.js jestjs

String.prototype.replaceAll()是一种有用的方法,在构建和执行时一切正常。但是,所有 Jest 测试都失败并显示以下错误:

TypeError: replaceAll is not a function
Run Code Online (Sandbox Code Playgroud)

这些是我的依赖项:

"dependencies": {
  "core-js": "^3.6.5",
  "vue": "^2.6.11",
  "vue-class-component": "^7.2.3",
  "vue-i18n": "^8.22.0",
  "vue-property-decorator": "^8.4.2",
  "vue-router": "^3.3.4",
  "vuex": "^3.5.1"
},
"devDependencies": {
  "@vue/test-utils": "^1.1.0",
  "jest-junit": "^12.0.0",
  "ts-jest": "^26.4.1",
  "typescript": "~3.9.3",
  "vue-jest": "^3.0.7",
  "vue-template-compiler": "^2.6.10"
},
Run Code Online (Sandbox Code Playgroud)

我该如何解决这种行为?

小智 33

这很可能发生,因为String.prototype.replaceAll它没有在 Node.js 中实现(至少从 version 开始v14.15.0)。

您可以使用的一种替代方法是正则表达式,如本例所示:

const str = 'foo-foo';
const regex = /foo/g; // Note the 'g' flag, which matches all occurrences of the expression

console.log(str.replace(regex, 'bar')); // 'bar-bar'
Run Code Online (Sandbox Code Playgroud)

您可以在此处查看有关正则表达式的更多信息。


Raf*_*res 7

问题

发生这种情况是因为replaceAll一个新功能没有在所有浏览器中实现,也没有在旧的 Node.js 版本中实现,如另一个答案中所述。您可以在Can I Use 中查看哪些浏览器支持它:

在此处输入图片说明

但是,.replace您可以添加一个 polyfill 来支持旧浏览器(如果需要),而不是与 RegExp 一起使用。就我而言,我使用的是 Electron,所以我只需要为 Jest 执行此操作。

垫片可以在这里找到

解决方案

用作浏览器的 polyfill

  1. 使用npm i string.prototype.replaceall或作为依赖项安装yarn add string.prototype.replaceall

  2. 在您的项目中添加以下代码。只需在一处添加即可。

import replaceAllInserter from 'string.prototype.replaceall';

replaceAllInserter.shim();
Run Code Online (Sandbox Code Playgroud)

仅在 Jest 中使用

更新 Node.js

正如@leonheess 在评论中提到的,您可以将 Node.js 更新到更新的版本。您将需要一个使用V8 中的 8.5版本的版本,这是.replaceAll实现时的版本

根据Node.js 网站上提供列表,两个版本都使用 V8 8.5,但从 Node.js 15.0.0 开始,使用的是 V8 的 8.6 版本。因此,升级到 Node.js v15 或更高版本。

用作 polyfill

如果您只想为 Jest 解决此问题,您可以按照本期所述进行操作

  1. 使用npm i -D string.prototype.replaceall或安装为开发依赖项yarn add -D string.prototype.replaceall

  2. 修改您的 Jest 配置,添加setupFilesAfterEnv如下属性:

{
  "jest": {
    "setupFilesAfterEnv": ["<rootDir>/jestSetup.js"]
  }
}
Run Code Online (Sandbox Code Playgroud)
  1. 将以下代码添加到您的jestSetup文件中:
import replaceAllInserter from 'string.prototype.replaceall';

replaceAllInserter.shim();
Run Code Online (Sandbox Code Playgroud)


小智 5

您还可以将其添加到 main.js 文件中的任何位置

if (typeof String.prototype.replaceAll === "undefined") {
  String.prototype.replaceAll = function (match, replace) {
    return this.replace(new RegExp(match, 'g'), () => replace);
  }
}
Run Code Online (Sandbox Code Playgroud)