requirejs throws a 404 trying to load a file from karma runner
javascript, karma-runner, requirejs
Solution
Hi it looks like the karma-requirejs doesn't like the resources to be loaded into the `tests` array.
Try to modify the code to exclude the helper files and it could help
var tests = [];
for (var file in window.__karma__.files) {
if (window.__karma__.files.hasOwnProperty(file)) {
// Add all spec files and helpers
if (/spec\/.+.js$/.test(file)) {
tests.push(file);
}
}
}
Problem
I am trying to load a test helper module using requirejs, but it fails even when it has been already loaded as a dependency at the start of karma run, and I can't figure out what's the problem, I always get ``` There is no timestamp for /base/spec/helpers/testHelpers!' ``` Tests run fine, inside them I require any module of the app without problem, it only fails when I require specifically something from spec folder. I have checked all karma related questions and none of them apply to these case. My karma.conf file: ``` module.exports = function (config) { 'use strict'; config.set({ basePath: '', frameworks: ['jasmine', 'requirejs'], files: [ 'bootstrapTests.js', { pattern: 'app/**/*.js', included: false }, { pattern: 'entities/**/*.js', included: false }, { pattern: 'config/**/*.js', included: false }, { pattern: 'libs/**/*.js', included: false }, { pattern: 'spec/**/*Spec.js', included: false }, { pattern: 'spec/helpers/**/*.js', included: false } ], exclude: [ 'bootstrap.js', 'bootstrap.built.js' ], preprocessors: { 'app/**/*.js': ['coverage'], 'entities/**/*.js': ['coverage'] }, coverageReporter: { reporters: [ { type: 'text-summary' } ] }, reporters: ['progress', 'coverage'], port: 9876, colors: true, logLevel: config.LOG_INFO, autoWatch: true, browsers: ['PhantomJS'], singleRun: false }); }; ``` My bootstrapTests.js file: ``` var tests = []; for (var file in window.__karma__.files) { if (window.__karma__.files.hasOwnProperty(file)) { // Add all spec files and helpers if (/spec\/.+.js$/.test(file)) { tests.push(file); } } } require.config({ baseUrl: '/base', paths: { 'backbone': 'libs/backbone', 'marionette': 'libs/backbone.marionette', 'jquery': 'libs/jquery-2.1.1', 'json2': 'libs/json2', 'underscore': 'libs/underscore', 'twig': 'libs/twig', 'editor': 'app/editor', 'widgets': 'app/widgets', 'menu': 'app/menu', 'helpers': 'spec/helpers' }, map: { '*': { 'jquery': 'config/jquery/jqueryPrivate' }, 'config/jquery/jqueryPrivate': { 'jquery': 'jquery' } }, deps: tests, callback: window.__karma__.start }); ``` so in my tests, ``` define([ 'app/app', 'backbone', 'editor/editorController', 'editor/editorApp', ], function ( myApp, Backbone, editorController ) { 'use strict'; describe('editorApp', function () { ..... ``` works like a charm, but when I try ``` define([ 'app/common/routerUtilities', 'backbone', 'helpers/testHelpers' ], function ( routerUtilities, Backbone, testHelpers ) { 'use strict'; describe('routerUtilities', function () { ... ``` it fails trying to fetch testHelpers, always get the error even when the module is already loaded. I tried specifying the path in all possible ways besides the shortened one: spec/helpers/testHelpers, /base/spec/helpers/testHelpers, /spec/helpers/testHelpers, ../../helpers/testHelpers, etc... The file is included in tests array as a dependency (checked), so it's loaded (checked with console.log) along my tests and other test setup modules: ``` LOG: ['/base/spec/app/appSpec.js', '/base/spec/app/common/routerUtilitiesSpec.js', '/base/spec/app/editor/EditorLayoutViewSpec.js', '/base/spec/app/editor/editorAppSpec.js', '/base/spec/app/editor/editorControllerSpec.js', '/base/spec/app/menu/menuControllerSpec.js', '/base/spec/helpers/envSetup.js', '/base/spec/helpers/testHelpers.js'] ``` I've checked that is included in window._ karma _.files as well (edited for brevity): ``` LOG: Object{ /base/bootstrapTests.js: 'c389a1d36d1c48f2879d434b10fd5a25b6b07758', /base/app/app.js: '7ad39554809146effd20dd1db908fc068f8119ba', /base/app/common/routerUtilities.js: '4da0e1d1794cccc727b544cacbc6d672c0d9965a', ... /base/config/marionette/renderer.js: '34a5e729f6133aa13841c5e949307cd437ca331b', /base/config/marionette/templateCache.js: '9f1901d4c6f43f5f90dadacb1c4ac179efd14b15', /base/spec/app/appSpec.js: 'e01f4fea3533e25cfcd4f7621e1c1c5559e0eed8', /base/spec/app/common/routerUtilitiesSpec.js: 'a06b9793a635633d053142dff24d1ba4427dd365', /base/spec/app/editor/EditorLayoutViewSpec.js: 'a24c7e8742e38b86d626bd6f3e45baacfa6af5eb', /base/spec/app/editor/editorAppSpec.js: '800c0e8e82840ebd0d1af07c3ec4556a24ee0b04', /base/spec/app/editor/editorControllerSpec.js: 'd2e259a477da85b6a2f742f5c6c3a4a34b7e340b', /base/spec/helpers/envSetup.js: '297aa1e59477d967aa210bece1726e8a372cb630', /base/spec/helpers/testHelpers.js: '5266065d344ca69f8f5980db16bf6d131492ebd0' ``` } The content of spec/helpers/testHelpers.js is: ``` define([ 'backbone' ], function ( Backbone ) { 'use strict'; var testHelpers = { setupRoutingTest: function () { Backbone.history.start(); Backbone.history.navigate(''); }, teardownRoutingTest: function () { Backbone.history.navigate(''); Backbone.history.stop(); } }; return testHelpers; }); ``` My folder structure is: ``` |-app |---common |---editor |---menu |---widgets |-config |---jquery |---marionette |-entities |-features |-gruntTasks |---lib |-spec |---app |-----common |-----editor |-----menu |-----widgets |---entities |---helpers |-tmp ```