Sinon not working for exported function

Viewed 1364

I have a really simple JS lib (called trysinon.js) that looks like this:

export function foo() {
  bar();
}

export function bar() {
 return 2;
}

And I have the following test

import expect from 'expect';
import sinon from 'sinon';
import * as trysinon from 'trysinon';

describe('trying sinon', function() {
  beforeEach(function() {
    sinon.stub(trysinon, 'bar');
  });

  afterEach(function() {
    trysinon.bar.restore();
  });

  it('calls bar', function() {
    trysinon.foo();
    expect(trysinon.bar.called).toBe(true);
  });
});

And the test is failing. How can I ensure the test passes?

2 Answers

I use arrow function instead, and it works.

export const foo = () => {
  bar();
}

export const bar = () => {
 return 2;
}
Related