NativeScript / nativescript-angular

Integrating NativeScript with Angular
http://docs.nativescript.org/angular/tutorial/ng-chapter-0
Apache License 2.0
1.21k stars 241 forks source link

[NG10] Testing is not working #2207

Closed mahmoudajawad closed 3 years ago

mahmoudajawad commented 4 years ago

Environment

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: https://github.com/NativeScript/nativescript-angular/issues/2201

juniorschen commented 4 years ago

anxiously waiting for the tests to work again

mahmoudajawad commented 4 years ago

@juniorschen, can you check NativeScript 7 with Angular 10? I've been trying to get a time slot to testing NS7 but don't have the time. NS7 is bringing ES2017 support which is @NathanWalker explained in another issue is the reason testing NS7+NG9/10 is not working.

juniorschen commented 4 years ago

@mahmoudajawad still not working, I love being able to test, give me confidence in what I'm releasing for production. It's a little sad to have out-of-release versions that don't support testing yet ... :/

mahmoudajawad commented 4 years ago

@juniorschen, I really wish I can dedicate time to help NS team with Angular testing, but can't currently, but remains a goal for me to becoming a core contributor. I beleive NS team is already doing amazing work, and for that they deserve a hand.

On the testing topic, are you getting the same errors I listed or different ones? If same, we will know the original issue beginning NG9 is still not solved even with ES2017 targeting. If different, we might want to consider the options again.

juniorschen commented 4 years ago

@mahmoudajawad the same errors (https://github.com/NativeScript/nativescript-angular/issues/2201) in ns7/angular 10

mahmoudajawad commented 4 years ago

This, @juniorschen, tells us that there's something else to do to get testing to work again. Frankly speaking, even with NS+NG8 I had a lot of cases which broke the testing and needed to make modifications, for that, I'm hopeful that this is something that we can resolve at app level, and not at NS[7] core level.

juniorschen commented 4 years ago

@mahmoudajawad I believe that the solution does not go this way, our app would have to carry out configurations without purpose, not to mention that the possibility of problems that we cannot control is huge, I'll give you an example: To overcome the jasmine type problem I imported them in each test file (import 'jasmine';), I had overcome this problem, but with the new version this simple solution no longer works

NathanWalker commented 3 years ago

Unit test runner is back 👍 https://twitter.com/NativeScript/status/1315016431558291456

alexist commented 3 years ago

and down again with NG 11