├── .gitignore ├── vendor └── contoh.js ├── test ├── todo.test.js ├── string.test.js ├── only.test.js ├── skip.test.js ├── number.test.js ├── equal.test.js ├── failing.test.js ├── it.test.js ├── exception.test.js ├── truthiness.test.js ├── async.test.js ├── not.test.js ├── mock-matchers.test.js ├── concurrent.test.js ├── array.test.js ├── each.test.js ├── sum.test.js ├── setup.test.js ├── scoping.test.js ├── each-object.test.js ├── mock-partial-class.test.js ├── mock-async-function.test.js ├── mock-partial-modules.test.js ├── mock-modules.test.js ├── mock-class.test.js └── mock-function.test.js ├── src ├── sayHello.js ├── exception.js ├── database.js ├── product-service.js ├── user-repository.js ├── async.js ├── user-service.js └── sum.js ├── babel.config.json └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | coverage 3 | node_modules 4 | -------------------------------------------------------------------------------- /vendor/contoh.js: -------------------------------------------------------------------------------- 1 | function contohVendor(){ 2 | console.info("Hello Vendor"); 3 | } 4 | -------------------------------------------------------------------------------- /test/todo.test.js: -------------------------------------------------------------------------------- 1 | test.todo("Create test for sumAll() with big numbers"); 2 | 3 | test.todo("Create test for sumAll() with negative numbers"); 4 | -------------------------------------------------------------------------------- /src/sayHello.js: -------------------------------------------------------------------------------- 1 | export const sayHello = (name) => { 2 | if (name) { 3 | return `Hello ${name}`; 4 | } else { 5 | throw new Error("Name is required"); 6 | } 7 | }; 8 | -------------------------------------------------------------------------------- /test/string.test.js: -------------------------------------------------------------------------------- 1 | test("string", () => { 2 | const name = "Eko Kurniawan Khannedy"; 3 | 4 | expect(name).toBe("Eko Kurniawan Khannedy"); 5 | expect(name).toMatch(/awan/); 6 | }); 7 | -------------------------------------------------------------------------------- /babel.config.json: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "@babel/preset-env" 4 | ], 5 | "plugins": [ 6 | [ 7 | "@babel/plugin-transform-runtime", 8 | { 9 | "regenerator": true 10 | } 11 | ] 12 | ] 13 | } 14 | -------------------------------------------------------------------------------- /test/only.test.js: -------------------------------------------------------------------------------- 1 | test("test 1", () => console.info("test 1")); 2 | test("test 2", () => console.info("test 2")); 3 | test.only("test 3", () => console.info("test 3")); 4 | test("test 4", () => console.info("test 4")); 5 | test("test 5", () => console.info("test 5")); 6 | -------------------------------------------------------------------------------- /test/skip.test.js: -------------------------------------------------------------------------------- 1 | test("test 1", () => console.info("test 1")); 2 | test("test 2", () => console.info("test 2")); 3 | test.skip("test 3", () => console.info("test 3")); 4 | test("test 4", () => console.info("test 4")); 5 | test("test 5", () => console.info("test 5")); 6 | -------------------------------------------------------------------------------- /src/exception.js: -------------------------------------------------------------------------------- 1 | export class MyException extends Error { 2 | 3 | } 4 | 5 | export const callMe = (name) => { 6 | if (name === "Eko") { 7 | throw new MyException("Ups my exceptions happens"); 8 | } else { 9 | return "OK"; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /src/database.js: -------------------------------------------------------------------------------- 1 | export const getProductById = (id) => { 2 | // select * from products where id = ${id} 3 | throw new Error("Not Implemented"); 4 | }; 5 | 6 | export const getAllProducts = () => { 7 | // select * from products 8 | throw new Error("Not Implemented"); 9 | }; 10 | -------------------------------------------------------------------------------- /src/product-service.js: -------------------------------------------------------------------------------- 1 | import {getAllProducts, getProductById} from "./database.js"; 2 | 3 | export class ProductService { 4 | 5 | static findById(id) { 6 | return getProductById(id); 7 | } 8 | 9 | static findAll() { 10 | return getAllProducts(); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/user-repository.js: -------------------------------------------------------------------------------- 1 | export class UserRepository { 2 | 3 | save(user) { 4 | throw new Error("Not implemented"); 5 | } 6 | 7 | findById(id) { 8 | throw new Error("Not implemented"); 9 | } 10 | 11 | findAll() { 12 | throw new Error("Not implemented"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /test/number.test.js: -------------------------------------------------------------------------------- 1 | test("numbers", () => { 2 | 3 | const value = 2 + 2; 4 | 5 | expect(value).toBeGreaterThan(3); 6 | expect(value).toBeGreaterThanOrEqual(4); 7 | 8 | expect(value).toBeLessThan(5); 9 | expect(value).toBeLessThanOrEqual(4); 10 | 11 | expect(value).toBe(4); 12 | 13 | }); 14 | -------------------------------------------------------------------------------- /test/equal.test.js: -------------------------------------------------------------------------------- 1 | test("test toBe", () => { 2 | const name = "Eko"; 3 | const hello = `Hello ${name}`; 4 | 5 | expect(hello).toBe('Hello Eko'); 6 | }); 7 | 8 | test("test toEqual", () => { 9 | let person = {id: "eko"}; 10 | Object.assign(person, {name: "Eko"}); 11 | 12 | expect(person).toEqual({id: "eko", name: "Eko"}); 13 | }) 14 | -------------------------------------------------------------------------------- /test/failing.test.js: -------------------------------------------------------------------------------- 1 | import {sayHello} from "../src/sayHello.js"; 2 | 3 | test("sayHello success", () => { 4 | expect(sayHello("Eko")).toBe("Hello Eko"); 5 | }); 6 | 7 | test.failing("sayHello error", () => { 8 | sayHello(null); 9 | }); 10 | 11 | test("sayHello error matchers", () => { 12 | expect(() => sayHello(null)).toThrow(); 13 | }); 14 | -------------------------------------------------------------------------------- /test/it.test.js: -------------------------------------------------------------------------------- 1 | import {sumAll} from "../src/sum.js"; 2 | 3 | describe("when call sumAll()", () => { 4 | it("should get 30 with parameter [10, 10, 10]", () => { 5 | expect(sumAll([10, 10, 10])).toBe(30); 6 | }); 7 | it("should get 50 with parameter [10, 10, 10, 10, 10]", () => { 8 | expect(sumAll([10, 10, 10, 10, 10])).toBe(50); 9 | }); 10 | }); 11 | -------------------------------------------------------------------------------- /test/exception.test.js: -------------------------------------------------------------------------------- 1 | import {callMe, MyException} from "../src/exception.js"; 2 | 3 | test("exception", () => { 4 | expect(() => callMe("Eko")).toThrow(); 5 | expect(() => callMe("Eko")).toThrow(MyException); 6 | expect(() => callMe("Eko")).toThrow("Ups my exceptions happens"); 7 | }); 8 | 9 | test("exception not happens", () => { 10 | expect(callMe("Budi")).toBe("OK"); 11 | }); 12 | -------------------------------------------------------------------------------- /test/truthiness.test.js: -------------------------------------------------------------------------------- 1 | test("truthiness", () => { 2 | 3 | let value = null; 4 | expect(value).toBeNull(); 5 | expect(value).toBeDefined(); 6 | expect(value).toBeFalsy(); 7 | 8 | value = undefined; 9 | expect(value).toBeUndefined(); 10 | expect(value).toBeFalsy(); 11 | 12 | value = "Eko"; 13 | expect(value).toBeTruthy(); 14 | expect(value).toBe("Eko"); 15 | 16 | }); 17 | -------------------------------------------------------------------------------- /test/async.test.js: -------------------------------------------------------------------------------- 1 | import {sayHelloAsync} from "../src/async.js"; 2 | 3 | test("test async function", async () => { 4 | const result = await sayHelloAsync("Eko"); 5 | expect(result).toBe("Hello Eko"); 6 | }); 7 | 8 | test("test async matchers", async () => { 9 | await expect(sayHelloAsync("Eko")).resolves.toBe("Hello Eko"); 10 | await expect(sayHelloAsync()).rejects.toBe("Name is empty"); 11 | }); 12 | -------------------------------------------------------------------------------- /test/not.test.js: -------------------------------------------------------------------------------- 1 | test("string.not", () => { 2 | const name = "Eko Kurniawan Khannedy"; 3 | 4 | expect(name).not.toBe("Joko"); 5 | expect(name).not.toEqual("Joko"); 6 | expect(name).not.toMatch(/joko/); 7 | }); 8 | 9 | test("number.not", () => { 10 | const value = 2 + 2; 11 | 12 | expect(value).not.toBeGreaterThan(6); 13 | expect(value).not.toBeLessThan(3); 14 | expect(value).not.toBe(10); 15 | }); 16 | -------------------------------------------------------------------------------- /test/mock-matchers.test.js: -------------------------------------------------------------------------------- 1 | import {calculate} from "../src/sum.js"; 2 | 3 | test("test mock matchers", () => { 4 | const callback = jest.fn(); 5 | 6 | calculate([10, 10, 10], callback); 7 | calculate([10, 10, 10, 10, 10], callback); 8 | 9 | expect(callback).toHaveBeenCalled(); 10 | expect(callback).toHaveBeenCalledTimes(2); 11 | expect(callback).toHaveBeenCalledWith(30); 12 | expect(callback).toHaveBeenCalledWith(50); 13 | }); 14 | -------------------------------------------------------------------------------- /test/concurrent.test.js: -------------------------------------------------------------------------------- 1 | import {sayHelloAsync} from "../src/async.js"; 2 | 3 | test.concurrent("concurrent 1", async () => { 4 | await expect(sayHelloAsync("Eko")).resolves.toBe("Hello Eko"); 5 | }); 6 | 7 | test.concurrent("concurrent 2", async () => { 8 | await expect(sayHelloAsync("Eko")).resolves.toBe("Hello Eko"); 9 | }); 10 | 11 | test.concurrent("concurrent 3", async () => { 12 | await expect(sayHelloAsync("Eko")).resolves.toBe("Hello Eko"); 13 | }); 14 | -------------------------------------------------------------------------------- /test/array.test.js: -------------------------------------------------------------------------------- 1 | test("array simple", () => { 2 | const names = ["eko", "kurniawan", "khannedy"]; 3 | expect(names).toEqual(["eko", "kurniawan", "khannedy"]); 4 | expect(names).toContain("eko"); 5 | }); 6 | 7 | test("array object", () => { 8 | const persons = [ 9 | { 10 | name: "Eko" 11 | }, 12 | { 13 | name: "Budi" 14 | } 15 | ]; 16 | expect(persons).toContainEqual({ 17 | name: "Eko" 18 | }); 19 | }); 20 | -------------------------------------------------------------------------------- /src/async.js: -------------------------------------------------------------------------------- 1 | export const sayHelloAsync = (name) => { 2 | return new Promise((resolve, reject) => { 3 | setTimeout(() => { 4 | if (name) { 5 | resolve(`Hello ${name}`); 6 | } else { 7 | reject("Name is empty"); 8 | } 9 | }, 1000); 10 | }); 11 | }; 12 | 13 | export const getBalance = async (name, from) => { 14 | const balance = await from(); 15 | return { 16 | name: name, 17 | balance: balance 18 | }; 19 | }; 20 | -------------------------------------------------------------------------------- /test/each.test.js: -------------------------------------------------------------------------------- 1 | import {sumAll} from "../src/sum.js"; 2 | 3 | const table = [ 4 | [ 5 | [], 6 | 0 7 | ], 8 | [ 9 | [10], 10 | 10 11 | ], 12 | [ 13 | [10, 10, 10], 14 | 30 15 | ], 16 | [ 17 | [10, 10, 10, 10, 10], 18 | 50 19 | ], 20 | [ 21 | [10, 10, 10, 10, 10, 10, 10], 22 | 70 23 | ] 24 | ]; 25 | 26 | test.each(table)("test sumAll(%s) should result %i", (numbers, expected) => { 27 | expect(sumAll(numbers)).toBe(expected); 28 | }) 29 | -------------------------------------------------------------------------------- /test/sum.test.js: -------------------------------------------------------------------------------- 1 | import {sum, sumAll} from "../src/sum.js"; 2 | 3 | test("test sum function 1", () => { 4 | 5 | const result = sum(1, 2); 6 | 7 | expect(result).toBe(3); 8 | 9 | }); 10 | 11 | test("test sum function 2", () => { 12 | 13 | const result = sum(1, 2); 14 | 15 | expect(result).toBe(3); 16 | 17 | }); 18 | 19 | test("test sum function 3", () => { 20 | 21 | const result = sum(1, 2); 22 | 23 | expect(result).toBe(3); 24 | 25 | }); 26 | 27 | test("test sum all", () => { 28 | const numbers = [1,1,1,1,1]; 29 | expect(sumAll(numbers)).toBe(5); 30 | }); 31 | -------------------------------------------------------------------------------- /test/setup.test.js: -------------------------------------------------------------------------------- 1 | import {sum} from "../src/sum.js"; 2 | 3 | beforeAll(async () => { 4 | console.info("Before All"); 5 | }); 6 | 7 | afterAll(async () => { 8 | console.info("After All"); 9 | }); 10 | 11 | beforeEach(async () => { 12 | console.info("Before Each"); 13 | }); 14 | 15 | afterEach(async () => { 16 | console.info("After Each"); 17 | }); 18 | 19 | test("first test", async () => { 20 | expect(sum(10, 10)).toBe(20); 21 | console.info("First Test"); 22 | }) 23 | 24 | test("second test", () => { 25 | expect(sum(10, 10)).toBe(20); 26 | console.info("Second Test"); 27 | }) 28 | -------------------------------------------------------------------------------- /src/user-service.js: -------------------------------------------------------------------------------- 1 | import {UserRepository} from "./user-repository.js"; 2 | 3 | export class UserService { 4 | 5 | constructor(userRepository) { 6 | if (userRepository) { 7 | this.userRepository = userRepository; 8 | } else { 9 | this.userRepository = new UserRepository(); 10 | } 11 | } 12 | 13 | save(user) { 14 | this.userRepository.save(user); 15 | } 16 | 17 | findById(id) { 18 | return this.userRepository.findById(id); 19 | } 20 | 21 | findAll() { 22 | return this.userRepository.findAll(); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /test/scoping.test.js: -------------------------------------------------------------------------------- 1 | beforeAll(() => console.info("Before All Outer")); 2 | afterAll(() => console.info("After All Outer")); 3 | beforeEach(() => console.info("Before Each Outer")); 4 | afterEach(() => console.info("After Each Outer")); 5 | 6 | test("Test Outer", () => console.info("Test Outer")); 7 | 8 | describe("Inner", () => { 9 | 10 | beforeAll(() => console.info("Before All Inner")); 11 | afterAll(() => console.info("After All Inner")); 12 | beforeEach(() => console.info("Before Each Inner")); 13 | afterEach(() => console.info("After Each Inner")); 14 | 15 | test("Test Inner", () => console.info("Test Inner")); 16 | 17 | }); 18 | -------------------------------------------------------------------------------- /src/sum.js: -------------------------------------------------------------------------------- 1 | export const sum = (first, second) => { 2 | return first + second; 3 | } 4 | 5 | export const sumAll = (numbers) => { 6 | let total = 0; 7 | for (let number of numbers) { 8 | total += number; 9 | } 10 | return total; 11 | } 12 | 13 | export const calculate = (numbers, callback) => { 14 | let total = 0; 15 | for (let number of numbers) { 16 | total += number; 17 | } 18 | callback(total); 19 | } 20 | 21 | export const calculateAndReturn = (numbers, callback) => { 22 | let total = 0; 23 | for (let number of numbers) { 24 | total += number; 25 | } 26 | return callback(total); 27 | } 28 | -------------------------------------------------------------------------------- /test/each-object.test.js: -------------------------------------------------------------------------------- 1 | import {sumAll} from "../src/sum.js"; 2 | 3 | const table = [ 4 | { 5 | numbers: [], 6 | expected: 0 7 | }, 8 | { 9 | numbers: [10], 10 | expected: 10 11 | }, 12 | { 13 | numbers: [10, 10, 10], 14 | expected: 30 15 | }, 16 | { 17 | numbers: [10, 10, 10, 10, 10], 18 | expected: 50 19 | }, 20 | { 21 | numbers: [10, 10, 10, 10, 10, 10, 10], 22 | expected: 70 23 | }, 24 | ]; 25 | 26 | test.each(table)("test sumAll($numbers) should result $expected", ({numbers, expected}) => { 27 | expect(sumAll(numbers)).toBe(expected); 28 | }) 29 | -------------------------------------------------------------------------------- /test/mock-partial-class.test.js: -------------------------------------------------------------------------------- 1 | import {UserRepository} from "../src/user-repository.js"; 2 | import {UserService} from "../src/user-service.js"; 3 | 4 | const repository = new UserRepository(); 5 | const service = new UserService(repository); 6 | 7 | test("test mock partial class findById", () => { 8 | const user = { 9 | id: 1, 10 | name: "Eko" 11 | }; 12 | 13 | const findByIdMock = jest.spyOn(repository, "findById"); 14 | findByIdMock.mockReturnValueOnce(user); 15 | 16 | expect(service.findById(1)).toEqual(user); 17 | expect(findByIdMock).toHaveBeenCalled(); 18 | expect(findByIdMock).toHaveBeenCalledWith(1); 19 | expect(repository.findById).toHaveBeenCalled(); 20 | expect(repository.findById).toHaveBeenCalledWith(1); 21 | }); 22 | 23 | test.failing("test mock partial findAll", () => { 24 | service.findAll(); 25 | }); 26 | -------------------------------------------------------------------------------- /test/mock-async-function.test.js: -------------------------------------------------------------------------------- 1 | import {getBalance} from "../src/async.js"; 2 | 3 | test("mock async function", async () => { 4 | const from = jest.fn(); 5 | from.mockResolvedValueOnce(1000); 6 | 7 | await expect(getBalance("Eko", from)).resolves.toEqual({ 8 | name: "Eko", 9 | balance: 1000 10 | }) 11 | 12 | expect(from.mock.calls.length).toBe(1); 13 | await expect(from.mock.results[0].value).resolves.toBe(1000); 14 | }); 15 | 16 | test.failing("mock async function rejected", async () => { 17 | const from = jest.fn(); 18 | from.mockRejectedValueOnce(new Error("Ups")); 19 | 20 | await getBalance("Eko", from); 21 | }); 22 | 23 | test("mock async function error matchers", async () => { 24 | const from = jest.fn(); 25 | from.mockRejectedValueOnce("Rejected"); 26 | 27 | await expect(getBalance("Eko", from)).rejects.toBe("Rejected"); 28 | }); 29 | -------------------------------------------------------------------------------- /test/mock-partial-modules.test.js: -------------------------------------------------------------------------------- 1 | import {getAllProducts, getProductById} from "../src/database.js"; 2 | import {ProductService} from "../src/product-service.js"; 3 | 4 | jest.mock("../src/database.js", () => { 5 | const originalModule = jest.requireActual("../src/database.js"); 6 | 7 | return { 8 | __esModule: true, 9 | ...originalModule, 10 | getAllProducts: jest.fn() 11 | } 12 | }); 13 | 14 | test.failing("mock modules getProductById", () => { 15 | ProductService.findById(1); 16 | }); 17 | 18 | test("mock modules getAllProducts", () => { 19 | const products = [ 20 | { 21 | id: 1, 22 | name: "Product Mock" 23 | }, 24 | { 25 | id: 2, 26 | name: "Product Mock" 27 | } 28 | ]; 29 | 30 | getAllProducts.mockImplementation(() => { 31 | return products; 32 | }); 33 | 34 | expect(ProductService.findAll()).toEqual(products); 35 | }); 36 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "belajar-nodejs-unit-test", 3 | "version": "1.0.0", 4 | "description": "Belajar NodeJS Unit Test", 5 | "main": "./src/index.js", 6 | "type": "module", 7 | "scripts": { 8 | "test": "jest" 9 | }, 10 | "jest": { 11 | "maxConcurrency" : 2, 12 | "verbose": true, 13 | "transform": { 14 | "^.+\\.[t|j]sx?$": "babel-jest" 15 | }, 16 | "collectCoverage": true, 17 | "coverageThreshold": { 18 | "global": { 19 | "branches": 100, 20 | "functions": 100, 21 | "lines": 100, 22 | "statements": 100 23 | } 24 | }, 25 | "collectCoverageFrom": [ 26 | "src/**/*.{js,jsx}", 27 | "!vendor/**/*.{js,jsx}" 28 | ] 29 | }, 30 | "author": "Eko Kurniawan Khannedy", 31 | "license": "ISC", 32 | "devDependencies": { 33 | "@babel/plugin-transform-runtime": "^7.17.12", 34 | "@babel/preset-env": "^7.17.12", 35 | "babel-jest": "^28.1.0", 36 | "jest": "^28.1.0" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /test/mock-modules.test.js: -------------------------------------------------------------------------------- 1 | import {ProductService} from "../src/product-service.js"; 2 | import {getAllProducts, getProductById} from "../src/database.js"; 3 | 4 | jest.mock("../src/database.js"); 5 | 6 | test("mock modules getProductById", () => { 7 | getProductById.mockImplementation((id) => { 8 | return { 9 | id: id, 10 | name: "Product Mock" 11 | } 12 | }); 13 | 14 | const product = ProductService.findById(1); 15 | 16 | expect(product).toEqual({ 17 | id: 1, 18 | name: "Product Mock" 19 | }); 20 | }); 21 | 22 | test("mock modules getAllProducts", () => { 23 | const products = [ 24 | { 25 | id: 1, 26 | name: "Product Mock" 27 | }, 28 | { 29 | id: 2, 30 | name: "Product Mock" 31 | } 32 | ]; 33 | 34 | getAllProducts.mockImplementation(() => { 35 | return products; 36 | }); 37 | 38 | expect(ProductService.findAll()).toEqual(products); 39 | }); 40 | -------------------------------------------------------------------------------- /test/mock-class.test.js: -------------------------------------------------------------------------------- 1 | import {UserRepository} from "../src/user-repository.js"; 2 | import {UserService} from "../src/user-service.js"; 3 | 4 | 5 | jest.mock("../src/user-repository.js"); 6 | 7 | const repository = new UserRepository(); 8 | const service = new UserService(repository); 9 | 10 | test("test mock user save", () => { 11 | 12 | const user = { 13 | id: 1, 14 | name: "Eko" 15 | }; 16 | 17 | service.save(user); 18 | 19 | expect(repository.save).toHaveBeenCalled(); 20 | expect(repository.save).toHaveBeenCalledWith(user); 21 | 22 | }); 23 | 24 | test("test mock class findById",() => { 25 | const user = { 26 | id: 1, 27 | name: "Eko" 28 | }; 29 | 30 | repository.findById.mockReturnValueOnce(user); 31 | 32 | expect(service.findById(1)).toEqual(user); 33 | expect(repository.findById).toHaveBeenCalled(); 34 | expect(repository.findById).toHaveBeenCalledWith(1); 35 | }) 36 | 37 | test("test mock class findAll",() => { 38 | const users = [ 39 | { 40 | id: 1, 41 | name: "Eko" 42 | }, 43 | { 44 | id: 2, 45 | name: "Eko" 46 | } 47 | ]; 48 | 49 | repository.findAll.mockReturnValueOnce(users); 50 | 51 | expect(service.findAll()).toEqual(users); 52 | expect(repository.findAll).toHaveBeenCalled(); 53 | }) 54 | -------------------------------------------------------------------------------- /test/mock-function.test.js: -------------------------------------------------------------------------------- 1 | import {calculate, calculateAndReturn} from "../src/sum.js"; 2 | 3 | test("test calculate", () => { 4 | const callback = jest.fn(); 5 | 6 | calculate([10, 10, 10], callback); 7 | calculate([10, 10, 10, 10, 10], callback); 8 | 9 | expect(callback.mock.calls.length).toBe(2); 10 | 11 | console.info(callback.mock.calls); 12 | 13 | expect(callback.mock.calls[0][0]).toBe(30); 14 | expect(callback.mock.calls[1][0]).toBe(50); 15 | }); 16 | 17 | test("test calculate without mock function", () => { 18 | const logging = (total) => { 19 | console.info(total); 20 | }; 21 | 22 | calculate([10, 10, 10], logging); 23 | calculate([10, 10, 10, 10, 10], logging); 24 | }) 25 | 26 | test("test mock return value", () => { 27 | const callback = jest.fn(); 28 | callback.mockReturnValueOnce(40); 29 | callback.mockReturnValueOnce(80); 30 | 31 | expect(calculateAndReturn([10, 10, 10], callback)).toBe(40); 32 | expect(calculateAndReturn([10, 10, 10], callback)).toBe(80); 33 | 34 | expect(callback.mock.results[0].value).toBe(40); 35 | expect(callback.mock.results[1].value).toBe(80); 36 | }); 37 | 38 | test("test mock implementation", () => { 39 | const callback = jest.fn(); 40 | callback.mockImplementation((total) => { 41 | return total * 2; 42 | }); 43 | 44 | expect(calculateAndReturn([10, 10, 10], callback)).toBe(60); 45 | expect(calculateAndReturn([10, 10, 10, 10, 10], callback)).toBe(100); 46 | 47 | expect(callback.mock.results[0].value).toBe(60); 48 | expect(callback.mock.results[1].value).toBe(100); 49 | }); 50 | --------------------------------------------------------------------------------