【发布时间】:2017-03-16 04:30:52
【问题描述】:
我正在使用 Karma 对一些代码运行测试。
测试和代码在被 Karma 运行之前都被转译(ES6 => ES5 使用 babel)。
这很好用,测试运行良好。
但是,如果我尝试使用任何正在测试的文件中的 text! 插件...
import template from 'text!./template.html';
...我明白了:
There is no timestamp for /base/src/text.js!
Uncaught Error: Script error for "text", needed by: text!app/template.html_unnormalized2
http://requirejs.org/docs/errors.html#scripterror
Uncaught Error: Load timeout for modules: text!app/template.html_unnormalized2
有人知道为什么会这样吗?
dist 文件夹中的构建工件(即被测项目)包含成功编码的文本 RequireJS 项目,例如:
define('text!app/template.html',[],function () { return '<div>foo</div>';});
其他信息
test-main.js
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);
}
});
require.config({
baseUrl: '/base/src',
paths: {},
shim: {},
deps: allTestFiles,
callback: window.__karma__.start
});
karma.conf.js
module.exports = function(config) {
'use strict';
var path = require('path');
var cdn = 'http://localhost:55635/modules/';
var basePath = path.dirname(__filename);
config.set({
basePath: '../../..',
frameworks: [
'requirejs',
'jasmine'
],
files: [
{
pattern: path.join(basePath, 'test-transpiled', '*-spec.js'),
included: false
},
path.join(basePath, 'dist', 'artifacts', 'app.js'),
path.join(basePath, 'test', 'unit', 'test-main.js')
],
proxies: {
'/cdn/': cdn
},
exclude: [],
preprocessors: {},
reporters: ['dots'],
colors: true,
autoWatch: false,
singleRun: false,
browsers: ['Chrome'],
});
};
编辑:
我在 karma.conf.js 中添加了以下内容:
files: [
{
pattern: path.join(basePath, 'node_modules/require-plugins/text/text.js'),
included: false
},
// ...
],
我在运行测试时继续收到错误:
There is no timestamp for /base/src/text.js!
大概是因为我需要在 test-main.js 的路径部分添加“文本”?
require.config({
baseUrl: '/base/src',
paths: {
'text': '../node_modules/require-plugins/text/text'
},
// ...
});
但我尝试了baseUrl 和text 路径中的路径的各种组合,但我无法让它停止 404-ing。
【问题讨论】:
标签: javascript text requirejs karma-runner