Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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: /,
);
});
});
24 changes: 1 addition & 23 deletions packages/search-api-graphql/test/print-sdl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand All @@ -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({
Expand Down Expand Up @@ -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: /,
);
});
});