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.

Is there a way to add imports?

See original GitHub issue

When you:

  • use the NGRX Store module
  • use it inside of a feature module
  • use it in a component within that module
  • and you want to test it, then they require you to add an additional import.

It looks like this:

imports: [ 
    StoreModule.forRoot({}), // This is the extra import
    StoreModule.forFeature('yourFeature', fromFeature.reducers)
]

Without the extra import, you can’t use the Store service within your component. It complains about missing providers.

But you can’t just add StoreModule.forRoot({}) to your feature module since it would defeat the whole purpose of having StoreModule.forFeature('yourFeature', fromFeature.reducers). The extra import is only for the test.

Is there a way to add new imports?

I recall there used to be an .imports() method on the shallow class instance, but it’s no longer there.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
kevinbealcommented, Aug 7, 2018

For reference, if someone else finds this wanting to add an import, you could do it like so:

export const addImports = <T>(module: Type<T>, imports: any[]) => {
    @NgModule({
        imports: [
            ...imports,
            module,
        ]
    })
    class ModuleForSpecs {}
    Shallow.neverMock(module);
    return ModuleForSpecs;
};

new Shallow(MyComponent, addImports(MyModule, [BrowserAnimationModule]))
0reactions
kevinbealcommented, Aug 8, 2018

That makes sense to me.

I thought it would be necessary to have an .include() for the NgRx example, and I was wrong. I ended up doing it more in line with how the NgRx guys want you to write tests (for feature modules).

I have another case that may also have some simpler/better workaround that I just haven’t figured out yet.

It basically comes down to only loading BrowserAnimationModule in the root module for my actual app (because of lazy loading https://github.com/angular/angular/issues/15829) but needing it for my tests too. In this case it’s adding an import rather than just replacing one. (Or at least that’s what it seems to be).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python import: Advanced Techniques and Tips
This tutorial will provide a thorough overview of Python's import statement and how it works. The import system is powerful, and you'll learn...
Read more >
5. The import system — Python 3.11.1 documentation
The import statement is the most common way of invoking the import machinery, but it is not the only way. Functions such as...
Read more >
import - JavaScript - MDN Web Docs - Mozilla
In order to use the import declaration in a source file, the file must be interpreted by the runtime as a module. In...
Read more >
How to import modules, packages, functions, etc. in Python
In Python, you can import the standard library, packages installed with pip, your own packages, and so on, with the import statement.5.
Read more >
How To Import Modules in Python 3 - DigitalOcean
In Python, modules are accessed by using the import statement. When you do this, you execute the code of the module, keeping the...
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