how to import es6 modules in karma
Asked Answered
T

0

8

I'm trying to do some testing with karma, mocha and chai. I'm confused how to import es6 module correctly in karma. I am already use karma-babel-preprocessor. Could anyone help me, thanks a lot!

Here is the error when run npm test karma error report

Here is my karma.conf.js

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['mocha', 'chai'],
    files: ['src/index.js', 'test/test_karma.js'],
    exclude: [],
    preprocessors: {
      'src/index.js': ['babel'],
      'test/test_karma.js': ['babel]
    },
    babelPreprocessor: {
      options: {
        presets: ['@babel/preset-env']
      }
    },
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['ChromeHeadless', 'Chrome'],
    singleRun: false,
    concurrency: Infinity
  })
}

Here is my index.js

const sayHello = () => {
  return 'hello karma'
}
export { sayHello }

Here is my test_karma.js

import {sayHello} from '../src/index';
import chai from 'chai';

describe('test', () => {
  it('test', () => {
    chai.assert(sayHello() === 'hello', 'say hello')
  })
})
Tenne answered 22/1, 2019 at 8:43 Comment(1)
Did you manage to fix this problem? I am having it too... #63837016Scaife

© 2022 - 2024 — McMap. All rights reserved.