29
loading...
This website collects cookies to deliver better user experience
interface Payload {
username: string;
password: string;
}
interface Authentication {
isAuthenticated: boolean;
authenticate: (payload: Payload) => void;
}
class FakeAuthentication implements Authentication {
isAuthenticated: boolean = false;
authenticate(payload: Payload): void {
if (payload.username === 'Bob' && payload.password === 'Ross') {
this.isAuthenticated = true;
}
}
}
const fakeAuth = new FakeAuthentication();
const payload = {
username: 'Bob',
password: 'Ross'
};
it('test fakeAuth', () => {
fakeAuth.authenticate(payload);
expect(fakeAuth.isAuthenticated).toEqual(true); // ✅
});
jest.spyOn()
,interface Counter {
count: number;
getCount: () => number;
getCountText: () => string;
}
const myCounter: Counter = {
count: 0,
getCount: function () {
return this.count;
},
getCountText: function() {
const count = this.getCount();
if (count > 10) {
return 'More than 10';
} else {
return count.toString();
}
}
};
jest.spyOn(myCounter, 'getCount').mockReturnValue(20);
expect(myCounter.getCountText()).toEqual('More than 10');
getCount
method such that it will always return a value 20
no matter what.expect(mockCounter.getCount).toHaveBeenCalled(); // ✅
analytics
that contains a number of methods that look like this,// analytics.ts
const analytics = {
sendEvent: function(eventName: string) {
// send even to analytics dashboard;
},
sendButtonClickEvent: function() {
this.sendEvent('button-click');
},
sendInitEvent: function() {
this.sendEvent('init');
}
};
export default analytics;
analytics
module,jest.mock('./analytics');
test('test analytics module', () => {
const analytics = require('./analytics').default;
expect(analytics.sendEvent._isMockFunction).toEqual(true); // ✅
expect(analytics.sendButtonClickEvent._isMockFunction).toEqual(true); // ✅
expect(analytics.sendInitEvent._isMockFunction).toEqual(true); // ✅
});
jest.mock('./analytics')
in this case is an equivalent toconst analytics = {
default: {
sendEvent: jest.fn(),
sendButtonClickEvent: jest.fn().
sendInitEvent: jest.fn()
}
}
export default analytics;
jest.fn()
is a handy function that will erase the current behavior of a method and replace it with a mock object. With this, we can safely invoke analytics.sendEvent()
for testing purposes and don't have to worry about side effects.analytics.sendEvent('button-click');
analytics.sendEvent('init');
expect(analytics.sendEvent).toHaveBeenCalledTimes(2); // ✅
expect(analytics.sendEvent).toHaveBeenCalledWith('button-click'); // ✅
expect(analytics.sendEvent).toHaveBeenCalledWith('init'); // ✅
Order
class and Warehouse
class, we are more likely to be able to use real objects or stubs and perform a state verification. If it's an awkward collaboration, such as collaboration between Order
class and MailService
class, we are likely to use mock and perform a behavior verification.database.save(payload)
function is called, we can only assume that the item will be saved to the database. While with state verification, we can actually perform a query to the database to verify that the item indeed exists.jest
, test double creation is limited to jest.mock()
and jest.spyOn()
, stub and spy are treated as the same thing, and you can use mock and stub interchangeably. While in sinon
, there are sinon.stub()
, sinon.fake()
, sinon.mock()
, and sinon.spy()
.