diff --git a/src/Test.res b/src/Test.res index 638c0c4..632c82a 100644 --- a/src/Test.res +++ b/src/Test.res @@ -1,6 +1,24 @@ open Types // TODO: mocking etc +/** + * Create mocks with the `mock` function. + * + * ```ts + * import { test, expect, mock } from "bun:test"; + * const random = mock(() => Math.random()); + * + * test("random", async () => { + * const val = random(); + * expect(val).toBeGreaterThan(0); + * expect(random).toHaveBeenCalled(); + * expect(random).toHaveBeenCalledTimes(1); + * }); + * ``` + */ +@module("bun:test") +external mock: (unit => 'value) => unit => 'value = "mock" + /** * Control the system time used by: * - `Date.now()` diff --git a/test/Mock.test.js b/test/Mock.test.js new file mode 100644 index 0000000..a8e181e --- /dev/null +++ b/test/Mock.test.js @@ -0,0 +1,23 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Buntest from "bun:test"; + +var mockFunction = Buntest.mock(function () { + + }); + +Buntest.describe("mock", (function () { + Buntest.beforeAll(function () { + mockFunction(); + }); + Buntest.test("mock function should have been called", (function () { + Buntest.expect(mockFunction).toHaveBeenCalled(); + mockFunction(); + Buntest.expect(mockFunction).toHaveBeenCalledTimes(2); + }), undefined); + })); + +export { + mockFunction , +} +/* mockFunction Not a pure module */ diff --git a/test/Mock.test.res b/test/Mock.test.res new file mode 100644 index 0000000..770524b --- /dev/null +++ b/test/Mock.test.res @@ -0,0 +1,15 @@ +open Test + +let mockFunction = mock(() => ()) + +describe("mock", () => { + beforeAll(() => { + mockFunction() + }) + + test("mock function should have been called", () => { + expect(mockFunction->Obj.magic)->Expect.toHaveBeenCalled + mockFunction() + expect(mockFunction->Obj.magic)->Expect.toHaveBeenCalledTimes(2.) + }) +})