xxxxxxxxxx
// myMath.js
const myMath = {
sum: (a, b) => a + b,
subtract: (a, b) => a - b,
};
module.exports = myMath;
xxxxxxxxxx
jest testing step by step
https://github.com/moamahfouz/jest-testing-step-by-step
xxxxxxxxxx
test('render h1 element', () => {
render(<App />);
screen.debug();
expect(screen.getByText('Hello World')).toBeInTheDocument();
});
xxxxxxxxxx
// Example of a unit test for a React component with Jest
import React from 'react';
import { render, screen } from '@testing-library/react';
import MyComponent from './MyComponent';
test('renders MyComponent', () => {
render(<MyComponent />);
const myComponentElement = screen.getByText(/My Component/i);
expect(myComponentElement).toBeInTheDocument();
});
xxxxxxxxxx
npm i jest --save -dev
-> In Package.json change the Script object(watchAll - jest will run auto)
"test" : "jest --watchAll"
-> test file should be named such way that re used by jest can recognize it
like filename.<test> or <spec> or skip it.js
Ex: sum.test.js or sum.spec.js or sum.js
-> require the target for testing file in this test file then enjoy testing.
-> visit jestWebsite-> Docs.
xxxxxxxxxx
const cal = require('../index');
test('adds 1 + 2 to equal 3', () => {
expect(cal.sum(1, 2)).toBe(3);
expect(cal.sum(1, 2)).not.toBe(4);
expect(cal.sum(1, 2)).toBeGreaterThan(2);
expect(cal.sum(1, 2)).toBeLessThan(4);
expect(cal.sum(1, 2)).toBeCloseTo(3);
// Testing datatype
expect(typeof cal.sum(1, 2)).toBe("number");
});
xxxxxxxxxx
test('Addition test', () => {
// Arrange
const a = 2;
const b = 3;
// Act
const result = a + b;
// Assert
expect(result).toBe(5);
});
xxxxxxxxxx
const cal = require('../index');
let a;
let b;
beforeEach(() => {
a = Math.floor(Math.random() * 100);
b = Math.floor(Math.random() * 100);
});
test(" testing sum " + a + " " + b + " function ", () => {
expect(cal.sum(a, b)).toBe(a + b);
expect(cal.sum(a, b)).not.toBe(a + b + 1);
expect(cal.sum(a, b)).toBeGreaterThan(a + b - 1);
expect(cal.sum(a, b)).toBeLessThan(a + b + 1);
expect(cal.sum(a, b)).toBeCloseTo(a + b);
// Testing datatype
expect(typeof cal.sum(a, b)).toBe("number");
});
test(" testing diff " + a + " " + b + " function ", () => {
expect(cal.diff(a, b)).toBe(a - b);
expect(cal.diff(a, b)).not.toBe(a - b + 1);
expect(cal.diff(a, b)).toBeGreaterThan(a - b - 1);
expect(cal.diff(a, b)).toBeLessThan(a - b + 1);
expect(cal.diff(a, b)).toBeCloseTo(a - b);
// Testing datatype
expect(typeof cal.diff(a, b)).toBe("number");
});
test(" testing mul " + a + " " + b + " function ", () => {
expect(cal.mul(a, b)).toBe(a * b);
expect(cal.mul(a, b)).not.toBe(a * b + 1);
expect(cal.mul(a, b)).toBeGreaterThan(a * b - 1);
expect(cal.mul(a, b)).toBeLessThan(a * b + 1);
expect(cal.mul(a, b)).toBeCloseTo(a * b);
// Testing datatype
expect(typeof cal.mul(a, b)).toBe("number");
});
test(" testing div " + a + " " + b + " function ", () => {
expect(cal.div(a, b)).toBe(a / b);
expect(cal.div(a, b)).not.toBe(a / b + 1);
expect(cal.div(a, b)).toBeGreaterThan(a / b - 1);
expect(cal.div(a, b)).toBeLessThan(a / b + 1);
expect(cal.div(a, b)).toBeCloseTo(a / b);
// Testing datatype
expect(typeof cal.div(a, b)).toBe("number");
});
test(" testing mod " + a + " " + b + " function ", () => {
expect(cal.mod(a, b)).toBe(a % b);
expect(cal.mod(a, b)).not.toBe(a % b + 1);
expect(cal.mod(a, b)).toBeGreaterThan(a % b - 1);
expect(cal.mod(a, b)).toBeLessThan(a % b + 1);
expect(cal.mod(a, b)).toBeCloseTo(a % b);
// Testing datatype
expect(typeof cal.mod(a, b)).toBe("number");
});
xxxxxxxxxx
import React, { useState } from "react";
const Counter = () => {
const [counter, setCounter] = useState(0);
const incrementCounter = () => {
setCounter((prevCounter) => prevCounter + 1);
};
const decrementCounter = () => {
setCounter((prevCounter) => prevCounter - 1);
};
return (
<>
<button data-testid="increment" onClick={incrementCounter}>
+
</button>
<p data-testid="counter">{counter}</p>
<button disabled data-testid="decrement" onClick={decrementCounter}>
-
</button>
</>
);
};
export default Counter;