question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NG0203 when usage TestBed on standalone component

See original GitHub issue

Which @angular/* package(s) are the source of the bug?

Don’t known / other

Is this a regression?

Yes

Description

When execute test from blank new generated standalone component, it show NG0203 instance of test pass.

Please provide a link to a minimal reproduction of the bug

https://github.com/davidNHK/ngx-components/blob/main/projects/ngx-components/src/lib/dummy/dummy.component.spec.ts

Please provide the exception or error you saw

Error: NG0203: inject() must be called from an injection context (a constructor, a factory function or a field initializer). Find more at https://angular.io/errors/NG0203
	error properties: Object({ code: -203 })
	    at injectInjectorOnly (projects/ngx-components/node_modules/@angular/core/fesm2020/core.mjs:4772:15)
	    at ɵɵinject (projects/ngx-components/node_modules/@angular/core/fesm2020/core.mjs:4783:12)
	    at Object.useFactory (projects/ngx-components/node_modules/@angular/core/fesm2020/core.mjs:29583:102)
	    at Object.factory (projects/ngx-components/node_modules/@angular/core/fesm2020/core.mjs:6983:38)
	    at R3Injector.hydrate (projects/ngx-components/node_modules/@angular/core/fesm2020/core.mjs:6896:35)
	    at R3Injector.get (projects/ngx-components/node_modules/@angular/core/fesm2020/core.mjs:6784:33)
	    at injectInjectorOnly (projects/ngx-components/node_modules/@angular/core/fesm2020/core.mjs:4779:33)
	    at ɵɵinject (projects/ngx-components/node_modules/@angular/core/fesm2020/core.mjs:4783:12)
	    at initializer (projects/ngx-components/node_modules/@angular/core/fesm2020/core.mjs:6576:65)
	    at R3Injector.resolveInjectorInitializers (projects/ngx-components/node_modules/@angular/core/fesm2020/core.mjs:6833:17)

Please provide the environment you discovered this bug in (run ng version)

_                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 14.1.3
Node: 16.14.2
Package Manager: npm 8.5.0 
OS: darwin x64

Angular: 14.1.0
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1401.3
@angular-devkit/build-angular   14.1.3
@angular-devkit/core            14.1.3
@angular-devkit/schematics      14.1.3
@angular/cli                    14.1.3
@schematics/angular             14.1.3
ng-packagr                      14.1.0
rxjs                            7.5.0
typescript                      4.7.2

Anything else?

I followed create libraries section in angular docs to create the repo

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JoostKcommented, Sep 5, 2022

It is advisable to have a single package.json file throughout the repro and/or to use npm/Yarn workspaces where compatible packages are all hoisted to the node_modules directory in the repository root (as long as dependencies are always installed from the workspace root, instead of per individual workspace).

There’s alternative approaches such as using a path mapping for @angular/* and mapping it into a single ./node_modules/@angular/* directory, but I’d try to avoid having multiple copies in the first place.

0reactions
angular-automatic-lock-bot[bot]commented, Oct 6, 2022

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

A guide to Standalone Components in Angular - Ninja Squad
Angular 14 introduces optional NgModules with the new standalone components!
Read more >
Component testing scenarios - Angular
This guide explores common component testing use cases. ... You must tell the TestBed to perform data binding by calling fixture.detectChanges() .
Read more >
Standalone components are nearly impossible to shallow test
The issue with Standalone Components is that we can't do this anymore because declaring the component in the test bed's module, doesn't work ......
Read more >
inject() must be called from an injection context (a constructor ...
To use the cookies service you musk inject it in the constructor function. ... angular/angularNG0203 when usage TestBed on standalone component#47345.
Read more >
Playing With Standalone Components / Optional Modules In ...
Ben Nadel uses the new Angular 14 standalone component / optional ... some real-world friction that can be used as a test-bed for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found