【问题标题】:karma mocha chai phantomJS - Can't find variable: exports\nat src/index.js:3:30karma mocha chai phantomJS - 找不到变量:exports\nat src/index.js:3:30
【发布时间】:2018-03-19 16:41:46
【问题描述】:

我正在尝试使用 karma mocha chai 和 phatomJS 测试用 ES6 编写的 javascript 模块的代码。

运行 karma start 会将以下输出返回到控制台:

19 03 2018 17:39:11.428:WARN [karma]: No captured browser, open http://localhost:9876/
19 03 2018 17:39:11.435:INFO [karma]: Karma v2.0.0 server started at http://0.0.0.0:9876/
19 03 2018 17:39:11.436:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency
19 03 2018 17:39:11.440:INFO [launcher]: Starting browser PhantomJS
19 03 2018 17:39:12.488:INFO [PhantomJS 2.1.1 (Mac OS X 0.0.0)]: Connected on socket RSlRhr_U-N5Ge7FIAAAA with id 75824261
PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR
  {
    "message": "ReferenceError: Can't find variable: exports\nat src/index.js:3:30",
    "str": "ReferenceError: Can't find variable: exports\nat src/index.js:3:30"
  }

这是我要运行的测试:

describe('returnString', () => {
  it('should return a string', () => {
    //const myString = returnString('Miha')
    expect(true).to.equal(true)
  })
})

这是我的karma.conf.js 文件:

// Karma configuration
// Generated on Mon Mar 19 2018 16:40:36 GMT+0100 (CET)

module.exports = function (config) {
  config.set({
    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',
    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['mocha', 'chai'],
    // list of files / patterns to load in the browser
    files: [
      'src/**/*.js'
    ],
    // list of files / patterns to exclude
    exclude: [
    ],
    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
      'src/**/*.js': ['babel']
    },
    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress'],
    // web server port
    port: 9876,
    // enable / disable colors in the output (reporters and logs)
    colors: true,
    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,
    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,
    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['PhantomJS'],
    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false,
    // Concurrency level
    // how many browser should be started simultaneous
    concurrency: Infinity
  })
}

这个错误是什么意思,我该如何摆脱它?

【问题讨论】:

  • 该错误表示src/index.js的第3行第30列中引用了一个尚未声明或定义的变量。
  • 你找到解决办法了吗?我今天也遇到类似的问题

标签: javascript phantomjs karma-runner karma-mocha


【解决方案1】:

我遇到了同样的问题。原来错误是由 Babel 不正确地转译的 export 子句引起的:

export * from './somePackage'

我找到了更多信息here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 1970-01-01
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    • 2019-02-14
    • 2019-02-03
    相关资源
    最近更新 更多