In this folder i was testing number with using toBe or not.toBe Methods
function sum(num1, num2) { return num1 + num2; } module.exports = sum
const sum = require('./sum') //Case 1 test("adds 1 + 2 to equal 3", () => { expect(sum(3 , 1)).not.toBe(5); // So in this case whenever value is 5 then test cases failed }) //Case 1 test("adds 3 + 2 to equal 5", () => { expect(sum(3 , 2)).toBe(5); }) //Case 1 test("adds 6 + 2 to equal 8", () => { expect(sum(6 , 2)).toBe(8); })
In this folder i was test object whit using toEqual and toStrictEqual Methods
Pro Tip toEqual ignores object keys with undefined properties, undefined array items, array sparseness, or object type mismatch. To take these into account use toStrictEqual instead.
function obj() { return { name: "Atul Bansal", age: undefined } } module.exports = obj;
const obj = require("./obj"); test("Object Testing", () => { expect(obj()).toEqual({ name: "Atul Bansal" }); // If you want to check the value of an object, use toEqual // You can also test for the opposite of a matcher using not: }); /* Pro TIP toEqual ignores object keys with undefined properties, undefined array items, array sparseness, or object type mismatch. To take these into account use toStrictEqual instead.
*/ test("Object Deep Testing", () => { expect(obj()).toStrictEqual({ name: "Atul Bansal",age:undefined }); });
You can check strings against regular expressions with toMatch
function string(args) { return args; } module.exports = string;const string = require('./string'); test('testing String', () => { expect(string('Hello')).toMatch("Hello") }) test('there is no I in team', () => { expect('team').not.toMatch(/I/); });
test('but there is a "stop" in Christoph', () => { expect('Christoph').toMatch(/stop/); });