import { New } from "base/New"; import { describe, expect, test, vi } from "vitest"; function createTestObject() { return { value: Math.random() }; } describe("New.test.ts", () => { describe("New", () => { test("returns an msg that creates new instances", () => { const msg = New(createTestObject); const g = vi.fn(); msg.then(g); expect(g).toHaveBeenCalledTimes(1); const instance = g.mock.calls[0][0]; expect(instance).toHaveProperty("value"); expect(typeof instance.value).toBe("number"); }); test("creates new instance on each call", () => { const msg = New(createTestObject); const g1 = vi.fn(); const g2 = vi.fn(); msg.then(g1); msg.then(g2); expect(g1).toHaveBeenCalledTimes(1); expect(g2).toHaveBeenCalledTimes(1); const instance1 = g1.mock.calls[0][0]; const instance2 = g2.mock.calls[0][0]; expect(instance1).toHaveProperty("value"); expect(instance2).toHaveProperty("value"); expect(instance1).not.toBe(instance2); expect(instance1.value).not.toBe(instance2.value); }); }); });