2

in an app project based on Angular 8.1.2 and Ionic 4 I wrote unit tests for a simple class in typescript. That worked with "npm test" fine. To prepare for more complex classes with needed mocking I changed the code to use the beforeEach method and the TestBed object in that way:

import { CryptHelper, CryptCodeTyp, CryptIOStringTyp } from './cryptHelper';
import { TestBed } from '@angular/core/testing';

describe('CryptHelper', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [ CryptHelper ],
      imports: [ ],
      providers: [ CryptHelper ],
    }).compileComponents();
  });

  it('should be created', () => {
    let fixture = TestBed.createComponent(CryptHelper);
    let app = fixture.debugElement.componentInstance;
    expect(app).toBeDefined();
  });
});

In that way I get in the test output the error message

error properties: Object({ ngSyntaxError: true })
Error: Unexpected value 'CryptHelper' declared by the module 'DynamicTestModule'. Please add a @Pipe/@Directive/@Component annotation.
    at syntaxError (http://localhost:9876/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:2175:1)
    at http://localhost:9876/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:19889:1
    at <Jasmine>
    at CompileMetadataResolver.getNgModuleMetadata (http://localhost:9876/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:19871:1)
    at JitCompiler._loadModules (http://localhost:9876/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:25582:1)
    at JitCompiler._compileModuleAndAllComponents (http://localhost:9876/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:25571:1)
    at JitCompiler.compileModuleAndAllComponentsAsync (http://localhost:9876/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:25533:1)
    at CompilerImpl.compileModuleAndAllComponentsAsync (http://localhost:9876/_karma_webpack_/node_modules/@angular/platform-browser-dynamic/fesm2015/platform-browser-dynamic.js:237:1)
    at TestingCompilerImpl.compileModuleAndAllComponentsAsync (http://localhost:9876/_karma_webpack_/node_modules/@angular/platform-browser-dynamic/fesm2015/testing.js:140:1)
    at TestBedViewEngine.compileComponents (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/testing.js:3118:1)

I found suggests to remove the class name from declarations,

...
    TestBed.configureTestingModule({
      declarations: [ ],
...

but that leads me to this error message:

error properties: Object({ ngSyntaxError: true })
Error: Illegal state: Could not load the summary for directive CryptHelper.
    at syntaxError (http://localhost:9876/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:2175:1)
    at CompileMetadataResolver.getDirectiveSummary (http://localhost:9876/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:19729:1)
    at JitCompiler.getComponentFactory (http://localhost:9876/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:25536:1)
    at CompilerImpl.getComponentFactory (http://localhost:9876/_karma_webpack_/node_modules/@angular/platform-browser-dynamic/fesm2015/platform-browser-dynamic.js:263:30)
    at TestingCompilerImpl.getComponentFactory (http://localhost:9876/_karma_webpack_/node_modules/@angular/platform-browser-dynamic/fesm2015/testing.js:148:1)
    at TestBedViewEngine.createComponent (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/testing.js:3418:1)
    at Function.createComponent (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/testing.js:3000:1)
    at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/core/services/cryptHelper.spec.ts:15:27)
    at <Jasmine>

I found suggestions to write the class name in the declarations. But this leads me to the upper error message. So what do I wrong? Any help is welcome.

Andreas
  • 25
  • 1
  • 3

2 Answers2

1

Supposing that CryptHelper is a component:

    TestBed.configureTestingModule({
        declarations: [ CryptHelper], // Your testing component goes here
        providers: [ // Your component's providers goes here, for example a mocked service
            {
                provide: MyService, useValue: mockMyService
            }
        ],
        imports: [
            // imports from your component, for example MatDialogModule, MatInputModule, MyComponentModule, ...
        ]
    })
    .compileComponents();

Supposing that CryptHelper is a service:

let cryptHelper: CryptHelper;

beforeEach(() => {
    TestBed.configureTestingModule({
        providers: [CryptHelper],
        imports: [] 
    });

    cryptHelper = TestBed.get(CryptHelper);
});

describe('CryptHelper Service creation', () => {
    it('should be created', inject([CryptHelper], (service: CryptHelper) => {
        expect(service).toBeTruthy();
    }));
});
Pedro Bezanilla
  • 1,167
  • 14
  • 22
  • 1
    Yes it's a service. So your 2nd suggest is working. :-) One additional question. When is it useful or needed to use the inject? Since the direct use of `it('should be created', () => { ... });` works too. – Andreas Dec 12 '19 at 12:15
  • Nice :) . Regarding your question, well... that's the syntax I use when testing my services, but you can do it('should be created', () => { const service: MyService = TestBed.get(MyService); expect(service).toBeTruthy() }); and there might be other ways aswell I guess... Hope it helps ^^ – Pedro Bezanilla Dec 12 '19 at 12:32
1

As your logs say CryptHelper is NOT a component. so correct test for it would be to use it as a service (if you expect to use it like a DI element). here is working example:

describe('CryptHelper', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [ CryptHelper ],
    });
  });

  it('should be created', () => {
    let fixture = TestBed.get(CryptHelper);
    let app = fixture.debugElement.componentInstance;
    expect(app).toBeDefined();
  });
});

as u can notice TestBed.get is used instead of TestBed.createComponent

Andrei
  • 10,117
  • 13
  • 21
  • Thank you, I understood, but comparing to the other answer from Pedro B. and after testing the object fixture contains the Object already. So `let app = ...` is not working and so I marked the other solution as the right one. – Andreas Dec 12 '19 at 12:29