Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
Angular test does not include import from node_modules

I cannot resolve an external library via node_modules in a test in angular 2. Am I missing something?

I have a service that imports a library from node_modules:

import {Injectable} from '@angular/core';
import Lib from 'lib';
@Injectable()
export class Service { 
  stuff() {
    return Lib.doStuff();
  }
}

When I run the application, Lib is defined and I can use the library.

When I run a test on that class, the Lib is undefined.

describe('tests', () => {
  let service: Service;
  beforeEach(() => service = new Service());
  it('test', () => {
    service.stuff();
  });
});

The test itself works. If I test for a hardcoded return value it works, so the overall setups seems to be fine.

I use a project skeleton created with angular-cli. Some versions:

@angular/cli: 1.0.0
node: 6.10.2
os: linux x64
@angular/common: 4.0.2
@angular/compiler: 4.0.2
@angular/core: 4.0.2
@angular/forms: 4.0.2
@angular/http: 4.0.2
@angular/platform-browser: 4.0.2
@angular/platform-browser-dynamic: 4.0.2
@angular/router: 4.0.2
@angular/cli: 1.0.0
@angular/compiler-cli: 4.0.2

Im quite new to angular, so i have no clue what happens behind the curtains. I would expect that all imports just get resolved.

Isn't that the case?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

you can only using import Lib from 'lib' if your lib export default the Lib symbol, if not you need using one of formular such as:

import * as Lib from 'lib'

or

import {Lib} from 'lib'

learn more about import in Mozilla Developer Network

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!