【发布时间】:2017-01-20 11:30:23
【问题描述】:
我正在使用 Jasmine 和 Karma 为 Angular 1.5.7 应用程序编写测试。
我应该如何从 karma.conf.js 文件或 test-main.js 文件中引用 AngularJS,以确保它可用于我的测试?
我目前可以运行单元测试,但是当测试引用的文件引用 AngularJS(即import angular from 'angular')时,找不到 AngularJS,这表明我需要额外的配置才能使其工作。
我正在使用 babel 转译为 ES5,并将导入转换为 RequireJS AMD。
错误是:
ERROR: 'There is no timestamp for /base/angular.js!'
如果我将 AngularJS 添加到 test-main.js 的路径部分,则会收到不同的错误:
TypeError: undefined is not an object (evaluating '_angular2.default.module')
如果我从测试目标语句中删除import angular from 'angular';,则会收到以下错误:
ReferenceError: Can't find variable: angular
我的test-main:
var TEST_REGEXP = /(spec|test)\.js$/i;
var allTestFiles = [];
Object.keys(window.__karma__.files).forEach(function(file) {
if (TEST_REGEXP.test(file)) {
var normalizedTestModule = file.replace(/^\/base\/|\.js$/g, '');
allTestFiles.push(normalizedTestModule);
}
});
var cdn = 'http://localhost:55635/modules/';
require.config({
baseUrl: '/base',
initialRequire: [],
globals: {
'es6-promise': '3.2.1',
},
paths: {
'lodash': cdn + 'lodash/2.4.1/lodash'
},
shim: {},
deps: allTestFiles,
callback: window.__karma__.start
});
我的karma.conf.js:
var path = require('path');
module.exports = function(config) {
'use strict';
var cdn = 'http://localhost:55635/modules/';
var basePath = path.dirname(__filename);
config.set({
basePath: '',
frameworks: [
'requirejs',
'jasmine'
],
files: [
{
pattern: cdn + 'my-lib/version/my-lib.js',
included: false
},
{
pattern: 'test-transpiled/**',
included: false
},
/**
* This is needed because PhantomJS does not support window.Promise.
*/
'node_modules/babel-polyfill/dist/polyfill.js',
'dist/artifacts/my-lib.js',
'test/unit/test-main.js',
],
proxies: {
'/cdn/': cdn
},
exclude: [],
preprocessors: {},
reporters: ['dots'],
colors: true,
autoWatch: false,
singleRun: true,
browsers: ['PhantomJS'],
});
};
【问题讨论】:
标签: javascript angularjs jasmine karma-runner