From f731467b8ac7169bfc97212abb537f4bbf9ebc92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=CC=88l?= Date: Mon, 16 Aug 2021 18:26:27 +0200 Subject: [PATCH] test(cli): test CLI warning for introspect --- packages/cli/src/CLI.ts | 6 +- .../cli/src/__tests__/__helpers__/context.ts | 5 + .../cli/src/__tests__/commands/CLI.test.ts | 115 ++++++++++++++++++ 3 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 packages/cli/src/__tests__/commands/CLI.test.ts diff --git a/packages/cli/src/CLI.ts b/packages/cli/src/CLI.ts index 413ee22eb582..808001758575 100644 --- a/packages/cli/src/CLI.ts +++ b/packages/cli/src/CLI.ts @@ -49,7 +49,7 @@ export class CLI implements Command { // display help for help flag or no subcommand if (args._.length === 0 || args['--help']) { - return CLI.help + return this.help() } // check if we have that subcommand @@ -107,10 +107,10 @@ export class CLI implements Command { return cmd.parse(argsForCmd) } // unknown command - return unknownCommand(CLI.help, args._[0]) + return unknownCommand(this.help() as string, args._[0]) } - private help(error?: string): string | HelpError { + public help(error?: string) { if (error) { return new HelpError(`\n${chalk.bold.red(`!`)} ${error}\n${CLI.help}`) } diff --git a/packages/cli/src/__tests__/__helpers__/context.ts b/packages/cli/src/__tests__/__helpers__/context.ts index 5f29e2df348f..2118651430c9 100644 --- a/packages/cli/src/__tests__/__helpers__/context.ts +++ b/packages/cli/src/__tests__/__helpers__/context.ts @@ -118,6 +118,7 @@ export const consoleContext: ContextContributorFactory< mocked: { 'console.error': jest.SpyInstance 'console.log': jest.SpyInstance + 'console.info': jest.SpyInstance 'console.warn': jest.SpyInstance } } @@ -129,6 +130,9 @@ export const consoleContext: ContextContributorFactory< ctx.mocked['console.log'] = jest .spyOn(console, 'log') .mockImplementation(() => {}) + ctx.mocked['console.info'] = jest + .spyOn(console, 'info') + .mockImplementation(() => {}) ctx.mocked['console.warn'] = jest .spyOn(console, 'warn') .mockImplementation(() => {}) @@ -137,6 +141,7 @@ export const consoleContext: ContextContributorFactory< afterEach(() => { ctx.mocked['console.error'].mockRestore() ctx.mocked['console.log'].mockRestore() + ctx.mocked['console.info'].mockRestore() ctx.mocked['console.warn'].mockRestore() }) diff --git a/packages/cli/src/__tests__/commands/CLI.test.ts b/packages/cli/src/__tests__/commands/CLI.test.ts new file mode 100644 index 000000000000..509902d23904 --- /dev/null +++ b/packages/cli/src/__tests__/commands/CLI.test.ts @@ -0,0 +1,115 @@ +import { CLI } from '../../CLI' +import { consoleContext, Context } from '../__helpers__/context' +import { + MigrateCommand, + MigrateDev, + MigrateResolve, + MigrateStatus, + MigrateReset, + MigrateDeploy, + DbPush, + DbPull, + // DbDrop, + DbSeed, + DbCommand, + handlePanic, +} from '@prisma/migrate' + +const ctx = Context.new().add(consoleContext()).assemble() + +const cliInstance = CLI.new( + { + // init: Init.new(), + // migrate: MigrateCommand.new({ + // dev: MigrateDev.new(), + // status: MigrateStatus.new(), + // resolve: MigrateResolve.new(), + // reset: MigrateReset.new(), + // deploy: MigrateDeploy.new(), + // }), + // db: DbCommand.new({ + // pull: DbPull.new(), + // push: DbPush.new(), + // // drop: DbDrop.new(), + // seed: DbSeed.new(), + // }), + /** + * @deprecated since version 2.30.0, use `db pull` instead (renamed) + */ + introspect: DbPull.new(), + // dev: Dev.new(), + // studio: Studio.new(), + // generate: Generate.new(), + // version: Version.new(), + // validate: Validate.new(), + // format: Format.new(), + // doctor: Doctor.new(), + // telemetry: Telemetry.new(), + }, + [ + 'version', + 'init', + 'migrate', + 'db', + 'introspect', + 'dev', + 'studio', + 'generate', + 'validate', + 'format', + 'doctor', + 'telemetry', + ], +) + +it('no params should return help', async () => { + const spy = jest + .spyOn(cliInstance, 'help') + .mockImplementation(() => 'Help Me') + + await cliInstance.parse([]) + expect(spy).toHaveBeenCalledTimes(1) + spy.mockRestore() +}) + +it('wrong flag', async () => { + const spy = jest + .spyOn(cliInstance, 'help') + .mockImplementation(() => 'Help Me') + + await cliInstance.parse(['--something']) + expect(spy).toHaveBeenCalledTimes(1) + spy.mockRestore() +}) + +it('help flag', async () => { + const spy = jest + .spyOn(cliInstance, 'help') + .mockImplementation(() => 'Help Me') + + await cliInstance.parse(['--help']) + expect(spy).toHaveBeenCalledTimes(1) + spy.mockRestore() +}) + +it('unknown command', async () => { + await expect(cliInstance.parse(['doesnotexist'])).resolves.toThrowError() +}) + +it('introspect should include deprecation warning', async () => { + const result = cliInstance.parse(['introspect']) + + await expect(result).rejects.toMatchInlineSnapshot(` + Could not find a schema.prisma file that is required for this command. + You can either provide it with --schema, set it as \`prisma.schema\` in your package.json or put it into the default location ./prisma/schema.prisma https://pris.ly/d/prisma-schema-location + `) + expect(ctx.mocked['console.log'].mock.calls).toHaveLength(0) + expect(ctx.mocked['console.info'].mock.calls).toHaveLength(0) + expect(ctx.mocked['console.warn'].mock.calls.join('\n')) + .toMatchInlineSnapshot(` + prisma:warn + prisma:warn prisma introspect command is deprecated. It has been renamed to prisma db pull + prisma:warn + `) + expect(ctx.mocked['console.error'].mock.calls).toHaveLength(0) +})