Skip to content

Commit

Permalink
remove process.cwd change in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Calder committed Feb 28, 2023
1 parent 0d73d57 commit 5a657b7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 49 deletions.
41 changes: 15 additions & 26 deletions tests/admin-ui-tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,31 +61,20 @@ export function generateDataArray(map: (key: number) => any, range: number) {

export async function deleteAllData(projectDir: string) {
const resolvedProjectDir = path.resolve(projectRoot, projectDir);
/**
* As of @prisma/[email protected] it appears that the prisma client runtime tries to resolve the path to the prisma schema
* from process.cwd(). This is not always the project directory we want to run keystone from.
* Here we mutate the process.cwd global with a fn that returns the project directory we expect, such that prisma
* can retrieve the correct schema file.
*/
const prevCwd = process.cwd;
try {
process.cwd = () => resolvedProjectDir;
const { PrismaClient } = require(path.join(
resolvedProjectDir,
'node_modules/.testprisma/client'
));
const prisma = new PrismaClient();

await prisma.$transaction(
Object.values(prisma)
.filter((x: any) => x?.deleteMany)
.map((x: any) => x?.deleteMany?.({}))
);

await prisma.$disconnect();
} finally {
process.cwd = prevCwd;
}

const { PrismaClient } = require(path.join(
resolvedProjectDir,
'node_modules/.testprisma/client'
));
const prisma = new PrismaClient();

await prisma.$transaction(
Object.values(prisma)
.filter((x: any) => x?.deleteMany)
.map((x: any) => x?.deleteMany?.({}))
);

await prisma.$disconnect();
}

export const adminUITests = (
Expand All @@ -99,7 +88,7 @@ export const adminUITests = (
let cleanupKeystoneProcess = () => {};

afterAll(async () => {
await cleanupKeystoneProcess();
cleanupKeystoneProcess();
});

async function startKeystone(command: 'start' | 'dev') {
Expand Down
34 changes: 11 additions & 23 deletions tests/examples-smoke-tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,16 @@ export async function loadIndex(page: playwright.Page) {
}

async function deleteAllData(projectDir: string) {
/**
* As of @prisma/[email protected] it appears that the prisma client runtime tries to resolve the path to the prisma schema
* from process.cwd(). This is not always the project directory we want to run keystone from.
* Here we mutate the process.cwd global with a fn that returns the project directory we expect, such that prisma
* can retrieve the correct schema file.
*/
const prevCwd = process.cwd;
try {
process.cwd = () => projectDir;
const { PrismaClient } = require(path.join(projectDir, 'node_modules/.myprisma/client'));
const prisma = new PrismaClient();

await prisma.$transaction(
Object.values(prisma)
.filter((x: any) => x?.deleteMany)
.map((x: any) => x?.deleteMany?.({}))
);

await prisma.$disconnect();
} finally {
process.cwd = prevCwd;
}
const { PrismaClient } = require(path.join(projectDir, 'node_modules/.myprisma/client'));
const prisma = new PrismaClient();

await prisma.$transaction(
Object.values(prisma)
.filter((x: any) => x?.deleteMany)
.map((x: any) => x?.deleteMany?.({}))
);

await prisma.$disconnect();
}

const treeKill = promisify(_treeKill);
Expand Down Expand Up @@ -79,7 +67,7 @@ export const exampleProjectTests = (
let cleanupKeystoneProcess = () => {};

afterAll(async () => {
await cleanupKeystoneProcess();
cleanupKeystoneProcess();
});

async function startKeystone(command: 'start' | 'dev') {
Expand Down

0 comments on commit 5a657b7

Please sign in to comment.