Unit Testing
JavaScript Unit-Test Frameworks
Jasmine
Mocha
Jest
Jasmine
Example
Spies
Fetch Mock
Last updated
Last updated
describe('priceScrapper', () => {
let priceScrapper;
beforeEach(() => priceScrapper = new PriceScrapper());
it('should scrap prices without currency', () => {
expect(priceScrapper.scrap('10.01')).toEqual({
coefficient: 1001,
currency: null,
exponent: -2
});
});
it('should scrap prices with currency', () => {
expect(priceScrapper.scrap('$10.01')).toEqual({
coefficient: 1001,
currency: 'USD',
exponent: -2
});
});
});describe('SearchEngine', () => {
it('should pass locale to third party api', () => {
/* Spying on `thirdPartySearchApi.search` and faking result. */
spyOn(thirdPartySearchApi, 'search').and.returnValue([
{
title: 'Wishtack - Making Your Wishes Come True',
url: 'https://www.wishtack.com'
}
]);
/* Trigger search. */
searchEngine.search({keywords: 'Wishtack'});
/* Check spy's call count. */
expect(thirdPartySearchApi.search.callCount).toBe(1);
/* Check spy's call args. */
expect(thirdPartySearchApi.search).toHaveBeenCalledWith({
country: 'US',
keywords: 'Wishtack',
language: 'en'
});
});
});