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.

[Angular] [bug] Automatic component declaration for Angular not working with `forRoot`

See original GitHub issue

Describe the bug One of the last Storybook releases introduced a new behavior for NgModules where Storybook only declares components dynamically if they were not declared already inside of an imported NgModule (see #6666 - Thanks to @MaximSagan for his awesome work!).

This new behavior also introduces an error with NgModules imported by a static forRoot method which is a really common pattern for Angular apps:

TypeError: Cannot read property ‘value’ of undefined

I already traced the error down to the line 60 in angular helpers. I would suggest to check the importItem for an existing key ngModule to identify forRoot-imports.

Click to expand diff
  const extractNgModuleMetadata = (importItem: any): NgModule => {
+ const target = importItem && importItem.ngModule ? importItem.ngModule : importItem;
  const decoratorKey = '__annotations__';
  const decorators: any[] =
    Reflect && Reflect.getOwnPropertyDescriptor
-     ? Reflect.getOwnPropertyDescriptor(importItem, decoratorKey).value
-     : importItem[decoratorKey];
+     ? Reflect.getOwnPropertyDescriptor(target, decoratorKey).value
+     : target[decoratorKey];

    if (!decorators || decorators.length === 0) {
      return null;
    }

    const ngModuleDecorator: NgModule | undefined = decorators.find(
      decorator => decorator instanceof NgModule
    );
    if (!ngModuleDecorator) {
      return null;
    }
    return ngModuleDecorator;
  };

I can provide a PR with this fix if you’re fine with this change.

To Reproduce Repo: https://github.com/pascaliske/form-elements Branch: feature/v8 Problematic line: https://github.com/pascaliske/form-elements/blob/feature/v8/.storybook/config.ts#L24 Steps to reproduce the behavior:

  1. Clone repo at branch feature/v8
  2. Start storybook with yarn run build && yarn run storybook
  3. See error in almost every story

Expected behavior Storybook can extract Metadata from imported NgModules both with or without forRoot.

Screenshots n/a

Code snippets n/a

System:

  • OS: MacOS 10.14.5
  • Device: Macbook Pro 2019
  • Browser: chrome
  • Framework: angular
  • Addons: notes, knobs, actions, links
  • Version: v5.1.8

Additional context n/a

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
pascaliskecommented, Jul 25, 2019

The bugfix from the PR works as expected. Thanks! 🙂

0reactions
shilmancommented, Jul 25, 2019

Ooh-la-la!! I just released https://github.com/storybookjs/storybook/releases/tag/v5.2.0-beta.9 containing PR #7224 that references this issue. Upgrade today to try it out!

You can find this prerelease on the @next NPM tag.

Closing this issue. Please re-open if you think there’s still more to do.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Component is part of the declaration of 2 modules
case:1 this error will occur, once you import the component in one module and again import in sub modules will occur.
Read more >
Angular Modules and NgModule - Complete Guide
It turns out that adding Home to the declarations of HomeModule does not automatically make the component visible to any other modules that ......
Read more >
48 answers on StackOverflow to the most popular Angular ...
You need to import { REACTIVE_FORM_DIRECTIVES } from '@angular/forms' in your controller and add it to directives in @Component . That will fix ......
Read more >
ngx-spinner - npm
A library with more than 50 different loading spinners for Angular 4 - 15. (https://napster2210.github.io/ngx-spinner/).
Read more >
ModuleWithProviders Migration - Angular
export class MyModule { static forRoot(config: SomeConfig): ModuleWithProviders { return { ngModule: SomeModule, providers: [ {provide: SomeConfig, ...
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