Sono bloccato per un po 'cercando di ottenere il test dell'unità impostato e runnning."Errore: non corrispondente anonimo define()" con Karma + RequireJS + Jasmine
Ho un front-end AngularJS caricato con RequireJS e r.js ottimizzato per la produzione, quindi è tutto in un unico file. Funziona!
Ciò che non funziona è eseguire i test dell'unità. Non è niente di eccezionale finora, ho appena copiato un antipasto da un tutorial.
tests/user/user.test.js
:
define(['angular', 'angular-mocks', 'app'], function(angular, app) {
describe('Unit: UserController', function() {
beforeEach(module('user.app'));
it('should do something', function() {
console.log('did it');
});
});
});
poi ho un karma.conf.js
:
module.exports = function(config) {
config.set({
logLevel: config.LOG_DEBUG,
basePath: './',
files: [
// r.js'd app files
{pattern: 'dist/app.js', included: false},
// the tests
{pattern: 'tests/**/*.test.js', included: true},
],
excluded: ['tests/main.test.js']
frameworks: ['jasmine', 'requirejs'],
browsers: ['PhantomJS'],
plugins: [
'karma-jasmine',
'karma-junit-reporter',
'karma-phantomjs-launcher',
'karma-requirejs'
],
});
};
A main.test.js
:
var allTestFiles = [];
var TEST_REGEXP = /(spec|test)\.js$/i;
var pathToModule = function(path) {
var returnValue = path.replace(/^\/base\//, '').replace(/\.js$/, '');
return returnValue;
};
Object.keys(window.__karma__.files).forEach(function(file) {
if (TEST_REGEXP.test(file)) {
// Normalize paths to RequireJS module names.
allTestFiles.push(pathToModule(file));
}
});
require.config({
baseUrl: '/base',
paths: {
'angular-mocks': 'node_modules/angular-mocks/angular-mocks',
'app': 'dist/app',
},
deps: allTestFiles,
callback: function() {
console.log('load complete');
window.__karma__.start();
},
});
E infine un compito gulp
:
var gulp = require('gulp');
var karma = require('karma').server;
gulp.task('unit-test', function(done) {
karma.start({
configFile: __dirname + '/karma.conf.js',
singleRun: true
}, function() { done(); });
}
Qui e là ho aggiunto alcune uscite di registro e mostra che i file sono stati caricati.
Ma io sono bloccato ottenendo questo Error: Mismatched anonymous define()
nel tests/user/user.test.js
e non vedo cosa c'è di sbagliato. Ecco l'output: http://pastebin.com/evJPj9B3
Se il nome del modulo di test di unità come
define('namedtestorso', ['angular', 'angular-mocks', 'app'], function(angular, app) {
...
... non ottengo un errore più ma i test non vengono eseguiti. Dal momento che tutti gli esempi posso ricordare di usarlo come definizione anonima, presumo che la denominazione non sia corretta qui. Ma in questo caso l'output del registro viene eseguito mostrando che il callback di configurazione da main.test.js
viene eseguito due volte. (È un suggerimento per qualcosa?)
Sarebbe bello se qualcuno avesse un'idea su come risolverlo/farlo funzionare o anche come continuare a scoprire cosa c'è che non va. Grazie in anticipo!