Build a mini test runner with describe, it, and expect
Implement a simple testing framework for frontend code that allows you to define test suites and test cases, and provides assertion methods for validating expected outcomes. Write the necessary functions and classes to achieve this. Popular tools such as Jest are built on the same ideas.
The framework should enable the definition of a describe function for test suites, an it function for test cases within those suites, and an expect function for making assertions within those test cases. The framework should support the following functionalities:
describe function.it function that describes the specific test.expect function and assertion methods like toExist, toBe, and toBeType.For example:
describe('Hello world', () => {
it('input exists', () => {
let input = null;
expect(input).toExist();
});
});
This should produce an output like:
beginning test suite Hello world
beginning test case input exists
failed running test suite Hello world on test case input exists with error message expected value to exist but got null
Another example:
describe('Hello world', () => {
it('input is as expected', () => {
let input = 'foo';
let expected = 'fooo';
expect(input).toBe(expected);
});
});
Should result in:
beginning test suite Hello world
beginning test case input is as expected
failed running test suite Hello world on test case input is as expected with error message expected "foo" to be "fooo"
And:
describe('Hello world', () => {
it('input is as expected', () => {
let input = 'foo';
let expected = 'string';
expect(input).toBeType(expected);
});
});
Would output:
beginning test suite Hello world
beginning test case input is as expected
failed running test suite Hello world on test case input is as expected with error message expected "foo" to be of type "string" but got "object"
This framework aims to provide a lightweight and straightforward way for developers to write and organize tests, ensuring the reliability and correctness of their frontend code.