NativeScript / NativeScript/nativescript-cli

NativeScript - unit tests throwing errors

Ouverte
#5,277 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

unit testing
Langage dominant
JavaScript
Étoiles
1.1k
Forks
204
Merge moyen
1 j 9 h
PR mergées (30 j)
8

Description

I'm writing an app with NativeScript 6+ and Angular 8+. I'm trying to write some unit tests for my services with karma and jasmine. My unit tests are throwing errors.

How can I write my unit tests properly? I read and followed the directions on this web page but it still doesn't work: https://docs.nativescript.org/angular/tooling/testing/testing

karma.config.js

// list of files / patterns to load in the browser
files: [
  'src/tests/setup.ts',
  'src/tests/**/*.spec.ts'
],

test

import {
  nsTestBedAfterEach,
  nsTestBedBeforeEach,
  nsTestBedRender
} from 'nativescript-angular/testing';

import { HttpInterceptorService } from '../app/core/interceptors/http-interceptor-service';

describe('HttpInterceptorService', () => {

  beforeEach(nsTestBedBeforeEach([HttpInterceptorService]));
  afterEach(nsTestBedAfterEach(false));

  it('should be created', () => {
  });

  it('should have its functions defined', () => {
  });

});

Error:

NativeScript / 13.3 (13.3; iPad): Executed 15 of 17 SUCCESS (0 secs / 0.018 secs)
NativeScript / 13.3 (13.3; iPad)  undefined at line undefined FAILED
    An error was thrown in afterAll
    TypeError: undefined is not an object (evaluating 'window.location.origin') (line 108)
        at <Jasmine>
    at file:///app/vendor.js:157139:46
        at <Jasmine>
    at file:///app/vendor.js:157341:57
        at <Jasmine>
    at file:///app/bundle.js:1389:28
        at <Jasmine>
    TypeError: undefined is not an object (evaluating 'window.location.origin') (line 108)
        at <Jasmine>
    at file:///app/vendor.js:157139:46
        at <Jasmine>
    at file:///app/vendor.js:157341:57
        at <Jasmine>
    at file:///app/bundle.js:1389:28
        at <Jasmine>
NativeScript / 13.3 (13.3; iPad): Executed 16 of 17 (1 FAILED) (0.084 secs / 0.018 secs)
TOTAL: 1 FAILED, 15 SUCCESS
CONSOLE LOG file:///node_modules/nativescript-unit-test-runner/app/main-view-model.js:132:0: NSUTR: this.error: An error was thrown in afterAll
TypeError: undefined is not an object (evaluating 'window.location.origin') (line 108)
at <Jasmine>
at file:///node_modules/nativescript-angular/zone-js/dist/zone-nativescript.jasmine.js:1445:0
at <Jasmine>
at file:///node_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:188:0
at <Jasmine>
at file:///src/main.ts:11:16
at <Jasmine>
TypeError: undefined is not an object (evaluating 'window.location.origin') (line 108)
at <Jasmine>
at file:///node_modules/nativescript-angular/zone-js/dist/zone-nativescript.jasmine.js:1445:0
at <Jasmine>
at file:///node_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:188:0
at <Jasmine>
at file:///src/main.ts:11:16
at <Jasmine>
CONSOLE LOG file:///node_modules/nativescript-unit-test-runner/app/main-view-model.js:120:0: NSUTR: completed test run.
CONSOLE LOG file:///node_modules/nativescript-unit-test-runner/app/main-view-model.js:120:0: NSUTR: completed test run.
CONSOLE LOG file:///node_modules/nativescript-unit-test-runner/app/main-view-model.js:124:0: NSUTR: completeAck
CONSOLE LOG file:///node_modules/nativescript-unit-test-runner/app/main-view-model.js:124:0: NSUTR: completeAck
NativeScript / 13.3 (13.3; iPad) ERROR

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par karma.config.js, src/tests/setup.ts et la spécification de HttpInterceptorService présentée dans le rapport, puis reproduisez l’exécution des tests de NativeScript 6+/Angular 8+. Analysez l’échec de afterAll impliquant window.location.origin et comparez la configuration avec la documentation de test liée. Le travail est terminé lorsque la suite de tests s’exécute jusqu’au bout sans cette erreur.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
angular, javascript
Domaine
mobile, testing
Type d'issue
Bug
Difficulté
3/5
Temps estimé
1-2 jours
Activité
À l'abandon
Clarté
À clarifier
Accessibilité débutants
25/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.