createStubInstance is not recognizing imported Angular typescript class constructor

40 views
Skip to first unread message

Rich Franzmeier

unread,
Jul 18, 2017, 11:42:44 AM7/18/17
to Sinon.JS
I have a situation where I need to create an Angular component that has services to inject.  So I add providers using createStubInstance as shown:

        TestBed.configureTestingModule({
           imports: [DashboardModule, RouterTestingModule.withRoutes([{ path:'dashboard',component:DashboardComponent}])],
           providers: [
               { provide: UserProfileService, useValue: sinon.createStubInstance(UserProfileService) },
               { provide: NavService, useValue: sinon.createStubInstance(NavService) }
           ]
       }).compileComponents();
Enter code here...
This was working fine for me and then 'something' happened and now I get the error:  The constructor should be a function.
createStubInstance@http://localhost:9876/_karma_webpack_/vendor.bundle.js:351093:69

I don't know what that 'something' is and am wondering why it fails now. I debugged into it and the constructor parm is undefined
for UserProfileService in the createStubInstance method.

Rich Franzmeier

unread,
Jul 18, 2017, 4:32:02 PM7/18/17
to Sinon.JS
This turned out to be an issue in how I was using barrels (index.ts files) for exporting.  If I imported from the exact directory it worked.  If I imported from that parent directory (which had an index.ts file which also exported the child directory) it didn't work.  Not sure why.
Reply all
Reply to author
Forward
0 new messages