karma-jspm icon indicating copy to clipboard operation
karma-jspm copied to clipboard

Uncaught SyntaxError: Unexpected token import

Open AlexisBarta opened this issue 8 years ago • 8 comments

I have the following error : Uncaught SyntaxError: Unexpected token import at path/test/BootStrapFormView.Test.js:3

Here my Karma configuration file and my package.json

karma.conf.js

// Karma configuration
// Generated on Wed Mar 02 2016 17:09:45 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: ['jspm', 'jasmine'],

    plugins: ['karma-jspm', 'karma-chrome-launcher', 'karma-jasmine'],

    jspm: {
        // Edit this to your needs
        loadFiles: ['src/**/*.js', 'test/*.js']
    },

    // list of files / patterns to load in the browser
    files: [
      'test/*'
    ],


    // list of files to exclude
    exclude: [
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
    },


    // 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_DEBUG,


    // 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: ['Chrome'],


    // 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
  })
}

package.json

{
  "name": "whatever",
  "version": "1.0.0",
  "description": "Nice Description",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Me",
  "license": "UNLICENSED",
  "devDependencies": {
    "babel-eslint": "^5.0.0",
    "eslint": "^2.2.0",
    "gulp": "^3.9.1",
    "gulp-html-replace": "^1.5.5",
    "jspm": "^0.16.30",
    "karma": "^0.13.21",
    "karma-chrome-launcher": "^0.2.2",
    "karma-jasmine": "^0.3.7",
    "karma-jspm": "^2.0.2"
  },
  "jspm": {
    "dependencies": {
      "backbone": "github:typhonjs-backbone-parse/backbone-parse-es6@master",
      "blueimp-file-upload": "npm:blueimp-file-upload@^9.12.1",
      "components/jqueryui": "github:components/jqueryui@^1.11.4",
      "jquery": "npm:jquery@^2.2.1",
      "parse": "npm:parse@^1.7.1",
      "text": "github:systemjs/plugin-text@^0.0.7",
      "typhonjs-core-parse-init": "github:typhonjs-backbone-parse/typhonjs-core-parse-init@master",
      "underscore": "npm:underscore@^1.8.3"
    },
    "devDependencies": {
      "babel": "npm:babel-core@^5.8.24",
      "babel-runtime": "npm:babel-runtime@^5.8.24",
      "core-js": "npm:core-js@^1.1.4"
    }
  }
}

AlexisBarta avatar Mar 02 '16 19:03 AlexisBarta

You also need karma-babel-preprocessor to work with ES6 via Karma. https://github.com/babel/karma-babel-preprocessor

dananichev avatar Mar 03 '16 16:03 dananichev

I forgot to put my test file

"use strict";

import BootstrapFormView    from "../src/js/views/BootstrapFormView.js";

describe("BootstrapFormView", function() {
    var bootstrapFormView = new BootstrapFormView();
  it("can convert error strings to errors", function() {
    var errorString = "|This is an error message";
    expect(bootstrapFormView.errorStringToErrors(errorString)).toBe("This is an error message");
  });
});

I installed karma-babel-preprocessor and added it to the plugging list and I still have the same issue.

AlexisBarta avatar Mar 04 '16 11:03 AlexisBarta

I'm running into this issue too. Was there any resolution?

chrisg220 avatar May 17 '16 00:05 chrisg220

I also have the same issue. Is there no solution out there?

xMarkusSpringerx avatar Sep 08 '16 09:09 xMarkusSpringerx

Sorry guys I can't help much cause I'm using Webpack now.

AlexisBarta avatar Sep 09 '16 11:09 AlexisBarta

I'm also having this issue. Anyone resolve this?

westonwatson avatar Sep 16 '16 21:09 westonwatson

@AlexisBarta I have also this error under Webpack with Karma :/, do you have to ? or did you have find a trick to fix it ? Thank you

kevincaradant avatar Sep 17 '16 16:09 kevincaradant

@AlexisBarta using webpack and babel you should be able to transpile the ES6. preprocessors: { 'tests/jasmine/**/*.js': ['webpack','babel'] }

westonwatson avatar Sep 19 '16 21:09 westonwatson