'sinon: How to stub an entire class, rather than just a method

I have a class under test that creates instances of another class. I want to stub out the entirety of the second class, so that its constructor never gets called. For example, If I have this setup:

Test.js

class Test {
  constructor() {
  }

  func() {
    let foo = new Foo()
    foo.hello()
  }
}

Foo.js

class Foo {
  constructor() {
    this.a = 1
    this.b = 2
    this.c = 3
    console.log('original constructor')
  }

  hello() {
    console.log('original hello')
  }

  goodbye() {
    console.log('original goodbye')
  }
}

In my test file, I want to somehow stub out the entirety of the Foo class, so that when I run my test for for Test.func() it doesn't call the original Foo constructor, but rather a stubbed constructor that returns an fake Foo object. I'll then stub the hello function of the fake Foo object to print stubbed hello instead of original hello.

How can I stub the entire class like this?

NOTE: I do NOT want to create a stub instance that I can use inside my test file. I need to stub the constructor itself, so that if something up the stack calls the constructor, it gets back a stub instance.



Solution 1:[1]

In sinon documentation:

If you want to create a stub object of MyConstructor, but don’t want the constructor to be invoked, use this utility function.

var stub = sinon.createStubInstance(MyConstructor)

http://sinonjs.org/releases/v1.17.7/stubs/

Solution 2:[2]

// A.spec.js
import { A } from './A';
import * as BClass from './B';
describe('A Test', () => {
  beforeEach(() => {
    class MockB {  // The fake B
      constructor(params) {  /* do some things */ }
      introduce() {  /* return a stub */ }
      locate() { /* do some things and return a stub */ }
    }
    sinon.stub(BClass, 'B').callsFake((args) => {
      return new MockB(args);
    }
  }
it('should assert personhood', () => { /* bla bla */ }
});

The below link helped me a lot.

https://medium.com/@kirien.eyma/mocking-imported-class-dependencies-in-sinon-js-with-typescript-8854f9c00ee

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rad
Solution 2 Debasis Mondal