From 20dc509f10e01ee2304efdeef395110b43e00ebf Mon Sep 17 00:00:00 2001 From: David de Boer Date: Fri, 21 Aug 2026 13:50:03 +0200 Subject: [PATCH] test(search-api-graphql): mock the unloadable Prettier peer in its own file - Move the unloadable-peer case out of print-sdl.test.ts, where it ran after tests that format for real and so depended on module-graph ordering: doMock only reaches a module imported after it, and resetModules does not evict an optional peer Node has already resolved. - Mock prettier with a hoisted vi.mock in the new file, so the peer is unloadable from the first import onwards and each file keeps its own process. - Drop the afterEach that only existed to unwind that mock. --- .../test/print-sdl-without-prettier.test.ts | 26 +++++++++++++++++++ .../search-api-graphql/test/print-sdl.test.ts | 24 +---------------- 2 files changed, 27 insertions(+), 23 deletions(-) create mode 100644 packages/search-api-graphql/test/print-sdl-without-prettier.test.ts diff --git a/packages/search-api-graphql/test/print-sdl-without-prettier.test.ts b/packages/search-api-graphql/test/print-sdl-without-prettier.test.ts new file mode 100644 index 00000000..1b10f3a9 --- /dev/null +++ b/packages/search-api-graphql/test/print-sdl-without-prettier.test.ts @@ -0,0 +1,26 @@ +import { fileURLToPath } from 'node:url'; +import { describe, expect, it, vi } from 'vitest'; +import { printSchemaModuleSdl } from '../src/print-sdl.js'; + +// Hoisted above the import above, so the optional peer is unloadable from the +// first import onwards. This case cannot share a file with the tests that +// format for real: a mock only reaches a module imported after it, and +// `vi.resetModules()` does not evict an optional peer Node has already +// resolved – which made the assertion depend on module-graph ordering. +vi.mock('prettier', () => { + throw new Error('Cannot find package ‘prettier’'); +}); + +describe('printSchemaModuleSdl without Prettier', () => { + it('names the optional Prettier peer when it cannot be loaded', async () => { + await expect( + printSchemaModuleSdl({ + modulePath: fileURLToPath( + new URL('./fixtures/no-options.mjs', import.meta.url), + ), + }), + ).rejects.toThrowError( + /Formatting the SDL requires “prettier”, an optional peer dependency .*, which could not be loaded: /, + ); + }); +}); diff --git a/packages/search-api-graphql/test/print-sdl.test.ts b/packages/search-api-graphql/test/print-sdl.test.ts index 8f373eb3..d71f49be 100644 --- a/packages/search-api-graphql/test/print-sdl.test.ts +++ b/packages/search-api-graphql/test/print-sdl.test.ts @@ -2,7 +2,7 @@ import { mkdtemp, readFile, writeFile } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { fileURLToPath } from 'node:url'; -import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { beforeEach, describe, expect, it } from 'vitest'; import { printSchemaModuleSdl } from '../src/print-sdl.js'; const fixture = (name: string): string => @@ -14,11 +14,6 @@ beforeEach(async () => { directory = await mkdtemp(join(tmpdir(), 'lde-print-sdl-')); }); -afterEach(() => { - vi.doUnmock('prettier'); - vi.resetModules(); -}); - describe('printSchemaModuleSdl', () => { it('prints the contract of the mounted module', async () => { const sdl = await printSchemaModuleSdl({ @@ -119,21 +114,4 @@ describe('printSchemaModuleSdl', () => { /Cannot load schema module “\/no\/such\/module\.mjs”/, ); }); - - it('names the optional Prettier peer when it cannot be loaded', async () => { - // Reset first: the tests above have already pulled the real Prettier into - // the module graph, and a mock only applies to a module imported after it. - vi.resetModules(); - vi.doMock('prettier', () => { - throw new Error('Cannot find package ‘prettier’'); - }); - const { printSchemaModuleSdl: printWithoutPrettier } = - await import('../src/print-sdl.js'); - - await expect( - printWithoutPrettier({ modulePath: fixture('no-options.mjs') }), - ).rejects.toThrowError( - /Formatting the SDL requires “prettier”, an optional peer dependency .*, which could not be loaded: /, - ); - }); });