I have a NativeScript Code-Sharing project. I wish to use Karma unit testing against the web version using ng test. This fails due to a .tns version of a service being wrongly imported when building the tests.
Following are steps to recreate the problem in a minimal NativeScript Code-Sharing project.
Create a new NativeScript Code-Sharing project
$ ng new -c=@nativescript/schematics ns-shared-test --sharedEnter the project folder
$ cd ns-shared-testGenerate a new service
$ ng generate service services/my-serviceGenerate a new component
$ ng generate component my-componentModify
my-component.component.tsto use my-serviceAdd at top of file:
import { MyServiceService } from '@src/app/services/my-service.service';And replace the constructor:
constructor( private myService:MyServiceService ) { }
Run unit tests
$ ng test- Observe that the test passes
Split my-service
$ cp src/app/services/my-service.service.ts src/app/services/my-service.service.tns.tsModify
my-service.service.tns.tsto use something NativeScript specificAdd at the top of file:
import { Color } from 'tns-core-modules/color/color';And replace the constructor:
constructor() { new Color('white'); }
Run unit tests
$ ng test- Observe that the test fails while loading NativeScript modules