运行角度单元测试时出现 ESM 导入错误

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

我正在运行 Angular 15 并让 tsConfig.json 使用 ES2022。我注意到

ng test
因此错误而失败。

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 typescript jasmine karma-jasmine karma-runner
© www.soinside.com 2019 - 2024. All rights reserved.