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

Using import in tests

Open depinav opened this issue 9 years ago • 3 comments

Im running into an issue when trying to import a module to test. I have an angular 1.3.15 app up working with jspm and babel to transpile es6. I installed karma-jspm to run unit tests and it works great until I try to import a module to test. Im currently importing a module with a directive on it. When I look at my console I see this error:

Error loading "app/directives/loading-spinner/loading-spinner.spec" at http://localhost:9876/base/app/directives/loading-spinner/loading-spinner.spec.js
Error loading "npm:[email protected]" at http://localhost:9876/base/jspm_packages/npm/[email protected]
Error evaluating http://localhost:9876/base/jspm_packages/npm/[email protected]
Error evaluating http://localhost:9876/base/jspm_packages/npm/[email protected]/browser.js
TypeError: 'undefined' is not a function (evaluating 're.test.bind(re)')
at undefined

My karma.conf file has a jspm object that looks like this:

jspm: {
      // Edit this to your needs
      loadFiles: ['app/**/*.spec.js'],
      serveFiles: []
    },

My jspm set up keeps config.js and jspm_packages in the default locations. Let me know if you need anymore info from me, or you have suggestions.

depinav avatar Jun 04 '15 16:06 depinav

How are you attempting to import them?

Based on what you have here I would guess that you need to add any files that you're loading to serveFiles, e.g.

jspm: {
    loadFiles: ['app/**/*.spec.js'],
    serveFiles: ['app/**/*.js']
}

It would be good to also move your test specs into a test directory so you don't have to include them twice.

Also note that jspm_packages is already included in serveFiles by default.

computmaxer avatar Jun 09 '15 19:06 computmaxer

I'm experience this exact problem, but it only happens with the PhantomJS runner. The Chrome runner works fine, so I suspect it is not directly a karma-jspm problem but I might be wrong. Did you ever get a resolution?

cmanus avatar Aug 17 '15 19:08 cmanus

@cmanus You are probably using PhantomJS 1.x, it lacks ES5 and don't work well with a lot of libs. Using karma-es5-shim resolve that for me:

npm install karma-es5-shim --save-dev

And add the es5-shim as the first framework on the karma.conf.js:

    frameworks: ['es5-shim', /* ... others */];

@depinav If you really want to use spec files on the same directory if you like can use that config:

jspm: {
    loadFiles: ['app/**/*.spec.js'],
    serveFiles ['app/**/!(*.spec).js']
}

lgvo avatar Sep 17 '15 19:09 lgvo