ngneat/spectator

Global Injections for NG15

issyezza opened this issue · 1 comments

Description

Sinds Angular 15 the test.ts file is not available anymore. Now it's not possible to define injections which will be available for each test. Is it possible to find a work-around or a new implementation to handle this.

Proposed solution

I'm not sure.

Alternatives considered

I looked for ways to handle this with Karma-Jasmine with no luck.

Do you want to create a pull request?

No

Although my answer is a bit late, if anyone still needs this, there is still the possibility to use the "test.ts".

You can re-create the test.ts as it was before, just remove the "context" stuff. The test.ts Angular is using can be found here:
https://github.com/angular/angular-cli/blob/adcecea1ed7b0d489fa0a5edf71bb502fa04b62e/packages/angular_devkit/build_angular/src/builders/karma/index.ts#L245

You can take this "file" and put it again in your own "test.ts" file.

Angular uses this default only when no "main" config option is set:
https://github.com/angular/angular-cli/blob/adcecea1ed7b0d489fa0a5edf71bb502fa04b62e/packages/angular_devkit/build_angular/src/builders/karma/index.ts#L118C17-L118C17

So basically:

  1. Copy the "default" test.ts form Angular into the test.ts file you want to use (per library, etc, as it was done in the past).
  2. Add the "main" property to the "options" of your "test"-target in your angular.json (or project.json if using NX) with the path to the test.ts:
    "test": {
            "executor": "@angular-devkit/build-angular:karma",
            "options": {
                "tsConfig": "libs/path/to/library/tsconfig.spec.json",
                "karmaConfig": "libs/path/to/library/karma.conf.js",
                "polyfills": ["zone.js", "zone.js/testing"],
                "main": "libs/path/to/library/src/test.ts",
                ...
        },
  1. Now you can add whatever you want again to your "test.ts" such as the spectator global injections (I do it after the getTestBed()..) or other things you want.

Good Luck!