我正在使用 gulp/gulp-jasmine/Angular 来运行我的单元测试。但是,我在运行 Gulp 目标时遇到以下错误:
C:\Projects\website2>gulp test
[01:53:10] Using gulpfile C:\Projects\website2\gulpfile.js
[01:53:10] Starting 'test'...
[01:53:11] Version: webpack 1.4.13
Asset Size Chunks Chunk Names
test.bundle.js 1051728 0 [emitted] test
F
Failures:
1) Exception loading: C:\Projects\website2\scripts\dist\test.bundle.js Error
1.1) ReferenceError: window is not defined
1 spec, 1 failure
Finished in 0.015 seconds
[01:53:11] 'test' errored after 916 ms
[01:53:11] Error in plugin 'gulp-jasmine'
Message:
Tests failed
我相信 gulp-jasmine 使用 PhantomJS(不会触发浏览器窗口)。有人可以帮助我解决我做错的事情吗?我是否缺少配置设置?
这是我的 gulpfile.js:
var gulp = require('gulp');
var path = require('path');
var webpack = require('gulp-webpack');
var webpackConfig = require('./webpack.config');
var testWebpackConfig = require('./test.webpack.config');
var jasmine = require('gulp-jasmine');
gulp.task('default', ['build'], function() {
});
gulp.task('build', function() {
return gulp.src(['scripts/app/**/*.js', '!scripts/app/**/*.tests.js'])
.pipe(webpack(webpackConfig))
.pipe(gulp.dest('scripts/dist'));
});
gulp.task('test', function() {
return gulp.src(['scripts/app/**/*.tests.js'])
.pipe(webpack(testWebpackConfig))
.pipe(gulp.dest('scripts/dist'))
.pipe(jasmine());
});
最佳答案
gulp-jasmine 通过 Node.js 运行测试,因此不适合客户端测试,请参阅 https://github.com/sindresorhus/gulp-jasmine/issues/46
对于客户端测试,如果您想使用 Jasmine(而不是 Karma 或并行),您可以编写一个 SpecRunner.html 文件(名称不重要)并在浏览器中运行它:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Jasmine Spec Runner</title>
<!-- You need to specify package 'jasmine-core' in your Node.js package.json -->
<link rel="shortcut icon" href="node_modules/jasmine-core/images/jasmine_favicon.png">
<link rel="stylesheet" href="node_modules/jasmine-core/lib/jasmine-core/jasmine.css">
<script src="node_modules/jasmine-core/lib/jasmine-core/jasmine.js"></script>
<script src="node_modules/jasmine-core/lib/jasmine-core/jasmine-html.js"></script>
<script src="node_modules/jasmine-core/lib/jasmine-core/boot.js"></script>
<!-- Source and Spec dependencies -->
<script src="node_modules/underscore/underscore.js"></script>
<script src="node_modules/angular/angular.js"></script>
<script src="node_modules/angular-mocks/angular-mocks.js"></script>
<!-- Source files -->
<script src="app/MySourceCode1.js"></script>
<script src="app/MySourceCode2.js"></script>
<!-- Spec files -->
<script src="test/MySourceCode1.spec.js"></script>
<script src="test/MySourceCode2.spec.js"></script>
</head>
<body>
</body>
</html>
或者使用gulp-jasmine-browser :
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
// You need to specify packages 'jasmine-core' and 'gulp-jasmine-browser'
// in your Node.js package.json
gulp.task('jasmine', function() {
return gulp.src([
'node_modules/underscore/underscore.js',
'node_modules/angular/angular.js',
'node_modules/angular-mocks/angular-mocks.js',
'app/MySourceCode1.js',
'app/MySourceCode2.js',
'test/MySourceCode1.spec.js',
'test/MySourceCode2.spec.js',
])
.pipe($.jasmineBrowser.specRunner())
.pipe($.jasmineBrowser.server());
});
或者使用Karma :)
关于angularjs - gulp-jasmine 窗口未定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27224919/