0

I am writing unit tests for my angularJS 1.4.x app Services and Controllers, the problem is all our controllers and services are declared insides modules like shown below, i.e "MyApp"

module MyApp {

    'user strict';

     export class MyService {
          public static $inject = [
            '$http',
            '$rootScope',
            'library'
          ];
          constructor(private $rootScope:any,
                    private $http:ng.IHttpService,
                    private library:lib.Library) {}

          // some methods...
     }
}

but when i try to require this file in my test.js file it gives the following error

SyntaxError: Unexpected token, expected ";" (1:7)

here is my test file

require('../../../node_modules/angular/angular.min.js');
require('../../../node_modules/angular-mocks/angular-mocks.js');    
require('../ts/MyService.ts');

describe('myService testing', function(){

    beforeEach(
        angular.mock.module('app')
    );

    var ser;

    beforeEach(inject(function(myService) {
        ser = myService;
    }));

    it('1 + 1 should equal 2', function(){
        var actual = ser.addTwoNumbers(1,1);
        expect(actual).toEqual(2);
    });       
});
1
  • Try changing require to import Commented Jan 21, 2020 at 13:46

1 Answer 1

1

This looks like the Application is written in TypeScript, but the test is written in JavaScript.

When you run the test (as JavaScript) and require the parts that are written in TypeScript, they cannot be parsed by the JavaScript interpreter.

Compile your TypeScript to JavaScript and include the resulting files (.js) in your test, not the TypeScript source files (.ts)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.