Skip to content

Commit

Permalink
Merge pull request #72 from farreldarian/back-to-vitest
Browse files Browse the repository at this point in the history
test: back to vitest
  • Loading branch information
fdarian authored Jun 24, 2024
2 parents 717915b + 0e276d7 commit e9a704b
Show file tree
Hide file tree
Showing 15 changed files with 43 additions and 37 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ jobs:
runs-on: ubuntu-latest

env:
PG_DATABASE_URL: postgres://postgres:[email protected]:5432/testdb
MYSQL_DATABASE_URL: mysql://root:[email protected]:3306/testdb
VITE_PG_DATABASE_URL: postgres://postgres:[email protected]:5432/testdb
VITE_MYSQL_DATABASE_URL: mysql://root:[email protected]:3306/testdb

services:
mysql:
Expand Down
Binary file modified bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions packages/usage/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# NOTE: this will also be used for running test,
# make sure that the db has no sensitive data
PG_DATABASE_URL=postgres://<username>:<password>@localhost:5432/<db>
MYSQL_DATABASE_URL=mysql://<username>:<password>@localhost:3306/<db>
VITE_PG_DATABASE_URL=postgres://<username>:<password>@localhost:5432/<db>
VITE_MYSQL_DATABASE_URL=mysql://<username>:<password>@localhost:3306/<db>
7 changes: 4 additions & 3 deletions packages/usage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "module",
"scripts": {
"start": "bun run src/index.ts",
"test": "bun test --preload tests/setup.ts",
"test": "bun vitest run",
"generate": "prisma generate",
"pushreset:postgres": "bun prisma db push --schema prisma/schema.prisma --force-reset --accept-data-loss",
"pushreset:mysql": "bun prisma db push --schema prisma/mysql/schema.prisma --force-reset --accept-data-loss",
Expand All @@ -17,7 +17,7 @@
"dependencies": {
"@paralleldrive/cuid2": "^2.2.2",
"@prisma/client": "5.15.0",
"better-sqlite3": "^9.4.3",
"better-sqlite3": "^11.0.0",
"date-fns": "^3.3.1",
"decimal.js": "^10.4.3",
"drizzle-orm": "^0.30.1",
Expand All @@ -35,6 +35,7 @@
"bun-types": "^1.0.30",
"prisma": "5.15.0",
"prisma-generator-drizzle": "workspace:*",
"typescript": "5.4.2"
"typescript": "5.4.2",
"vitest": "^1.6.0"
}
}
2 changes: 1 addition & 1 deletion packages/usage/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ generator drizzle {

datasource db {
provider = "postgresql"
url = env("PG_DATABASE_URL")
url = env("VITE_PG_DATABASE_URL")
}

model SelfReference {
Expand Down
2 changes: 1 addition & 1 deletion packages/usage/scripts/cloneMysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const TARGET_PATH = './prisma/mysql/schema.prisma'

const schema = (await Bun.file(BASE_PATH).text())
.replace('postgresql', 'mysql')
.replace('PG_DATABASE_URL', 'MYSQL_DATABASE_URL')
.replace('VITE_PG_DATABASE_URL', 'VITE_MYSQL_DATABASE_URL')
.replace(/(\/\/ start -mysql\n)[\s\S]*?(\n\/\/ end -mysql)/g, '')
.replace(/^.*-mysql.*/gm, '')

Expand Down
2 changes: 1 addition & 1 deletion packages/usage/scripts/cloneSqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const TARGET_PATH = './prisma/sqlite/schema.prisma'

const schema = (await Bun.file(BASE_PATH).text())
.replace('postgresql', 'sqlite')
.replace('env("PG_DATABASE_URL")', '"file:./test.db"')
.replace('env("VITE_PG_DATABASE_URL")', '"file:./test.db"')
.replace(/(\/\/ start -sqlite\n)[\s\S]*?(\n\/\/ end -sqlite)/g, '')
.replace(/^.*-sqlite.*/gm, '')

Expand Down
4 changes: 2 additions & 2 deletions packages/usage/src/lib/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { url, object, parse, pipe, string } from 'valibot'

const env = parse(
object({
MYSQL_DATABASE_URL: pipe(string(), url()),
VITE_MYSQL_DATABASE_URL: pipe(string(), url()),
}),
process.env
)

const connection = await mysql.createConnection(env.MYSQL_DATABASE_URL)
const connection = await mysql.createConnection(env.VITE_MYSQL_DATABASE_URL)

export const db = drizzle(connection, { schema, mode: 'default' })
4 changes: 2 additions & 2 deletions packages/usage/src/lib/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { url, object, parse, pipe, string } from 'valibot'

const env = parse(
object({
PG_DATABASE_URL: pipe(string(), url()),
VITE_PG_DATABASE_URL: pipe(string(), url()),
}),
process.env
)

const queryClient = postgres(env.PG_DATABASE_URL)
const queryClient = postgres(env.VITE_PG_DATABASE_URL)
export const db = drizzle(queryClient, { schema })
4 changes: 2 additions & 2 deletions packages/usage/src/lib/sqlite.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Database } from 'bun:sqlite'
import { drizzle } from 'drizzle-orm/bun-sqlite'
import Database from 'better-sqlite3'
import { drizzle } from 'drizzle-orm/better-sqlite3'
import { schema } from 'prisma/sqlite/drizzle/schema'

const sqlite = new Database('./prisma/sqlite/test.db')
Expand Down
19 changes: 10 additions & 9 deletions packages/usage/tests/configure-date-mode.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { $ } from 'bun'
import { execSync } from 'node:child_process'
import * as fs from 'node:fs'
import { type TempDirectory, createTempHandler } from './utils/temp'

const tempHandler = createTempHandler()
Expand All @@ -10,11 +11,11 @@ afterAll(async () => {
test('global config', async () => {
const temp = await tempHandler.prepare()

await Bun.write(
fs.writeFileSync(
getSchemaPath(temp),
`datasource db {
provider = "postgresql"
url = env("PG_DATABASE_URL")
url = env("VITE_PG_DATABASE_URL")
}
generator drizzle {
Expand All @@ -28,9 +29,9 @@ test('global config', async () => {
date DateTime
}`
)
await $`bun prisma generate --schema ${getSchemaPath(temp)}`.quiet()
execSync(`bun prisma generate --schema ${getSchemaPath(temp)}`)

const output = await Bun.file(`${temp.basePath}/drizzle.ts`).text()
const output = fs.readFileSync(`${temp.basePath}/drizzle.ts`, 'utf-8')
expect(output).toContain(
"date: timestamp('date', { mode: 'string', precision: 3 })"
)
Expand All @@ -39,11 +40,11 @@ test('global config', async () => {
test('field-level config', async () => {
const temp = await tempHandler.prepare()

await Bun.write(
fs.writeFileSync(
getSchemaPath(temp),
`datasource db {
provider = "postgresql"
url = env("PG_DATABASE_URL")
url = env("VITE_PG_DATABASE_URL")
}
generator drizzle {
Expand All @@ -58,9 +59,9 @@ test('field-level config', async () => {
stringDate DateTime
}`
)
await $`bun prisma generate --schema ${getSchemaPath(temp)}`.quiet()
execSync(`bun prisma generate --schema ${getSchemaPath(temp)}`)

const output = await Bun.file(`${temp.basePath}/drizzle.ts`).text()
const output = await fs.readFileSync(`${temp.basePath}/drizzle.ts`, 'utf-8')
expect(output).toContain(
"normalDate: timestamp('normalDate', { mode: 'date', precision: 3 })"
)
Expand Down
3 changes: 0 additions & 3 deletions packages/usage/tests/setup.ts

This file was deleted.

12 changes: 6 additions & 6 deletions packages/usage/tests/single-file-output.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { execSync } from 'node:child_process'
import fs from 'node:fs'
import { $ } from 'bun'
import { createTempHandler } from './utils/temp'

const tempHandler = createTempHandler()
Expand All @@ -11,39 +11,39 @@ afterAll(() => {
test('generates drizzle.ts', async () => {
const temp = await tempHandler.prepare()
writeSchemaWithOutput(temp.name, 'drizzle.ts')
await $`bun prisma generate --schema .temp/${temp.name}/schema.prisma`.quiet()
execSync(`bun prisma generate --schema .temp/${temp.name}/schema.prisma`)

expect(fs.existsSync(`.temp/${temp.name}/drizzle.ts`)).toBe(true)
})

test('generates ./drizzle.ts', async () => {
const temp = await tempHandler.prepare()
writeSchemaWithOutput(temp.name, './drizzle.ts')
await $`bun prisma generate --schema .temp/${temp.name}/schema.prisma`.quiet()
execSync(`bun prisma generate --schema .temp/${temp.name}/schema.prisma`)

expect(fs.existsSync(`.temp/${temp.name}/drizzle.ts`)).toBe(true)
})

test('generates sub/drizzle.ts', async () => {
const temp = await tempHandler.prepare()
writeSchemaWithOutput(temp.name, 'sub/drizzle.ts')
await $`bun prisma generate --schema .temp/${temp.name}/schema.prisma`.quiet()
execSync(`bun prisma generate --schema .temp/${temp.name}/schema.prisma`)

expect(fs.existsSync(`.temp/${temp.name}/sub/drizzle.ts`)).toBe(true)
})

test('generates ./sub/drizzle.ts', async () => {
const temp = await tempHandler.prepare()
writeSchemaWithOutput(temp.name, './sub/drizzle.ts')
await $`bun prisma generate --schema .temp/${temp.name}/schema.prisma`.quiet()
execSync(`bun prisma generate --schema .temp/${temp.name}/schema.prisma`)

expect(fs.existsSync(`.temp/${temp.name}/sub/drizzle.ts`)).toBe(true)
})

test('generates ./sub/multi/drizzle.ts', async () => {
const temp = await tempHandler.prepare()
writeSchemaWithOutput(temp.name, './sub/multi/drizzle.ts')
await $`bun prisma generate --schema .temp/${temp.name}/schema.prisma`.quiet()
execSync(`bun prisma generate --schema .temp/${temp.name}/schema.prisma`)

expect(fs.existsSync(`.temp/${temp.name}/sub/multi/drizzle.ts`)).toBe(true)
})
Expand Down
6 changes: 3 additions & 3 deletions packages/usage/turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
"tasks": {
"pushreset:postgres": {
"cache": false,
"env": ["PG_DATABASE_URL"]
"env": ["VITE_PG_DATABASE_URL"]
},
"pushreset:mysql": {
"cache": false,
"dependsOn": ["clone:mysql"],
"env": ["MYSQL_DATABASE_URL"]
"env": ["VITE_MYSQL_DATABASE_URL"]
},
"pushreset:sqlite": {
"cache": false,
"dependsOn": ["clone:sqlite"]
},
"test": {
"cache": false,
"env": ["PG_DATABASE_URL", "MYSQL_DATABASE_URL"],
"env": ["VITE_PG_DATABASE_URL", "VITE_MYSQL_DATABASE_URL"],
"dependsOn": [
"^build",
"pushreset:postgres",
Expand Down
7 changes: 7 additions & 0 deletions packages/usage/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
globals: true,
},
})

0 comments on commit e9a704b

Please sign in to comment.