我使用Karma對某些代碼運行測試。使用RequireJS文本插件
在由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>';});
附加信息
測試爲主。 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!
大概是因爲我需要將「text」添加到test-main.js的路徑部分?
require.config({
baseUrl: '/base/src',
paths: {
'text': '../node_modules/require-plugins/text/text'
},
// ...
});
但我已經試過和baseUrl
各種組合在text
路徑的路徑,我不能讓它停止404-ING。
我是否需要在'test-main.js'中的'paths'中添加一個項目? (''文本':'...') – Ben
我編輯了我的答案。 – Louis
插件駐留在'node_modules/require-plugins/text/text.js'中。但我不確定如何從測試主體內部成功地引用這個。我所有的東西都試過404s。 – Ben