Skip to content

Not able to configure a module from TestBed.configureTestingModule #479

Open

Description

@tsonevn

From @ignaciolarranaga on September 28, 2016 21:3

Please, provide the details below:

Tell us about the problem

I can not configure a testing module, on the start of the tests it throws:

NativeScript / 9.1 (9.1; iPhone)  ../../tests/shared/services/user.service.spec.js at line 0 FAILED
    ReferenceError: Can't find variable: Zone
NativeScript / 9.1 (9.1; iPhone): Executed 1 of 0 (1 FAILED) ERROR (0.033 secs / 0 secs)

Which platform(s) does your issue occur on?

iOS

Please provide the following version numbers that your issue occurs with:

  • CLI: 2.3.0
  • Cross-platform modules:
    tns-core-modules: 2.3.0
  • Runtime(s): ios 2.3.0

Please tell us how to recreate the issue in as much detail as possible.

Just try to configure a test module

Is there code involved? If so, please share the minimal amount of code needed to recreate the problem.

import { TestBed } from "@angular/core/testing";
import { NativeScriptModule } from "nativescript-angular/platform";
import { NativeScriptFormsModule } from "nativescript-angular/forms"
import { NativeScriptHttpModule } from "nativescript-angular/http";
import { UserService } from "../../../shared/services";
import { IdentifiedHttp } from "../../../shared/util";

describe("UserService tests", () => {
    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [
                NativeScriptModule,
                NativeScriptFormsModule,
                NativeScriptHttpModule,
            ],
            declarations: [ UserService ],
            providers: [ IdentifiedHttp ]
        });
    });
});

Copied from original issue: NativeScript/NativeScript#2814

Activity

  1. ignaciolarranaga commented on Sep 29, 2016

    @ignaciolarranaga

    @NathanWalker I saw you were showing examples on tests during the developer day workshop (https://nativescript.github.io/developer-day-workshop/#chapter6.8) and actually being able to configure a TestBed, some clue you can give me on why it is not finding the Zone ?.

    Thanks in advance!

  2. hypery2k commented on Oct 6, 2016

    @hypery2k
    Contributor

    related to #478

  3. hypery2k commented on Oct 6, 2016

    @hypery2k
    Contributor

    try this:

    import 'nativescript-angular/zone.js/dist/zone-nativescript';
    import {BrowserDynamicTestingModule,platformBrowserDynamicTesting} from '@angular/platform-browser-dynamic/testing';
    import {NS_COMPILER_PROVIDERS} from 'nativescript-angular/platform';
    import {TestBed} from '@angular/core/testing';
    
    TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting(NS_COMPILER_PROVIDERS));
    
    

    worked for me

  4. camiloazula commented on Dec 8, 2016

    @camiloazula

    I get

    TypeError: undefined is not an object (evaluating 'ProxyZoneSpec.assertPresent') (line 178439)
    	resetFakeAsyncZone
    	attemptSync
    	run
    	execute
    	execute
    	queueRunnerFactory
    	execute
    	fn
    	attemptAsync
    	run
    	execute
    	execute
    	queueRunnerFactory
    	fn
    	attemptAsync
    	run
    	execute
    	invokeTask
    
  5. hdeshev commented on Dec 9, 2016

    @hdeshev
    Contributor

    TestBed isn't supported officially supported yet. It seems great some people are able to get it working, but we haven't put any dedicated effort into it so far.

  6. camiloazula commented on Dec 9, 2016

    @camiloazula

    is it planned to support it?

  7. hdeshev commented on Dec 9, 2016

    @hdeshev
    Contributor

    No, it isn't planned for any of the upcoming releases. We'd consider a PR if someone is willing to investigate this.

  8. camiloazula commented on Dec 11, 2016

    @camiloazula

    What way is then for now adviced to test NG2 apps?

  9. hypery2k commented on Dec 12, 2016

    @hypery2k
    Contributor

    you can take a look at my angular sample here: https://github.com/TobiasHennig/nativescript-toast/blob/master/samples/angular/app/tests/app.component.spec.ts

    I've using TestBed: https://github.com/TobiasHennig/nativescript-toast/blob/master/samples/angular/app/tests/setup.js

    you can check it for yourself, just clone the repo:

    cd samples/angular
    npm run clean && npm run build
    npm run test // only on macOS
    
  10. cboisen commented on Jul 11, 2017

    @cboisen

    @hypery2k Does this still run for you?
    I get:
    Error: Not implemented! at NativeScriptDomAdapter.setGlobalVar

    Using NativeScript 3, with Karma (and the rest of the setup like yours).

  11. kamok commented on Jul 24, 2017

    @kamok

    @hauthorn I just tried @hypery2k's config and I got the same error:

    NativeScript / 10.3.1 (10.3.1; iPhone) LoginPage should init FAILED
    	Error: Not implemented! in file:///app/tns_modules/nativescript-angular/zone-js/dist/zone-nativescript.js (line 993)
    	setGlobalVar@file:///app/tns_modules/nativescript-angular/dom-adapter.js:148:95 [ProxyZone]
    	_createNgProbe@file:///app/tns_modules/@angular/platform-browser/./bundles/platform-browser.umd.js:2383:26 [ProxyZone]
    	createInternal [ProxyZone]
    

    BTW, does your console also have all the @file and in file lines? How do I disable them, they're just noise to me.

  12. kamok commented on Jul 24, 2017

    @kamok

    @hauthorn Check this out: #733. I was able to fix that setGlobalVar method by just writing over it. Not sure if this is the right thing to do...but TestBed works for me now.

  13. Ahnert1 commented on Mar 29, 2019

    @Ahnert1

    Is TestBed supported in NativeScript? It seems like there are a bunch of potential workarounds that everyone tries, but without success. It is very important for my team to be able to use TestBed to initialize components which have numerous dependencies.

  14. NickIliev commented on Apr 15, 2019

    @NickIliev

    @Ahnert1 here you can find an example on how to configure testBed in NativeScript

  15. gsavchenko commented on Jul 24, 2019

    @gsavchenko

    Hey,

    I have been working on trying to get TestBed configured properly in NativeScript for a month now and still haven't gotten it. The example provided is not sufficient as I have attempted to reproduce it and still face issues. I am unsure of what I am doing wrong.

    Please checkout my Stack Overflow question if you have been able to get TestBed working in NativeScript I would very much appreciate it.

    https://stackoverflow.com/questions/57186925/how-to-implement-unit-tests-in-nativescript-using-testbed-and-jasmine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions