我正在尝试在 AngularJS 中使用 Karma、Jasmine、Webpack 设置测试。我的 webpack 工作正常,我可以使用npm start
一个简单的测试工作正常,但是当我尝试添加我的 app.js 时,一切都走下坡路了。
我已经尝试了许多许多搜索和许多解决方案。请不要想,当我遇到死胡同时,我正在写这个问题。我今天整天都在谷歌上搜索并找到可能的解决方案。
有我的 karma.conf.js 文件
// Karma configuration
// Generated on Mon Sep 18 2017 09:27:36 GMT+1000 (AEST)
var webpackConfig = require('./webpack.config.js');
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
frameworks: ['jasmine'],
// ... normal karma configuration
files: [
'./node_modules/jasmine-core/lib/jasmine-core/jasmine.js',
'./node_modules/angular/angular.js', // angular
'./node_modules/angular-mocks/angular-mocks.js', // loads our modules for tests
// all files for test
'./resources/assets/js/auth/app.js',
'./resources/assets/js/account/invoices/*.spec.js',
],
preprocessors: {
// add webpack as preprocessor
'./resources/assets/js/account/invoices/*.spec.js': ['webpack'],
},
webpack: {
// webpack configuration
webpackConfig
},
webpackMiddleware: {
// webpack-dev-middleware configuration
// i. e.
stats: 'errors-only'
},
// 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
})
}
我写的测试是
describe('Unit testing Dash', function() {
var $compile,
$rootScope;
// Load the myApp module, which contains the directive
beforeEach(angular.mock.module('app'));
// Store references to $rootScope and $compile
// so they are available to all tests in this describe block
beforeEach(inject(function(_$compile_, _$rootScope_){
// The injector unwraps the underscores (_) from around the parameter names when matching
$compile = _$compile_;
$rootScope = _$rootScope_;
}));
it('Replaces the element with the appropriate content', function() {
// Compile a piece of HTML containing the directive
var element = $compile("<dash-list></dash-list>")($rootScope);
// fire all the watches, so the scope expression {{1 + 1}} will be evaluated
$rootScope.$digest();
// Check that the compiled element contains the templated content
expect(element.html()).toContain("Unpaid");
});
});
它失败并出现此错误:
18 09 2017 15:00:18.554:DEBUG [web-server]: serving (cached): project/resources/assets/js/account/invoices/dash.spec.js
18 09 2017 15:00:18.557:DEBUG [web-server]: serving (cached): project/resources/assets/js/account/invoices/add-business.spec.js
18 09 2017 15:00:18.576:DEBUG [web-server]: serving: project/node_modules/karma/static/context.js
Chrome 60.0.3112 (Mac OS X 10.12.6) ERROR
Uncaught SyntaxError: Unexpected token import
at resources/assets/js/auth/app.js:1
我正在使用 OSX。从技术上讲,webpack 应该将编译后的文件提供给 Karma。但是意外的令牌导入意味着该文件没有被编译并提供给 Karma。
请帮忙。
我什至尝试用 babel 编译,但这也没有去任何地方。我搬到了 webpack,因为我们已经构建了 webpack 配置,并且项目是使用 pages.config 编译的
更新
对于遇到这个问题的任何人,我使用了 Felix 的 OPTION #1。我只是将编译后的文件包含在 Karma 中。它就像魅力一样。
每当文件更改时,Webpack 都会自动编译文件,因此只需添加输出即可。
我的文件数组如下所示
files: [
'./node_modules/jasmine-core/lib/jasmine-core/jasmine.js',
'./node_modules/angular/angular.js', // angular
'./node_modules/angular-mocks/angular-mocks.js', // loads our modules for tests
'./resources/assets/js/account/stripe.spec.js',
'./node_modules/angular-stripe/index.js',
'./node_modules/angular-stripe-checkout/angular-stripe-checkout.js',
// all files for test
'./public/assets/vendor.bundle.js',
'http://localhost:8080/assets/account.js',
'./resources/assets/js/account/invoices/*.spec.js',
不幸的是,我不得不使用 http 作为第二个选项,但它工作正常。