Sure, here is a simple README file for a testing tutorial for adding two numbers and testing it with Jest framework for GitHub:
This tutorial demonstrates how to write tests for a simple function that adds two numbers using Jest, a JavaScript testing framework.
- Create a new directory for your project and initialize it as a npm package using the following command:
npm init -y
- Install Jest using the following command:
npm install --save-dev jest
Create a file named sum.js
and add the following code:
function sum(a, b) {
return a + b;
}
module.exports = sum;
This function simply adds two numbers and returns the result.
Create a file named sum.test.js
and add the following code:
const sum = require('./sum');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
test('adds 3 + 4 to equal 7', () => {
expect(sum(3, 4)).toBe(7);
});
test('adds negative numbers -1 + -2 to equal -3', () => {
expect(sum(-1, -2)).toBe(-3);
});
These tests cover different scenarios for adding two numbers, including positive, negative, and mixed numbers.
To run the tests, open your terminal and navigate to the project directory. Then, run the following command:
npm test
Jest will execute the tests and report the results. If all tests pass, you should see the following output:
PASS ./sum.test.js
✓ adds 1 + 2 to equal 3 (2ms)
✓ adds 3 + 4 to equal 7 (1ms)
✓ adds negative numbers -1 + -2 to equal -3 (1ms)
Congratulations! You have successfully written tests for the function using Jest.