Jest - Basic Concepts
Basic Test
// .test.js
describe('title situation here', () => {
// here we can put several tests, groupped in a way that makes sense
test("the situation that is being tested", () => {
expect(["monkey", "seal", "elephant", "dog"].length).toBe(4);
});
test("another situation that is being tested", () => {
expect(["monkey", "seal", "elephant", "dog"].length - 1).toBe('dog');
});
});Setup and Teardown
It is important to know that the functions below can be used outside or inside a describe() block. If used inside, the function will only affect the existing tests within.
// setup and teardown
beforeEach(() => {
// code here runs before each existing test
});
beforeAll(() => {
// runs once before all tests
});
afterEach(() => {
// the code runs after each test in this file
});
afterAll(() => {
// runs once after all tests
});