升级到 Angular 15 中断单元测试

问题描述 投票:0回答:0

我正在跑步

Angular 15.2.5
并让
tsConfig.json
使用
ES2022
用于
target
module
lib
。我注意到
ng test
因此错误而失败。即使 karma.conf.js 文件为空,我也会得到同样的错误。

ERROR [config]: Error in config file! Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/user101/project/node_modules/is-docker/index.js from /Users/user101/project/karma.conf.js not supported.

Instead change the require of index.js in /Users/user101/project/karma.conf.js to a dynamic import() which is available in all CommonJS modules.

这就是我的 karma.conf.js 的样子。我该如何解决这个 ESM 错误?

const isDocker = require('is-docker')();

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      'karma-spec-reporter',
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage'),
      require('@angular-devkit/build-angular/plugins/karma'),
    ],
    client: {
      clearContext: false,
      random: false
    },
    coverageIstanbulReporter: {
      dir: require('path').join(__dirname, 'coverage'),
      reports: ['html', 'lcovonly'],
      fixWebpackSourcePaths: true,
    },
    customLaunchers: {
      ChromeCustom: {
        base: 'ChromeHeadless',
        flags: isDocker ? ['--no-sandbox'] : [],
      },
      ChromeDebug: {
        base: 'Chrome',
        flags: ['--remote-debugging-port=9888'],
      },
    },
    reporters: ['progress', 'kjhtml', 'spec'],
    specReporter: {
      maxLogLines: 5,
      suppressErrorSummary: false,
      suppressFailed: false,
      suppressPassed: true,
      suppressSkipped: false,
      showSpecTiming: false,
      failFast: false,
      prefixes: {
        success: '    OK: ',
        failure: 'FAILED: ',
        skipped: 'SKIPPED: '
      }
    },
    port: 1234,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['ChromeDebug'],
    singleRun: false,
    captureTimeout: 120000,
    browserDisconnectTimeout: 120000,
    browserDisconnectTolerance: 3,
    browserNoActivityTimeout: 120000,
    failOnFailingTestSuite: true,
    browserConsoleLogOptions: {
      level: 'warn', format: '%b %T: %m', terminal: false
    }
  });
};

Angular 和 Node 版本:

Angular CLI: 15.2.4
Node: 16.17.1
Package Manager: npm 8.15.0
OS: darwin arm64

Angular: 15.2.5
... animations, cdk, common, compiler, compiler-cli, core, forms
... language-service, material, platform-browser
... platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1502.4
@angular-devkit/build-angular   15.2.4
@angular-devkit/core            15.2.4
@angular-devkit/schematics      15.2.4
@angular/cli                    15.2.4
@schematics/angular             15.2.4
rxjs                            7.8.0
typescript                      4.9.5
webpack                         5.77.0
angular cypress karma-jasmine es2022
© www.soinside.com 2019 - 2024. All rights reserved.