user3469358
user3469358

Reputation: 121

jasmine encountered a declaration exception

Running Visual Studio I have the following jasmine test.

'use strict';

///<reference path="jasmine.js"/>
///<reference path="../../Scripts/angular.min.js"/>
///<reference path="../../Scripts/angular-route.min.js"/>
///<reference path="../../Scripts/angular-mocks.js"/>
///<reference path="../application.js"/>

describe('StatusPage Tests', function () {
    describe('Application Functions', function () {
        var location;

        beforeEach(module("StatusApplication"));

        beforeEach(inject(function($location) {
            location = $location;
        }));

        it('DetermineRootUrl_Application_RootUrl', function () {
            var result = DetermineRootUrl(location);

            var expectedResult = 'https://localhost/OK59SP1/';
            expect(expectedResult).toBe(expectedResult);
        });
    });
});

the problem seems when I try and use a angular-mock function. as soon as I include any of the beforeEach code blocks the test does not run and the only message I get back is "encountered a declaration exception"

I am not sure what I am doing wrong here, any suggestions? I checked the paths to the files referenced and they are correct.

Upvotes: 12

Views: 21709

Answers (3)

Orkhan Alikhanov
Orkhan Alikhanov

Reputation: 10050

This happened for me in jest when I used expect directly inside describe. expect should be called inside it (or whatever alias it has like xtest, test)

describe('a', () => {
   expect(1).toEqual(0); /// encountered a declaration exception

   it('b', () => {
      expect(1).toEqual(0); /// works fine
   });
});

Upvotes: 4

kobL
kobL

Reputation: 23

The references must be on top of the file (https://stackoverflow.com/a/7003383/5409756). I recommend also to create a references file with all references included. This way you have only to include one file in all the tests. (How to reference multiple files for javascript IntelliSense in VS2010)

This should work:

///<reference path="jasmine.js"/>
///<reference path="../../Scripts/angular.min.js"/>
///<reference path="../../Scripts/angular-route.min.js"/>
///<reference path="../../Scripts/angular-mocks.js"/>
///<reference path="../application.js"/>

'use strict';

describe('StatusPage Tests', function () {
    describe('Application Functions', function () {
        var location;

        beforeEach(module("StatusApplication"));

        beforeEach(inject(function($location) {
            location = $location;
        }));

        it('DetermineRootUrl_Application_RootUrl', function () {
            var result = DetermineRootUrl(location);

            var expectedResult = 'https://localhost/OK59SP1/';
            expect(expectedResult).toBe(expectedResult);
        });
    });
});

Upvotes: 1

Lev Kazakov
Lev Kazakov

Reputation: 1379

Check this out: Testacular: encountered a declaration exception

Try including angular-mocks.js in your config file

Upvotes: 3

Related Questions