TypeError: Constructor Set requires 'new'

TypeError: Constructor Set requires 'new'

开玩笑地给出我的测试套件,结果是 "TypeError: Constructor Set requires 'new'"

.babelrc配置为:

{
  "presets": [["env", { "modules": false }]],
  "env": {
    "test": {
      "presets": [["env"]]
    }
  }
}

测试套件:

test('Constructor Set requires "new"', () => {
  class A extends Set {
    constructor(...arg) {
      super(...arg);
      this.add(10);
    }
  }

  const a = new A([1, 2]);
  expect([...a][2]).toEqual(10);
});

预计测试应该会通过。相反,出现错误:

TypeError: Constructor Set requires 'new'
        at A.Set (<anonymous>)

      1 | test('Constructor Set requires "new"', () => {
      2 |   class A extends Set {
    > 3 |     constructor(...arg) {
        |                         ^
      4 |       super(...arg);
      5 |       this.add(10);
      6 |     }

要解决此问题,需要为 preset-env 插件设置目标。对于 jest,它是 node。 (可以放在.bybelrc"test"段)

"env": {
  "test": {
    "presets": [["env", { "targets": { "node": true } }]]
   }
 }