Created
January 21, 2021 20:45
-
-
Save ali-kamalizade/6beac9e27462721a06fb5991b38d2f03 to your computer and use it in GitHub Desktop.
An example how to use different testing expectation helpers provided by both Jest and Jasmine.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('Jasmine & Jest expectations', () => { | |
it('can match anything if we do not care for specific parameters', () => { | |
const spyTrackEvent = spyOn(loginService, 'trackUserLoginEvent').and.callThrough(); | |
loginService.loginUser('test@example.org', 'justcoderthings'); | |
// we expect that the spied function has been called with 3 parameters: a number, the string "2021-01-21" and anything | |
expect(spyTrackEvent).toHaveBeenCalledWith(jasmine.any(Number), '2021-01-21', jasmine.anything()); | |
}); | |
it('can match partial strings (regex)', () => { | |
const spyCreateApiToken = spyOn(loginService, 'createApiToken').and.callThrough(); | |
createTokenAndTrackEvent('some-user-id'); | |
// we expect the created API token to contain "ey" | |
expect(spyCreateApiToken).toHaveBeenCalledWith(jasmine.stringMatching('ey')); | |
}); | |
it('can match partial objects', () => { | |
const spyCreateUser = spyOn(userService, 'create').and.callThrough(); | |
userService.createUser({ email: 'test@example.org', firstName: 'John', lastName: 'Doe' }); | |
// We expect the created user to have the email "test@example.org" | |
expect(spyCreateUser).toHaveBeenCalledWith(jasmine.objectContaining({ | |
email: 'test@example.org' | |
})); | |
}); | |
it('can match partial arrays', () => { | |
const spyCreateUses = spyOn(userService, 'createUsers').and.callThrough(); | |
userService.createUsers([{ email: 'test@example.org' }, { email: 'john.doe@ac.me' }]); | |
// we expect one of the created users to have the email "john.doe@ac.me" | |
expect(spyCreateUses).toHaveBeenCalledWith(jasmine.arrayContaining([{ email: 'john.doe@ac.me' }])); | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment