Karma Webpack茉莉花单元测试不起作用
我正在尝试在AngularJS中使用Karma,Jasmine,Webpack进行测试.
我的webpack运行正常,可以使用npm start
I am trying to setup testing with Karma, Jasmine, Webpack in AngularJS.
My webpack is working fine and I can run my website using npm start
一个简单的测试可以正常工作,但是当我尝试添加app.js时,一切都变得艰难了.
A simple test works fine but the moment I try to add my app.js everything goes downhill.
我尝试了许多搜索和解决方案.请不要以为,当我陷入困境时,我正在写这个问题.我今天整天都在搜寻,并找到可能的解决方案.
I have tried many many many searches and many solutions. Please dont think, I am writing this question the moment I hit a deadend. I have spent all day today just googling and find possible solutions.
有我的karma.conf.js文件
There is my karma.conf.js file
// 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.但是意外的令牌 import 意味着该文件没有被编译并提供给Karma.
I am using OSX. Technically, webpack should serve the compiled file to Karma. But unexpected token import means that file is not being compiled and served to Karma.
请帮助.
我什至尝试使用babel进行编译,但这也没有任何进展.我搬到webpack的原因是我们已经建立了webpack config,并且使用webpage.config
更新
对于研究这个问题的人,我使用了Felix的OPTION#1. 我只是在Karma中包含了已编译的文件.它像魅力一样工作.
To anyone who stubble upon this question, I used the OPTION #1 by Felix. I just included the compiled files in the Karma. It worked like charm.
只要文件更改,Webpack就会自动编译文件,因此只需添加输出即可正常工作.
Webpack auto compile files whenever files are changed, so just adding the output worked fine.
我的文件数组如下
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,但是它工作正常.
要安装的业力问题:错误
karma problem to install: ERROR
bpack-2.0.3.tgz}}," 2.0.4:{" name:" karma-webpack," version:" 2.0.4,
bpack-2.0.3.tgz"}},"2.0.4":{"name":"karma-webpack","version":"2.0.4" ,
要解决此错误,请使用以下命令.它为我解决了...
To solve this error following command is useful for it. Its Solve for me...
npm缓存清理--force