Skip to content

Node is not defined when running jasmine tests with karma #2311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
elatus111 opened this issue Jan 5, 2021 · 5 comments · Fixed by #2340
Closed

Node is not defined when running jasmine tests with karma #2311

elatus111 opened this issue Jan 5, 2021 · 5 comments · Fixed by #2340

Comments

@elatus111
Copy link

Environment
Provide version numbers for the following components (information can be retrieved by running tns info in your project folder or by inspecting the package.json of the project):

  • CLI: 7.0.11
  • Cross-platform modules:
  • Android Runtime: 7.0.1
  • iOS Runtime: 7.0.0
  • Plugin(s):
  • NativeScript-Angular: 10.1.0
  • Angular: 10.1.7

Describe the bug
When running karma tests I get the error "Node is not defined". This is happening in a sandbox project and an existing project that is being upgraded from Angular/Nativescript-angular 8 to 10. I updated the imports in my test-main.ts to what it seems they should be on however the documentation has not been updated so I am not sure if they are correct. My test-main.ts is below. I was unable to only import '@nativescript/zone-js/testing.jasmine' as my tests would fail with the error that "Zone is not defined". I am able to get the error to go away by importing '@nativescript/zone-js/zone-nativescript' as well, but then I get the error that I described above. This is with node version 10.16.0.

import '@nativescript/zone-js/zone-nativescript';
import '@nativescript/zone-js/testing.jasmine';
import {nsTestBedInit} from '@nativescript/angular/testing';
nsTestBedInit();

error

To Reproduce
I created a fresh project using Angular 10 and added nativescript and i get the same issue.
Expected behavior
Tests would run correctly

Sample project

This is my sandbox project where I am getting the same error.

https://github.com/elatus111/nativescript-angular-sandbox

Additional context
Package.json below

{
"name": "angular10project",
"version": "0.0.0",
"scripts": {
"android": "ns run android --no-hmr",
"ios": "ns run ios --no-hmr",
"mobile": "ns run",
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "~10.2.3",
"@angular/common": "~10.2.3",
"@angular/compiler": "~10.2.3",
"@angular/core": "~10.2.3",
"@angular/forms": "~10.2.3",
"@angular/platform-browser": "~10.2.3",
"@angular/platform-browser-dynamic": "~10.2.3",
"@angular/router": "~10.0.14",
"@nativescript/angular": "~10.1.0",
"@nativescript/core": "~7.0.13",
"@nativescript/theme": "~2.3.0",
"@nativescript/unit-test-runner": "^1.0.2",
"reflect-metadata": "~0.1.12",
"rxjs": "~6.6.0",
"tslib": "^2.0.0",
"zone.js": "~0.10.2"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.1001.7",
"@angular/cli": "~10.1.7",
"@angular/compiler-cli": "~10.1.6",
"@nativescript/android": "7.0.1",
"@nativescript/ios": "~7.0.0",
"@nativescript/schematics": "^10.1.0",
"@nativescript/tslint-rules": "~0.0.5",
"@nativescript/types": "~7.0.0",
"@nativescript/webpack": "~3.0.0",
"@ngtools/webpack": "~10.0.0",
"@types/jasmine": "3.5.14",
"@types/jasminewd2": "~2.0.3",
"@types/node": "^13.1.1",
"codelyzer": "^6.0.0",
"jasmine-core": "~3.6.0",
"jasmine-spec-reporter": "~5.0.0",
"karma": "5.2.3",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage-istanbul-reporter": "~3.0.2",
"karma-jasmine": "4.0.1",
"karma-jasmine-html-reporter": "^1.5.0",
"karma-nativescript-launcher": "0.4.0",
"karma-webpack": "3.0.5",
"protractor": "~7.0.0",
"ts-node": "~9.1.1",
"tslint": "~6.1.0",
"typescript": "3.9.7"
},
"main": "main.tns.js"
}

@elatus111 elatus111 changed the title Node is not defined when running karma tests Node is not defined when running jasmine tests with karma Jan 5, 2021
@linuxsimba
Copy link

figured how to write unit (component, service, interceptor) tests using NG10 and NS7. read my blog about my adventures figuring it out. https://linuxsimba.github.io/nativescript-unit-tests. i hope in future NS releases unit test becomes easier and works out of the box.

@elatus111
Copy link
Author

@linuxsimba Thanks for diving into this! I've been looking into your fixes and had a few questions. I see that you created your own custom nsTestBedInit which uses the BrowserTestingModule instead of the NativeScriptTestingModule. Was this your fix for the 'Node is not defined' issue?

I saw in your blog that you mentioned you were not testing UI. Ideally I would keep using this module as my tests are using nsTestBedRender.

@edusperoni
Copy link
Collaborator

We have nailed it down to a regression in Angular which now checks for Node (which is not polyfilled). The next version of the angular integration won't use a custom zone implementation, which will improve things going forward.

To fix the Node error you can do:
class Dummy {}
global.Node = Dummy;

We already have it working and in a simpler way, so stay tuned

@dmrickey
Copy link

dmrickey commented Apr 27, 2021

@edusperoni Is there preview release or something that fixes this problem now instead of whenever it's going to be released? What was the last version that didn't have this bug? (of Angular, I don't use nativescript and am still running into this problem but can only find references to this issue on this "nativescript" repo)

@edusperoni
Copy link
Collaborator

@dmrickey you can just put this code somewhere (preferably where you init your tests):

if (typeof Node === 'undefined' && !global.Node) {
	class DummyNode {}
	global.Node = DummyNode as any;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants