-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
DbDrop.test.ts
155 lines (122 loc) · 5.24 KB
/
DbDrop.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
process.env.GITHUB_ACTIONS = '1'
import prompt from 'prompts'
import { DbDrop } from '../commands/DbDrop'
import { consoleContext, Context } from './__helpers__/context'
const ctx = Context.new().add(consoleContext()).assemble()
describe('drop', () => {
it('requires --preview-feature flag', async () => {
ctx.fixture('empty')
const result = DbDrop.new().parse([])
await expect(result).rejects.toThrowErrorMatchingInlineSnapshot(`
This feature is currently in Preview. There may be bugs and it's not recommended to use it in production environments.
Please provide the --preview-feature flag to use this command.
`)
})
it('should fail if no schema file', async () => {
ctx.fixture('empty')
const result = DbDrop.new().parse(['--preview-feature'])
await expect(result).rejects.toThrowErrorMatchingInlineSnapshot(`
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
`)
})
it('with missing db should fail (prompt)', async () => {
ctx.fixture('reset')
ctx.fs.remove('prisma/dev.db')
prompt.inject(['y']) // simulate user yes input
const result = DbDrop.new().parse(['--preview-feature'])
await expect(result).rejects.toMatchInlineSnapshot(
`The database name entered "y" doesn't match "dev.db".`,
)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
})
it('with missing db should fail (--force)', async () => {
ctx.fixture('reset')
ctx.fs.remove('prisma/dev.db')
const result = DbDrop.new().parse(['--preview-feature', '--force'])
await expect(result).rejects.toMatchInlineSnapshot(`
Failed to delete SQLite database at \`dev.db\`.
No such file or directory (os error 2)
`)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
})
it('should work (prompt)', async () => {
ctx.fixture('reset')
prompt.inject(['dev.db']) // simulate user input
const result = DbDrop.new().parse(['--preview-feature'])
await expect(result).resolves.toMatchInlineSnapshot(`
🚀 The SQLite database "dev.db" from "file:dev.db" was successfully dropped.
`)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Datasource "my_db": SQLite database "dev.db" at "file:dev.db"
`)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
})
it('should work (--force)', async () => {
ctx.fixture('reset')
const result = DbDrop.new().parse(['--preview-feature', '--force'])
await expect(result).resolves.toMatchInlineSnapshot(`
🚀 The SQLite database "dev.db" from "file:dev.db" was successfully dropped.
`)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Datasource "my_db": SQLite database "dev.db" at "file:dev.db"
`)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
})
it('should work (-f)', async () => {
ctx.fixture('reset')
const result = DbDrop.new().parse(['--preview-feature', '-f'])
await expect(result).resolves.toMatchInlineSnapshot(`
🚀 The SQLite database "dev.db" from "file:dev.db" was successfully dropped.
`)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Datasource "my_db": SQLite database "dev.db" at "file:dev.db"
`)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
})
it('should be cancelled (prompt)', async () => {
ctx.fixture('reset')
const mockExit = jest.spyOn(process, 'exit').mockImplementation()
prompt.inject([new Error()]) // simulate cancel
const result = DbDrop.new().parse(['--preview-feature'])
await expect(result).resolves.toMatchInlineSnapshot(`
🚀 The SQLite database "dev.db" from "file:dev.db" was successfully dropped.
`)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Datasource "my_db": SQLite database "dev.db" at "file:dev.db"
Drop cancelled.
`)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
expect(mockExit).toBeCalledWith(0)
})
it('should ask for --force if not provided if CI', async () => {
ctx.fixture('reset')
const result = DbDrop.new().parse(['--preview-feature'])
await expect(result).rejects.toMatchInlineSnapshot(
`Use the --force flag to use the drop command in an unnattended environment like prisma db drop --force --preview-feature`,
)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
})
})