Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global Injections for NG15 #602

Open
issyezza opened this issue Apr 6, 2023 · 1 comment
Open

Global Injections for NG15 #602

issyezza opened this issue Apr 6, 2023 · 1 comment

Comments

@issyezza
Copy link

issyezza commented Apr 6, 2023

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

@n4imLess
Copy link

n4imLess commented Jul 12, 2023

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants