Skip to content

Lib.useInMemoryClipboard

Lib › useInMemoryClipboard

Type: (this: void, options?: UseInMemoryClipboardOptions) => InMemoryClipboard

Replaces a window's clipboard with an in-memory one until the returned clipboard's restore() (or [Symbol.dispose]) runs.

A test that uses the system clipboard has two problems. The harness keeps its window hidden and unfocused, and Chromium refuses navigator.clipboard to a document that is not focused. And the system clipboard is the owner's: a test overwrites it, and anything else on the machine can overwrite it between the test's write and its read.

It replaces navigator.clipboard's writeText, readText, write and read in the window, and the readText, writeText, readHTML, writeHTML, write and clear methods of Electron's clipboard module where the window has require. The Electron module is shared by every window of the renderer, so it is replaced for all of them.

The clipboard stays installed across evaluations. Read it back from a later one with Lib.getInMemoryClipboard, and restore it in a finally or an afterEach: a clipboard left installed answers every later test. Installing a second one in the same window restores the first.