为什么我不能在我的 wdio.conf.js 文件中使用 'import'?我正在尝试将 HtmlReporter 用于 WebDriverIO

Why can I not use 'import' in my wdio.conf.js file? I am trying to use HtmlReporter for WebDriverIO

我正在尝试按照此处的说明配置和使用 WebDriverIO 的 HTML Reporter: https://webdriver.io/docs/rpii-wdio-html-reporter.html

它说我需要在我的 wdio.conf.js 文件中添加一个导入语句,如下所示:

// wdio.conf.js
import { ReportAggregator, HtmlReporter} from '@rpii/wdio-html-reporter' ;
module.exports = {

但是,当我这样做并尝试 运行 我的测试时,我收到以下错误:

从 '@rpii/wdio-html-reporter' 导入 { ReportAggregator, HtmlReporter} ; ^

语法错误:意外的标记 {

它根本不允许我在这里使用任何导入语句。

有人知道为什么吗?

这是我的完整wdio.conf.js内容:

const config = require('./Lib/config')
// wdio.conf.js
import { ReportAggregator, HtmlReporter} from '@rpii/wdio-html-reporter' ;

exports.config = {

    runner: 'local',
   
    specs: [
        My tests here
    ],
  
    exclude: [

    ],
    
    maxInstances: 10,
    
    capabilities: [{
        
        maxInstances: config.maxInstance,
        //
        browserName: 'chrome',
        
    }],
    
    logLevel: config.logLevel,
  
    bail: config.bail,
   
    baseUrl: 'http://localhost',
   
    waitforTimeout: 15000,
    
    connectionRetryTimeout: 120000,
 
    connectionRetryCount: 3,
    
    services: [   'chromedriver'
      
    ],
    
   
    framework: 'mocha',
  
    reporters: ['spec']
    [HtmlReporter, {
        debug: true,
        outputDir: './reports/html-reports/',
        filename: 'report.html',
        reportTitle: 'Test Report Title',
        
        //to show the report in a browser when done       showInBrowser: true,

   
        useOnAfterCommandForScreenshot: false,

        
        LOG: log4j.getLogger("default")
    }
    ],
    
    mochaOpts: {
        // Babel setup
        require: ['@babel/register'],
        ui: 'bdd',
        timeout: 60000
    },
    
}

尝试在文件顶部添加 require('@babel/register');

而不是使用

import { ReportAggregator, HtmlReporter} from '@rpii/wdio-html-reporter' ;

尝试使用

const {ReportAggregator, HtmlReporter} = require('@rpii/wdio-html-reporter');

这是 ES6 import 语法,所以你需要先用 Babel 编译你的配置,或者使用 ES5 import with require。此外,您的配置中有错字,记者应该是一个数组,在该数组中您可以设置一个带有字符串(具有默认选项)的记者或一个带有记者名称和选项对象的数组:

记者:[ 'spec', [HtmlReporter, { outputDir: './reports/html-reports/', ... } ] ]