diff --git a/packages/core/src/scripts/prisma.ts b/packages/core/src/scripts/prisma.ts index 5d34d69d199..57b001f469f 100644 --- a/packages/core/src/scripts/prisma.ts +++ b/packages/core/src/scripts/prisma.ts @@ -16,7 +16,7 @@ export async function prisma (cwd: string, args: string[], frozen: boolean) { // this is the compiled version of the configuration which was generated during the build step if (!(await fs.stat(builtConfigPath).catch(() => null))) { - console.error('🚨 keystone build must be run before running keystone start') + console.error('🚨 keystone build must be run before running keystone prisma') throw new ExitError(1) } diff --git a/tests/cli-tests/__snapshots__/prisma.test.ts.snap b/tests/cli-tests/__snapshots__/prisma.test.ts.snap index 01de043ea45..ce562c097de 100644 --- a/tests/cli-tests/__snapshots__/prisma.test.ts.snap +++ b/tests/cli-tests/__snapshots__/prisma.test.ts.snap @@ -1,7 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`keystone prisma exits with the same code as the prisma child process exits with 1`] = ` -"? Generated GraphQL and Prisma schemas +"? GraphQL and Prisma schemas are up to date ! Unknown command "bad-thing" @@ -59,12 +59,14 @@ Examples Display Prisma debug info $ prisma debug + " `; exports[`keystone prisma uses the db url in the keystone config 1`] = ` -"? Generated GraphQL and Prisma schemas +"? GraphQL and Prisma schemas are up to date Prisma schema loaded from schema.prisma Datasource "sqlite": SQLite database "app.db" at "file:./app.db" -Error: P1003: Database app.db does not exist at ./app.db" +Error: P1003: Database app.db does not exist at ./app.db +" `; diff --git a/tests/cli-tests/prisma.test.ts b/tests/cli-tests/prisma.test.ts index 0e6c26e4c9e..29366bbdb57 100644 --- a/tests/cli-tests/prisma.test.ts +++ b/tests/cli-tests/prisma.test.ts @@ -1,34 +1,38 @@ -import execa from 'execa' -import { basicKeystoneConfig, cliBinPath, schemas, symlinkKeystoneDeps, testdir } from './utils' +import { + basicKeystoneConfig, + schemas, + spawnCommand, + spawnCommand2, + symlinkKeystoneDeps, + testdir +} from './utils' // testing erroring when the schemas are not up to date is in artifacts.test.ts test('keystone prisma exits with the same code as the prisma child process exits with', async () => { - const tmp = await testdir({ + const cwd = await testdir({ ...symlinkKeystoneDeps, ...schemas, 'keystone.js': basicKeystoneConfig, }) - const result = await execa('node', [cliBinPath, 'prisma', 'bad-thing'], { - reject: false, - all: true, - cwd: tmp, - }) - expect(result.all!.replace(/[^ -~\n]/g, '?').replace(/ \n/g, '\n')).toMatchSnapshot() - expect(result.exitCode).toBe(1) + + await spawnCommand(cwd, ['build', '--no-ui']) + const { exitCode, output } = await spawnCommand2(cwd, ['prisma', 'bad-thing']) + + expect(exitCode).toEqual(1) + expect(output.replace(/[^ -~\n]/g, '?').replace(/ \n/g, '\n')).toMatchSnapshot() }) test('keystone prisma uses the db url in the keystone config', async () => { - const tmp = await testdir({ + const cwd = await testdir({ ...symlinkKeystoneDeps, ...schemas, 'keystone.js': basicKeystoneConfig, }) - const result = await execa('node', [cliBinPath, 'prisma', 'migrate', 'status'], { - reject: false, - all: true, - cwd: tmp, - }) - expect(result.all!.replace(/[^ -~\n]/g, '?').replace(/ \n/g, '\n')).toMatchSnapshot() - expect(result.exitCode).toBe(1) + + await spawnCommand(cwd, ['build', '--no-ui']) + const { exitCode, output } = await spawnCommand2(cwd, ['prisma', 'migrate', 'status']) + + expect(exitCode).toEqual(1) + expect(output.replace(/[^ -~\n]/g, '?').replace(/ \n/g, '\n')).toMatchSnapshot() }) diff --git a/tests/cli-tests/prisma.test.ts.2 b/tests/cli-tests/prisma.test.ts.2 new file mode 100644 index 00000000000..75dc2e5ea79 --- /dev/null +++ b/tests/cli-tests/prisma.test.ts.2 @@ -0,0 +1,37 @@ +import { + basicKeystoneConfig, + cliBinPath, + schemas, + spawnCommand, + symlinkKeystoneDeps, + testdir +} from './utils' + +// testing erroring when the schemas are not up to date is in artifacts.test.ts + +test('keystone prisma exits with the same code as the prisma child process exits with', async () => { + const cwd = await testdir({ + ...symlinkKeystoneDeps, + ...schemas, + 'keystone.js': basicKeystoneConfig, + }) + + const output = await spawnCommand(cwd, ['prisma', 'bad-thing']) + expect(result.all!.replace(/[^ -~\n]/g, '?').replace(/ \n/g, '\n')).toMatchSnapshot() + expect(result.exitCode).toBe(1) +}) + +test('keystone prisma uses the db url in the keystone config', async () => { + const tmp = await testdir({ + ...symlinkKeystoneDeps, + ...schemas, + 'keystone.js': basicKeystoneConfig, + }) + const result = await execa('node', [cliBinPath, 'prisma', 'migrate', 'status'], { + reject: false, + all: true, + cwd: tmp, + }) + expect(result.all!.replace(/[^ -~\n]/g, '?').replace(/ \n/g, '\n')).toMatchSnapshot() + expect(result.exitCode).toBe(1) +}) diff --git a/tests/cli-tests/utils.ts b/tests/cli-tests/utils.ts index ed6295175ff..52d3420d738 100644 --- a/tests/cli-tests/utils.ts +++ b/tests/cli-tests/utils.ts @@ -90,13 +90,27 @@ export async function spawnCommand (cwd: string, commands: string[]) { p.stdout.on('data', (data) => (output += data.toString('utf-8'))) p.stderr.on('data', (data) => (output += data.toString('utf-8'))) p.on('error', err => reject(err)) - p.on('exit', code => { - if (code) return reject(new ExitError(code)) + p.on('exit', exitCode => { + if (typeof exitCode === 'number' && exitCode !== 0) return reject(new ExitError(exitCode)) resolve(output) }) }) } +export async function spawnCommand2 (cwd: string, commands: string[]) { + let output = '' + return new Promise<{ + exitCode: number | null, + output: string + }>((resolve, reject) => { + const p = spawn('node', [cliBinPath, ...commands], { cwd }) + p.stdout.on('data', (data) => (output += data.toString('utf-8'))) + p.stderr.on('data', (data) => (output += data.toString('utf-8'))) + p.on('error', err => reject(err)) + p.on('exit', exitCode => (resolve({ exitCode, output }))) + }) +} + let dirsToRemove: string[] = [] afterAll(async () => {