Viktor Shevchenko
Web developer from Kyiv
with JavaScript
➜ node -v
v10.15.3
➜ npm -v
6.9.0
➜ npm init
➜ npm install jest --save-dev
➜ jest --init
➜ npm install axios --save-dev
describe('test section name', () => {
test('what should happen in test',() => {
// your code()
expect(... your code resule ...).toBe(... expected result ...)
})
});
describe('testing async code with callbacks', () => {
test('should return valid shop id', done => {
function callback(data) {
expect(data.statusCode).toBe(200);
done();
}
getShopDetailsNodeHttp(VALID_SHOP, callback);
})
});
describe('fetching total products in shop with Promises', () => {
test('should return 54', () => {
return getProductsCount(VALID_SHOP).then(count => expect(count).toBe(54))
});
test('should return Error', () => {
expect.assertions(1);
return getProductsCount(INVALID_SHOP).catch(e => expect(e).toBe('Error: No shop found'))
});
});
describe('fetching total products in shop with Promises and resolves, rejects expects', () => {
test('should return 54', () => {
expect(getProductsCount(VALID_SHOP)).resolves.toBe(54)
});
test('should return Error', () => {
expect.assertions(1);
return expect(getProductsCount(INVALID_SHOP)).rejects.toBe('Error: No shop found');
});
});
with .resolves, .rejects expect api
describe('fetching total products in shop with async/await', () => {
test('should return 54', async () => {
const number = await getProductsCount(VALID_SHOP);
expect(number).toBe(54);
});
test('should return "Error: No shop found"', async () => {
expect.assertions(1);
try {
const number = await getProductsCount(INVALID_SHOP);
}
catch (e) {
expect(e).toBe('Error: No shop found');
}
});
});
describe.only('fetching total products in shop with async/await combined with .resolves/.rejects', () => {
test('should return 54', async () => {
await expect(getProductsCount(VALID_SHOP)).resolves.toBe(54);
});
test('should return "Error: No shop found"', async () => {
expect.assertions(1);
await expect(getProductsCount(INVALID_SHOP)).rejects.toBe('Error: No shop found');
});
});
Combined with .resolves, .rejects
describe('check server response', () => {
test('should return 200', async () => {
const response = await getShopDetails(VALID_SHOP);
expect(response.status).toBe(200);
});
test('should return 404', async () => {
expect.assertions(1);
try {
const response = await getShopDetails(INVALID_SHOP);
}
catch (e) {
expect(e.response.status).toBe(404);
}
});
});
describe.only('check server response data', () => {
test('should return valid shop id', async () => {
const response = await getShopDetails(VALID_SHOP);
expect(response.data.id).toBe(12895814);
});
});
describe.only('check server response data via shapshot', () => {
test('should return valid shop id', async () => {
const response = await getShopDetails(VALID_SHOP);
expect(response.data.toJSON()).toMatchSnapshot();
});
});
› 1 snapshot written.
Snapshot Summary
› 1 snapshot written from 1 test suite.
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[
`check server response data via shapshot should return valid shop id 1`] = `
"{
\\"id\\":12895814,
\\"name\\":\\"CalacaCeramicArt\\",
\\"title\\":\\"Unique Handmade Ceramic Teapots, Mugs, Sculptures, etc.\\",
\\"productsCount\\":54
}"
\`;
› 1 snapshot failed.
Snapshot Summary
› 1 snapshot failed from 1 test suite. Inspect your code changes or run `npm test -- -u` to update them.
Modify response body
➜ npm test -- -u
› 1 snapshot updated.
Snapshot Summary
› 1 snapshot updated from 1 test suite.
Update shapshots
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[
`check server response data via shapshot should return valid shop id 1`] = `
"{
\\"id\\":12895814,
\\"name\\":\\"CalacaCeramicArt\\",
\\"title\\":\\"Unique Handmade Ceramic Teapots, Mugs, Sculptures, etc.\\",
\\"productsCount\\":55
}"
\`;
By Viktor Shevchenko