-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Init.test.ts
187 lines (143 loc) · 7.96 KB
/
Init.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import fs from 'fs'
import { join } from 'path'
import stripAnsi from 'strip-ansi'
import { defaultEnv, defaultGitIgnore, defaultSchema } from '../../Init'
import { consoleContext, Context } from '../__helpers__/context'
const ctx = Context.new().add(consoleContext()).assemble()
test('is schema and env written on disk replace', async () => {
const result = await ctx.cli('init')
expect(stripAnsi(result.stdout)).toMatchSnapshot()
const schema = fs.readFileSync(join(ctx.tmpDir, 'prisma', 'schema.prisma'), 'utf-8')
expect(schema).toMatch(defaultSchema())
const env = fs.readFileSync(join(ctx.tmpDir, '.env'), 'utf-8')
expect(env).toMatch(defaultEnv())
})
test('works with url param', async () => {
ctx.fixture('init')
const result = await ctx.cli('init', '--url', 'file:dev.db')
expect(stripAnsi(result.stdout)).toMatchSnapshot()
const schema = fs.readFileSync(join(ctx.tmpDir, 'prisma', 'schema.prisma'), 'utf-8')
expect(schema).toMatch(defaultSchema('sqlite'))
const env = fs.readFileSync(join(ctx.tmpDir, '.env'), 'utf-8')
expect(env).toMatchInlineSnapshot(`
# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#using-environment-variables
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server and MongoDB (Preview).
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
DATABASE_URL="file:dev.db"
`)
})
test('works with provider param - postgresql', async () => {
ctx.fixture('init')
const result = await ctx.cli('init', '--datasource-provider', 'postgresql')
expect(stripAnsi(result.stdout)).toMatchSnapshot()
const schema = fs.readFileSync(join(ctx.tmpDir, 'prisma', 'schema.prisma'), 'utf-8')
expect(schema).toMatch(defaultSchema('postgresql'))
const env = fs.readFileSync(join(ctx.tmpDir, '.env'), 'utf-8')
expect(env).toMatchInlineSnapshot(`
# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#using-environment-variables
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server and MongoDB (Preview).
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
DATABASE_URL="postgresql://johndoe:randompassword@localhost:5432/mydb?schema=public"
`)
})
test('works with provider param - mysql', async () => {
ctx.fixture('init')
const result = await ctx.cli('init', '--datasource-provider', 'mysql')
expect(stripAnsi(result.stdout)).toMatchSnapshot()
const schema = fs.readFileSync(join(ctx.tmpDir, 'prisma', 'schema.prisma'), 'utf-8')
expect(schema).toMatch(defaultSchema('mysql'))
const env = fs.readFileSync(join(ctx.tmpDir, '.env'), 'utf-8')
expect(env).toMatchInlineSnapshot(`
# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#using-environment-variables
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server and MongoDB (Preview).
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
DATABASE_URL="mysql://johndoe:randompassword@localhost:3306/mydb"
`)
})
test('works with provider param - SQLITE', async () => {
ctx.fixture('init')
const result = await ctx.cli('init', '--datasource-provider', 'SQLITE')
expect(stripAnsi(result.stdout)).toMatchSnapshot()
const schema = fs.readFileSync(join(ctx.tmpDir, 'prisma', 'schema.prisma'), 'utf-8')
expect(schema).toMatch(defaultSchema('sqlite'))
const env = fs.readFileSync(join(ctx.tmpDir, '.env'), 'utf-8')
expect(env).toMatchInlineSnapshot(`
# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#using-environment-variables
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server and MongoDB (Preview).
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
DATABASE_URL="file:./dev.db"
`)
})
test('works with provider param - SqlServer', async () => {
ctx.fixture('init')
const result = await ctx.cli('init', '--datasource-provider', 'SqlServer')
expect(stripAnsi(result.stdout)).toMatchSnapshot()
const schema = fs.readFileSync(join(ctx.tmpDir, 'prisma', 'schema.prisma'), 'utf-8')
expect(schema).toMatch(defaultSchema('sqlserver'))
const env = fs.readFileSync(join(ctx.tmpDir, '.env'), 'utf-8')
expect(env).toMatchInlineSnapshot(`
# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#using-environment-variables
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server and MongoDB (Preview).
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
DATABASE_URL="sqlserver://localhost:1433;database=mydb;user=SA;password=randompassword;"
`)
})
test('works with provider param - MongoDB', async () => {
ctx.fixture('init')
const result = await ctx.cli('init', '--datasource-provider', 'MongoDB')
expect(stripAnsi(result.stdout)).toMatchSnapshot()
const schema = fs.readFileSync(join(ctx.tmpDir, 'prisma', 'schema.prisma'), 'utf-8')
expect(schema).toMatch(defaultSchema('mongodb'))
const env = fs.readFileSync(join(ctx.tmpDir, '.env'), 'utf-8')
expect(env).toMatchInlineSnapshot(`
# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#using-environment-variables
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server and MongoDB (Preview).
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
DATABASE_URL="mongodb+srv://root:randompassword@cluster0.ab1cd.mongodb.net/mydb?retryWrites=true&w=majority"
`)
})
test('errors with invalid provider param', async () => {
ctx.fixture('init')
const result = ctx.cli('init', '--datasource-provider', 'INVALID')
await expect(result).rejects.toThrowError()
})
test('warns when DATABASE_URL present in .env ', async () => {
fs.writeFileSync(join(ctx.tmpDir, '.env'), `DATABASE_URL="postgres://dont:overwrite@me:5432/tests"`)
const result = await ctx.cli('init')
expect(stripAnsi(result.all!)).toMatchSnapshot()
const schema = fs.readFileSync(join(ctx.tmpDir, 'prisma', 'schema.prisma'), 'utf-8')
expect(schema).toMatch(defaultSchema())
const env = fs.readFileSync(join(ctx.tmpDir, '.env'), 'utf-8')
expect(env).toMatch(`DATABASE_URL="postgres://dont:overwrite@me:5432/tests"`)
})
test('appends when .env present', async () => {
fs.writeFileSync(join(ctx.tmpDir, '.env'), `SOMTHING="is here"`)
const result = await ctx.cli('init')
expect(stripAnsi(result.stdout)).toMatchSnapshot()
const schema = fs.readFileSync(join(ctx.tmpDir, 'prisma', 'schema.prisma'), 'utf-8')
expect(schema).toMatch(defaultSchema())
const env = fs.readFileSync(join(ctx.tmpDir, '.env'), 'utf-8')
expect(env).toMatchSnapshot()
})
test('writes a minimal .gitignore file', async () => {
ctx.fixture('init')
await ctx.cli('init')
const gitignore = fs.readFileSync(join(ctx.tmpDir, '.gitignore'), 'utf-8')
expect(gitignore).toMatch(defaultGitIgnore())
expect(gitignore).toMatchSnapshot()
})
test('ignore .gitignore file if already present (do not override)', async () => {
ctx.fixture('init')
const gitignorePath = join(ctx.tmpDir, '.gitignore')
fs.writeFileSync(gitignorePath, `# This should not be overriden`)
const gitignoreBefore = fs.readFileSync(gitignorePath, 'utf-8')
await ctx.cli('init')
const gitignoreAfter = fs.readFileSync(gitignorePath, 'utf-8')
expect(gitignoreAfter).toEqual(gitignoreBefore)
})