Last active
February 25, 2021 08:52
-
-
Save linuxsimba/12dc983bd542b73e86b487ab590bf4e2 to your computer and use it in GitHub Desktop.
setup.ts file for karma testing NG10/NS7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable @typescript-eslint/restrict-template-expressions */ | |
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ | |
/* eslint-disable @typescript-eslint/no-unsafe-call */ | |
/* eslint-disable @typescript-eslint/no-unsafe-return */ | |
/* eslint-disable @typescript-eslint/no-unsafe-member-access */ | |
/* eslint-disable no-console */ | |
/* eslint-disable @typescript-eslint/no-explicit-any */ | |
/* eslint-disable @typescript-eslint/no-unsafe-assignment */ | |
/* PATH /src/tests/setup.ts */ | |
import '@nativescript/zone-js'; // only needed to prevent the error message 'Angular needs Zone.js, otherwise remove it if you get the 'Zone already loaded' message. | |
import { TestBed } from '@angular/core/testing'; | |
import { platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing'; | |
import { BrowserTestingModule } from '@angular/platform-browser/testing'; | |
export function customNsTestBedInit(): void { | |
TestBed.initTestEnvironment( | |
BrowserTestingModule, | |
platformBrowserDynamicTesting() // NS_COMPILER_PROVIDERS) | |
); | |
} | |
export function customNsTestBedAfterEach(resetEnv = true, resetFn = customNsTestBedInit): void { | |
TestBed.resetTestingModule(); | |
if (resetEnv) { | |
TestBed.resetTestEnvironment(); | |
resetFn(); | |
} | |
} | |
export function customNsTestBedBeforeEach( | |
components: any[], | |
providers: any[] = [], | |
imports: any[] = [], | |
entryComponents: any[] = []) { | |
return (done) => { | |
// If there are no entry components we can take the simple path. | |
if (entryComponents.length === 0) { | |
TestBed.configureTestingModule({ | |
declarations: [ ...components ], | |
providers: [ ...providers ], | |
imports: [ ...imports ] | |
}); | |
} | |
TestBed.compileComponents() | |
.then(() => done()) | |
.catch((e) => { | |
console.log(`Failed to instantiate test component with error: ${e}`); | |
console.log(e.stack); | |
done(); | |
}); | |
}; | |
} | |
// Note. This testing infrastructure can test components but it | |
// can only test the functions. No rendering of the dom is done | |
// Nativescript runs its own zone.js which intereferes with the | |
// zone.js code run by NG10. So my test suite completely disables | |
// the nativescript code and it should not execute anywhere. | |
// ** In testing if you inject something that calls any nativescript | |
// code it will reactivate the nativescript zone.js. make sure to stub | |
// this out so that it does not interfere with the unit test. | |
/// intialize Test bed. | |
customNsTestBedInit(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment