Skip to content

Commit

Permalink
test(cli): test CLI warning for introspect
Browse files Browse the repository at this point in the history
  • Loading branch information
Jolg42 committed Aug 16, 2021
1 parent 69c19f0 commit f731467
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/cli/src/CLI.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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}`)
}
Expand Down
5 changes: 5 additions & 0 deletions packages/cli/src/__tests__/__helpers__/context.ts
Expand Up @@ -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
}
}
Expand All @@ -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(() => {})
Expand All @@ -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()
})

Expand Down
115 changes: 115 additions & 0 deletions 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)
})

0 comments on commit f731467

Please sign in to comment.