in Education by
I'm trying to write tests for an angular 5 application which uses a js import, I've been doing research but i cant seen to figure out how to mock the js import. In the test, I just want to check that the bar() method has been called using a spyOn but can't figure out how to do this. import {foo} from 'fooProject'; export class Component { constructor(){} trigger() { foo.bar(); } } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
One option is you can create a factory method for foo and then register that factory in the providers for the module. Then, in your unit test, you can inject whatever you want and fake the members. import {foo} from 'fooProject'; export function fooFactory(): any { return foo; } @NgModule({ providers: [{ provide: 'Foo', useFactory: fooFactory }] }) export class YourModule { } import { Inject, Component } from '@angular/core'; export class YourComponent { constructor(@Inject('Foo') private foo:any){} trigger() { this.foo.bar(); } }

Related questions

0 votes
    I have a service with a simple GET method: export class MyService { constructor(private http: HttpClient) { ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    recently I started to work on Vue js and I want to use a javascript plugin like Isotope also I ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to split a string with angularJS (4 answers) Closed 5 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to split a string with angularJS (4 answers) Closed 5 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to split a string with angularJS (4 answers) Closed 5 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    In my tests I need to test what happens when an OracleException is thrown (due to a stored procedure ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 23, 2022 in Education by JackTerrance
0 votes
    I'm writing here since I have a question about Mat Toolbar and Mat-sidenav from Angular Material. I' ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    I want polity mocks Select the correct answer from above options...
asked Aug 3, 2022 in Education by JackTerrance
0 votes
    I need to mock a Logger in a @Service class to make tests. I want to verify if a logger message ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 1, 2022 in Education by JackTerrance
0 votes
    I'm trying to test my Category class. I'm using Mockery::mock() method, with 'overload:' prefix ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I'm trying to test my Category class. I'm using Mockery::mock() method, with 'overload:' prefix ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    So for a HW assignment, we're making a Yahtzee game. At least part of it. For preparation, our ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    How can I load a Python module given its full path? Note that the file can be anywhere in the filesystem, ... a configuration option. Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I'm trying to follow PEP 328, with the following directory structure: pkg/ __init__.py components/ Core.py __init__ ... m missing here? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
...