// Employs 'mini-mocha' to emulate running in the Mocha test runner (mochajs.org)
require("@fatso83/mini-mocha").install();
const sinon = require('sinon@7.1.1'); // WORKS!
const sinon = require('sinon@7.2.0'); // FAILS!
const sinon = require('sinon'); // FAILS!
const { assert } = require('@sinonjs/referee');
const SAMPLE_USER = {
id: 1,
name: 'Kid',
age: 22,
weight: 83.5,
notes: [{ txt: 'abc' }, { txt: 'def' }, { txt: 'ghi' }],
};
class MyUser {
constructor(id, name, age, weight, notes = []) {
this.id = id;
this.name = name;
this.age = age;
this.weight = weight;
this.notes = notes;
}
}
const createFakeUser = overrides => ({
id: '007',
name: 'Bond',
...overrides,
});
class Talkie {
talk(user, topic) {
console.log('talked to', user.name, 'about', topic);
}
}
const action = (userId, talkie) => {
const { name, age, weight, notes } = SAMPLE_USER;
const user = new MyUser(userId, name, age, weight, notes);
talkie.talk(user, 'apples');
return user;
}
describe('stubbing', () => {
const talkie = new Talkie();
afterEach(() => {
sinon.restore();
});
beforeEach(() => {
sinon.spy(talkie, 'talk');
});
it('is the same user', () => {
const userA = action(1, talkie);
const userB = createFakeUser(SAMPLE_USER);
assert.equals(JSON.stringify(userA), JSON.stringify(userB));
});
it('works in 7.1.1 but fails in 7.2.0 and onwards', () => {
const userA = action(1, talkie);
const userB = createFakeUser(SAMPLE_USER);
sinon.assert.calledWith(talkie.talk, userB, sinon.match.string);
});
});