Closed xirdneh closed 6 years ago
Using:
karma start karma.conf.js --single-run
When karma is compiling the source, I assume using webpack, this error is returned:
Module parse failed: Unexpected token (1:0) You may need an appropriate loader to handle this file type. | <!-- <div modal="ModalClass"> | <div class="modal-header"> | <h3 class="modal-title">Some Title</h3> @ ./project/static/scripts/applications/app.js 68:22-69 @ ./project/static/scripts/test-context.js
And it shows for every template imported as
import template from './html/template-name.html';
A webpack config such as:
// webpack.config.js module.exports = function(env, arg){ return { devtool: 'source-map', module: { rules: [ { test: /\.js$/, exclude: /node_modules/, options: { presets: ['es2015'], plugins: ['angularjs-annotate'], }, }, { test: /\.html$/, exclude: /node_modules/, use: [{ { loader: 'html-loader' } }], }, { test: /\.(s?)css$/, exclude: /node_modules/, use: [{ loader: 'css-loader', }, { loader: 'sass-loader' }], }, ], }, }; };
And a karma config such as:
//karma.config.js const webpackConfig = require('./webpack.config.js') module.exports = function(config){ config.set({ basePath: './', files: [ './project/static/scripts/test-context.js', ], frameworks: ['jasmine'], browsers: ['FirefoxHeadless', 'ChromeHeadless'], plugins: [ 'karma-webpack', 'karma-chrome-launcher', 'karma-firefox-launcher', 'karma-jasmine', ] preprocessors: { './project/static/scripts/test-context.js: ['webpack'], './project/static/scripts/**/*.spec.js: ['webpack'], } webpack: { devtool: webpackConfig.devtool, resolve: webpackConfig.resolve, module: webpackConfig.module, plugins: webpackConfig.plugins, externals: webpackConfig.externals }, }); };
With a test-context.js file such as:
test-context.js
import angular from 'angular'; import mocks from 'angular-mocks'; import './project/applications/app.js' let context = require.context('.', true, /\.spec\.js/); context.keys().forEach(context);
And importing at least one html template:
And running karma start karma.conf.js --single-run
The curious thing is that if I change the webpack configuration to export an object instead of a function it works. It only fails when using
module.exports = function(env, arg){}
for webpack config. Any idea why this is happening?
Ah, never mind. I missed the fact that it's an actual function so I had to call it.
const webpackConfig = require('./webpack.config.js')('prod');
Expected Behavior
Using:
karma start karma.conf.js --single-run
should run test correctly.Actual Behavior
When karma is compiling the source, I assume using webpack, this error is returned:
And it shows for every template imported as
Code
A webpack config such as:
And a karma config such as:
How Do We Reproduce?
With a
test-context.js
file such as:And importing at least one html template:
And running
karma start karma.conf.js --single-run
The curious thing is that if I change the webpack configuration to export an object instead of a function it works. It only fails when using
for webpack config. Any idea why this is happening?