1

I'm trying to write a unit test for an angular component in a hybrid app. The component in question uses "let var = angular.element (.....)" in one of its methods, and this is the code that is causing the error.

The project builds fine, and everything works as expected.

However in the spec.ts file, the default test

it('should create', () => {
    expect(component).toBeTruthy();
  });

results in Karma throwing an error of "Reference Error: Angular is not defined".

I've tried looking for a solution but was unable to find anything relevant to this (there was one question with the same error but those projects used AngularJS and the deprecated bower package). I also tried declaring the angular node modules path in test => scripts in the Angular.json file but this lead to another error where Karma would timeout. I'm wondering if this issue is hybrid app specific?

DaggeJ
  • 2,094
  • 1
  • 10
  • 22
skeffin
  • 53
  • 4

2 Answers2

0

It's hard to tell what is the problem without seeing some more of your setup, but here are a few things I did to set up angularJS testing in a hybrid app.

  • npm install angular-mocks -s
  • In tsconfig.spec.json, add angular to "types" as shown below
"compilerOptions": {
        "outDir": "../out-tsc/spec",
        "types": [
            "jasmine",
            "angular",
            "node"
        ]
  • In test.ts: import "angular-mocks";
  • In the angularJS tests: beforeEach(angular.mock.module('your-module'));

Hope this at least helps you on the way.

DaggeJ
  • 2,094
  • 1
  • 10
  • 22
0

You can add AngularJs lib files in Karma configurations. karma.conf.js has a files option where you can provide path to your AngularJs lib folder:

files: [{pattern :'PATH_TO_ANGULARJS_LIB_FOLDER/angular/*.js', watched: false}]