Skip to content

Commit 9ca03c4

Browse files
committed
Use dynamic import() to load Prisma client
The previous implementation used createRequire(import.meta.url) to synchronously require() the Prisma client at options.clientPath. This is incompatible with Prisma 7's `prisma-client` generator, which outputs plain TypeScript (ESM) files that cannot be loaded by Node's native CJS require(). Switch to await import(options.clientPath) so that: - Under Vitest, clientPath can point at the generated TS source directly (e.g. a file:// URL to `client.ts`), and Vite's transform pipeline handles the TypeScript/ESM resolution. - Under plain Node, any ESM-compatible specifier continues to work (package names, file:// URLs, .js/.mjs paths). `createContext` becomes async; callers must `await` it. The only caller is `environment.setup`, which is already async. Fixes #22
1 parent fc5b11e commit 9ca03c4

3 files changed

Lines changed: 5 additions & 8 deletions

File tree

src/contest.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ const makeContext = async (
1919
| 'transactionPending'
2020
| 'transactionStarted'
2121
| 'transactionEnded' = 'transactionPending',
22-
): Promise<[ReturnType<typeof createContext>, PrismaClientStub]> => {
23-
const context = createContext({
22+
): Promise<[Awaited<ReturnType<typeof createContext>>, PrismaClientStub]> => {
23+
const context = await createContext({
2424
clientPath: '../test/prisma-client-stub.js',
2525
databaseUrl: 'postgres://fake',
2626
log: ['query'],

src/context.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
import { createRequire } from 'node:module';
21
import { PrismaPg } from '@prisma/adapter-pg';
32
import type {
43
PrismaClientLike,
54
PrismaPostgresEnvironmentOptions,
65
} from './dts/index.js';
76

8-
const require = createRequire(import.meta.url);
9-
107
/**
118
* Creates the test context used by the `prisma-postgres` Vitest environment.
129
*
1310
* @param options PrismaPostgresEnvironmentOptions
1411
* @returns The test context object used by both the Vitest environment and the
1512
* user's Prisma client mock.
1613
*/
17-
export function createContext(options: PrismaPostgresEnvironmentOptions) {
14+
export async function createContext(options: PrismaPostgresEnvironmentOptions) {
1815
let savePointCounter = 0;
1916

2017
/**
@@ -31,7 +28,7 @@ export function createContext(options: PrismaPostgresEnvironmentOptions) {
3128
*/
3229
let internalEndTestTransaction: (() => void) | null = null;
3330

34-
const { PrismaClient } = require(options.clientPath);
31+
const { PrismaClient } = await import(options.clientPath);
3532
const originalClient: PrismaClientLike = new PrismaClient({
3633
adapter: new PrismaPg({ connectionString: process.env.DATABASE_URL }),
3734
log: options.log,

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const environment: Environment = {
1717
throw new Error('no DATABASE_URL defined!');
1818
}
1919

20-
const ctx = createContext(options);
20+
const ctx = await createContext(options);
2121
await ctx.setup();
2222

2323
// make context available globally for setupFiles.

0 commit comments

Comments
 (0)