jest js code example
Example 1: what is jest
Jest is a JavaScript testing framework maintained by Facebook, Inc. with a
focus on simplicity.
It works with projects using: Babel, TypeScript, Node.js, React, Angular and
Vue.js.
It aims to work out of the box and config free.
Example 2: jest
const sum = require('./sum');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
Copy
Example 3: jest
beforeAll(() => console.log('1 - beforeAll'));
afterAll(() => console.log('1 - afterAll'));
beforeEach(() => console.log('1 - beforeEach'));
afterEach(() => console.log('1 - afterEach'));
test('', () => console.log('1 - test'));
describe('Scoped / Nested block', () => {
beforeAll(() => console.log('2 - beforeAll'));
afterAll(() => console.log('2 - afterAll'));
beforeEach(() => console.log('2 - beforeEach'));
afterEach(() => console.log('2 - afterEach'));
test('', () => console.log('2 - test'));
});
Copy
Example 4: jest
function sum(a, b) {
return a + b;
}
module.exports = sum;
Copied