Common Terminologies of Jest
Describe Blocks
A describe block is used for organizing test cases in logical groups of tests. For example, we want to group all the tests for a specific class.
describe('Your describe block logical name', () => {
/**Test cases comes under describe block**/
});
"it" or "test" Tests
we use the test
keyword to start a new test case definition. The it
keyword is an alias for the test keyword. Personally, I like to use test, which allows for more natural language flow of writing tests.
it('your test logical name comes here', () => {
/** test case logic***/
});
test('your test logical name comes here', () => {
/** test case logic***/
});
Matchers
A matcher is used for creating assertions in combination with the expect keyword. We want to compare the output of our test with a value we expect the function to return.
test('sum of two number', () => {
expect(2+2).toBe(4)
});
expect function is used everytime you want to test a value.you will use expect along with a "matcher" function to assert something about value
The complete list of exposed matchers can be found in the https://jestjs.io/docs/en/expect.html#tobevalue.
beforeEach
If you have some work you need to do repeatedly for many tests, you can use beforeEach
block. This blocks Runs a function before each of the tests in this file runs
beforeEach(() => {
/** logic will go here**/
});
beforeEach
If you have some work you need to do repeatedly for many tests, you can use afterEach
block. This blocks Runs a function after each of the tests in this file runs
afterEach(() => {
/** logic will go here**/
});