Skip to content

Commit d6467c5

Browse files
committed
test: preload clipboard mock before shell imports
1 parent 461ab21 commit d6467c5

5 files changed

Lines changed: 16 additions & 18 deletions

File tree

bunfig.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[test]
2+
preload = ["./test/setup.ts"]

packages/shell/test/document-copy.test.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { testRender } from "../../../test/support/test-render.ts"
2-
import { test, expect, afterEach, beforeEach, describe, mock } from "bun:test"
3-
import { clipboardStub, copied } from "../../../test/support/clipboard-mock.ts"
2+
import { test, expect, afterEach, beforeEach, describe } from "bun:test"
3+
import { copied } from "../../../test/support/clipboard-mock.ts"
44

5-
mock.module("@tooee/clipboard", () => clipboardStub())
65

76
const { TooeeProvider, useDocumentController, Document } = await import("@tooee/shell")
87
const { press, pressTab } = await import("./support/test-helpers.ts")

packages/view/test/view-search-copy.test.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { testRender } from "../../../test/support/test-render.ts"
2-
import { test, expect, afterEach, beforeEach, describe, mock } from "bun:test"
2+
import { test, expect, afterEach, beforeEach, describe } from "bun:test"
33
import { act } from "react"
4-
import { clipboardStub, copied } from "../../../test/support/clipboard-mock.ts"
4+
import { copied } from "../../../test/support/clipboard-mock.ts"
55

6-
mock.module("@tooee/clipboard", () => clipboardStub())
76

87
const { TooeeProvider } = await import("@tooee/shell")
98
const { MarkSetBuilder, MarkPriorities } = await import("@tooee/marks")

test/setup.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { mock } from "bun:test"
2+
import { clipboardStub } from "./support/clipboard-mock.ts"
3+
4+
// Register before any test can load @tooee/shell, whose copy command keeps its
5+
// imported clipboard binding for the lifetime of the shared Bun test process.
6+
mock.module("@tooee/clipboard", () => clipboardStub())

test/support/clipboard-mock.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
/**
22
* Text handed to `copyToClipboard` since the last `copied.length = 0`.
33
*
4-
* `mock.module` binds the module registry process-wide, and a module already
5-
* imported keeps the factory that was registered when it first resolved. Two
6-
* test files stubbing `@tooee/clipboard` therefore cannot each own a private
7-
* array — whichever registered first keeps receiving the writes. They share
8-
* this one instead, and each file registers its own stub around it:
9-
*
10-
* ```ts
11-
* mock.module("@tooee/clipboard", () => clipboardStub())
12-
* ```
13-
*
14-
* The `mock.module` call must live in the test file: resolved from here it
15-
* targets a different module instance than the one under test imports.
4+
* Bun shares its module registry across test files, so the suite preload owns
5+
* the single process-wide `@tooee/clipboard` mock and all clipboard assertions
6+
* share this sink. Registering mocks inside individual files is order-dependent:
7+
* `@tooee/shell` may already have captured the production clipboard binding.
168
*/
179
export const copied: string[] = []
1810

0 commit comments

Comments
 (0)