Skip to content

[NG10] Testing is not working #2207

@mahmoudajawad

Description

@mahmoudajawad

Environment

  • CLI: 6.8.0
  • Cross-platform modules: 7.0.0-rc.34
  • Android Runtime: 6.5.3
  • iOS Runtime: 6.5.2
  • Plugin(s): N/A
  • NativeScript-Angular: 10.0.0
  • Angular: 10.0.0

Describe the bug
After creating the project and setting up testing per the guide on: https://docs.nativescript.org/angular/tooling/testing/testing (which works with NS6+NG8, only. Doesn't work with NS6+NG9) and running it the following is logged in the console:

ERROR in ./src/tests/example.ts
Module build failed (from ../node_modules/@ngtools/webpack/src/index.js):
Error: /path/to/project/src/tests/example.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.
    at NativeScriptAngularCompilerPlugin.getCompiledFile (/path/to/project/node_modules/@ngtools/webpack/src/angular_compiler_plugin.js:935:23)
    at NativeScriptAngularCompilerPlugin.getCompiledFile (/path/to/project/node_modules/@nativescript/webpack/plugins/NativeScriptAngularCompilerPlugin.ts:27:17)
    at /path/to/project/node_modules/@ngtools/webpack/src/loader.js:42:31
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
ℹ 「wdm」: Failed to compile.

After comparing tsconfig.tns.json from NS+NG10, and current working version of NS+NG8, I found the following lines are missing from the earlier:

  "include": [
	"src/tests/**/*.ts"
  ],

By adding the lines again to the file, the previous error message is gone, but a brand-new is coming:

System.err: An uncaught Exception occurred on "main" thread.
System.err: Unable to start activity ComponentInfo{org.nativescript.PROJECT/com.tns.NativeScriptActivity}: com.tns.NativeScriptException: Calling js method onCreate failed
System.err: Error: Building UI from XML. @bundle-app-root.xml:1:1
System.err:  > Class constructor Observable cannot be invoked without 'new'

To Reproduce

  1. Follow the guide (https://docs.nativescript.org/angular/tooling/testing/testing) to add testing to NS+NG10 project.

Even, with most basic unit testing example, the error is there:

import "reflect-metadata"; 
describe("A suite", function() {
  it("contains spec with an expectation", function() {
    expect(true).toBe(true);
  });
});

As I wanted to have more verbose test, I went ahead and setup testbed, but the same error showed up. Here is how I modified the guide from the guide to correct the imports:

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

// example.spec.ts
import { Component, ElementRef, NgZone, Renderer2 } from '@angular/core';
import { ComponentFixture, async } from '@angular/core/testing';
import { StackLayout } from '@nativescript/core';
import {
    nsTestBedAfterEach,
    nsTestBedBeforeEach,
    nsTestBedRender
} from '@nativescript/angular/testing';

@Component({
    template: `
        <StackLayout><Label text="Layout"></Label></StackLayout>
    `
})
export class ZonedRenderer {
    constructor(public elementRef: ElementRef, public renderer: Renderer2) {}
}

describe('Renderer E2E', () => {
    beforeEach(nsTestBedBeforeEach([ZonedRenderer]));
    afterEach(nsTestBedAfterEach(false));
    afterAll(() => {});

    it('executes events inside NgZone when listen is called outside NgZone', async(() => {
        const eventName = 'someEvent';
        const view = new StackLayout();
        const eventArg = { eventName, object: view };
        const callback = arg => {
            expect(arg).toEqual(eventArg);
            expect(NgZone.isInAngularZone()).toBeTruthy();
        };
        nsTestBedRender(ZonedRenderer).then(
            (fixture: ComponentFixture<ZonedRenderer>) => {
                fixture.ngZone.runOutsideAngular(() => {
                    fixture.componentInstance.renderer.listen(
                        view,
                        eventName,
                        callback
                    );

                    view.notify(eventArg);
                });
            }
        );
    }));
});

Expected behavior
Unit tests are supposed to be detected and run and return test results as it used to do with NS+NG8.

Sample project
This is happening for new projects, as well as upgraded projects. No sample is required. If the issue is not reproducible I 'll provide a project showing the issue.

Additional context
This is continuation to: #2201

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions