3
respostas

Cannot read property 'replace' of undefined

Olá, equipe de professores! Como vão? Estou tendo problemas com a função que testa o AppComponente. Ela não foi modificada e em algum momento esse erro estourou pra mim. Aparentemente ela não está conseguindo ser criada.

Karma: Failed: Invalid token specified: Cannot read property 'replace' of undefined error properties: Object({ ngDebugContext: DebugContext_({ view: Object({ def: Object({ factory: Function, nodeFlags: 50577409, rootNodeFlags: 50331649, nodeMatchedQueries: 0, flags: 0, nodes: [ Object({ nodeIndex: 0, parent: null, renderParent: null, bindingIndex: 0, outputIndex: 0, checkIndex: 0, flags: 33554433, childFlags: 49152, directChildFlags: 49152, childMatchedQueries: 0, matchedQueries: Object({ }), matchedQueryIds: 0, references: Object({ }), ngContentIndex: null, childCount: 1, bindings: [ ], bindingFlags: 0, outputs: [ ], element: Object({ ns: '', name: 'ap-header', attrs: [ ], template: null, componentProvider: Object({ nodeIndex: 1, parent: , renderParent: , bindingIndex: 0, outputIndex: 0, checkIndex: 1, flags: 49152, childFlags: 0, directChildFlags: 0, childMatchedQueries: 0, matchedQueries: Object, matchedQueryIds: 0, references: Object, ngContentIndex: -1, childCount: 0, bindings: Array, bindingFlags: 0, outputs: Array, ... at at Object../node_modules/jwt-decode/lib/index.js (http://localhost:9876/_karma_webpack_/node_modules/jwt-decode/lib/index.js:9:1) at webpack_require (http://localhost:9876/_karma_webpack_/webpack/bootstrap:79:1) at Object../src/app/core/user/user.service.ts (http://localhost:9876/_karma_webpack_/src/app/core/user/user.service.ts:2:1) at webpack_require (http://localhost:9876/_karma_webpack_/webpack/bootstrap:79:1) at Object../src/app/core/footer/footer.component.ts (http://localhost:9876/_karma_webpack_/src/app/core/footer/footer.component.ts:5:1) at webpack_require (http://localhost:9876/_karma_webpack_/webpack/bootstrap:79:1) at Object../src/app/core/core.module.ts (http://localhost:9876/_karma_webpack_/src/app/core/core.module.ts:10:1) at webpack_require (http://localhost:9876/_karma_webpack_/webpack/bootstrap:79:1) at Object../src/app/app.component.spec.ts (http://localhost:9876/_karma_webpack_/src/app/app.component.spec.ts:5:1) at webpack_require (http://localhost:9876/_karma_webpack_/webpack/bootstrap:79:1)

AppComponentSpec:

import { async, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';

import { AppComponent } from './app.component';
import { CoreModule } from './core/core.module';
import { ErrorsModule } from './errors/errors.module';
import { PhotosModule } from './photos/photos.module';
import { VMessageModule } from './shared/componets/vmessage/vmessage.module';

describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        PhotosModule,
        ErrorsModule,
        VMessageModule,
        CoreModule,
        RouterTestingModule.withRoutes([])
      ],
      declarations: [AppComponent]
    }).compileComponents();
  }));
  it('should create the app', async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  }));
});
3 respostas

Fala ai Gabriel, tudo bem? Acho que você vai precisar mockar o serviço de token, algo parecido como é feito com o userService nesse video: https://cursos.alura.com.br/course/angular-unit-test/task/71380

Espero ter ajudado.

Eu também obtive o mesmo erro.Para mim não ficou claro a resposta do instrutor. Alguém pode dar mais detalhe de como fazer a correção?

Consegui solucionar. Basta rodar: npm update