0
Good afternoon,
When running the unit test in Karmajs I have as return the following errors: Using Angularjs with Requirejs, Angularamd and Instanbul.
Referenceerror: Can’t find variable: module
And when I change the line in the file test.specjs.
for beforeEach(angular.mock.module('ng-app'));
generates the following error.
Referenceerror: Can’t find variable: angular
Below follows the configuration of karma.confjs.
basePath: '',
frameworks: ['jasmine', 'requirejs'],
reporters: ['progress', 'coverage'],
exclude: [],
files: [
{ pattern: '../public/libs/jquery.min.js', included: false },
{ pattern: '../public/libs/angular/angular.js', included: false },
{ pattern: '../public/libs/angular-1.3.15/angular-mocks.js', included: false},
{pattern: '../public/app-admin/ng-controllers/**/*.js', included: false},
'test/test-main.js',
'specs/*.spec.js'
],
preprocessors: {
'**/../public/app-admin/ng-controllers/**/*.js': ['coverage']
},
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['PhantomJS'],
singleRun: false,
concurrency: Infinity,
coverageReporter: {
includeAllSources: true,
dir: 'test/coverage',
reporters: [
{ type: "html", subdir: "html" },
{ type: 'text-summary' }
]
}
Configuration of test-main.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)) {
allTestFiles.push(pathToModule(file));
}
});
require.config({
baseUrl: '/base',
paths: {
'jquery': '../../public/libs/jquery.min',
'angular': '../../public/libs/angular-1.4.3/angular',
'angular-min': '../../public/libs/angular-1.4.3/angular.min',
'angular-mock': '../../public/libs/angular-1.3.15/angular-mocks',
'angular-route': '../../public/libs/angular/angular-route',
// angularAMD libs
'angularAMD': '../../public/libs/angularAMD/angularAMD',
'ngload': '../../public/libs/angularAMD/ngload',
// angular dependencies
'ngUiRouter': '../../public/libs/angular/angular-ui-router',
'ngUiSortable': '/app-admin/helpers/sortable',
'angular-click-outside': '../../public/libs/angular-click-outside-master/clickoutside.directive',
'moment': '../../public/libs/jquery-vendors/moment-develop/min/moment.min',
// angular app
'ng-app': '/app-admin/ng-app',
// application helpers (js)
'helper': '/app-admin/helpers',
// paths to plugins to load the SRCs
'text': '../../public/libs/require/text',
'json': '../../public/libs/require/src/json',
'route': '../../public/app-admin/routes',
'jquery-vendor': '../../public/libs/jquery-vendors',
// ng-modules path (angular)
'ng-controller': '../../public/app-admin/ng-controllers',
'ng-directive': '../../public/app-admin/ng-directives',
'ng-model': '../../public/app-admin/ng-models',
'ng-factory': '../../public/app-admin/ng-factories',
'ng-service': '../../public/app-admin/ng-services',
'test': '../specs'
},
// dynamically load all test files
deps: allTestFiles,
// we have to kickoff jasmine, as it is asynchronous
callback: window.__karma__.start
});
And finally, my test.specjs.
describe("Test de spec", function () {
// load the controller's module
beforeEach(module('ng-app'));
var controller,
scope;
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
controller = $controller('headerAreaController', {
$scope: scope
});
}));
it('Teste', function () {
var a = true;
expect(a).toBe(true)
});
});
Here is the error that is shown in the console
PhantomJS 2.1.1 (Windows 8 0.0.0) ERROR
{
"message": "An error was thrown in afterAll\nReferenceError: Can't find variable: module in http://localhost:9877specs/teste.spec.js (line 4)\nhttp://localhost:9877specs/teste.spec.js:4:20\n<Jasmine>",
"str": "An error was thrown in afterAll\nReferenceError: Can't find variable: module in http://localhost:9877specs/teste.spec.js (line 4)\nhttp://localhost:9877specs/teste.spec.js:4:20\n<Jasmine>"
}
PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 0 of 0 ERROR (0.043 secs / 0 secs)
=============================== Coverage summary ===============================
Statements : 0.48% ( 89/18571 )
Branches : 0% ( 0/8490 )
Functions : 0% ( 0/2909 )
Lines : 0.48% ( 89/18555 )
================================================================================
And the other error when the line is changed beforeEach(angular.mock.module('ng-app'));
PhantomJS 2.1.1 (Windows 8 0.0.0) ERROR
{
"message": "An error was thrown in afterAll\nReferenceError: Can't find variable: angular in http://localhost:9877specs/teste.spec.js (line 4)\nhttp://localhost:9877specs/teste.spec.js:4:20\n<Jasmine>",
"str": "An error was thrown in afterAll\nReferenceError: Can't find variable: angular in http://localhost:9877specs/teste.spec.js (line 4)\nhttp://localhost:9877specs/teste.spec.js:4:20\n<Jasmine>"
}
PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 0 of 0 ERROR (0.043 secs / 0 secs)
=============================== Coverage summary ===============================
Statements : 0.48% ( 89/18571 )
Branches : 0% ( 0/8490 )
Functions : 0% ( 0/2909 )
Lines : 0.48% ( 89/18555 )
================================================================================
Thanks in advance!
the problem is probably in the Mports in your project, see here if it is the solution: https://stackoverflow.com/questions/44682587/karma-error-cant-find-variable-angular
– Melissa
Hi, I got access to this link you sent me and also did not work =/
– Danilo