Skip to content

Commit

Permalink
chore: test more unhappy paths, parallelize integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
timsuchanek committed Jul 2, 2020
1 parent 988d18f commit 22aa841
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 48 deletions.
1 change: 1 addition & 0 deletions src/packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
"set-value": "3.0.2",
"sql-template-tag": "3.4.0",
"stacktrace-parser": "0.1.10",
"stat-mode": "^1.0.0",
"strip-ansi": "6.0.0",
"strip-indent": "3.0.0",
"ts-jest": "25.5.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`runtime works blog example should succeed: blog example should succeed 1`] = `undefined`;
exports[`blog example should succeed: blog example should succeed 1`] = `undefined`;

exports[`runtime works blog-env example should not succeed: blog-env example should not succeed 1`] = `
exports[`blog example should succeed: blog-env example should not succeed 1`] = `
[Error: error: Environment variable not found: SQLITE_URL.
--> schema.prisma:3
|
Expand All @@ -13,18 +13,20 @@ exports[`runtime works blog-env example should not succeed: blog-env example sho
Validation Error Count: 1]
`;

exports[`runtime works blog-env-postgresql example should succeed: blog-env-postgresql example should succeed 1`] = `undefined`;
exports[`blog example should succeed: chmod example should succeed 1`] = `undefined`;

exports[`runtime works enums example should succeed: enums example should succeed 1`] = `undefined`;
exports[`blog example should succeed: enums example should succeed 1`] = `undefined`;

exports[`runtime works hooks example should succeed: hooks example should succeed 1`] = `undefined`;
exports[`blog-env-postgresql example should succeed: blog-env-postgresql example should succeed 1`] = `undefined`;

exports[`runtime works missing-binary example should not succeed: missing-binary example should not succeed 1`] = `[TypeError: e.message.split(...).slice(...).trim is not a function]`;
exports[`blog-env-postgresql example should succeed: hooks example should succeed 1`] = `undefined`;

exports[`runtime works missing-binary-native example should not succeed: missing-binary-native example should not succeed 1`] = `[TypeError: e.message.split(...).slice(...).trim is not a function]`;
exports[`blog-env-postgresql example should succeed: missing-binary example should not succeed 1`] = `[TypeError: e.message.split(...).slice(...).trim is not a function]`;

exports[`runtime works new-line example should succeed: new-line example should succeed 1`] = `undefined`;
exports[`blog-env-postgresql example should succeed: missing-binary-native example should not succeed 1`] = `[TypeError: e.message.split(...).slice(...).trim is not a function]`;

exports[`runtime works restart example should succeed: restart example should succeed 1`] = `undefined`;
exports[`blog-env-postgresql example should succeed: new-line example should succeed 1`] = `undefined`;

exports[`runtime works transaction example should succeed: transaction example should succeed 1`] = `undefined`;
exports[`blog-env-postgresql example should succeed: restart example should succeed 1`] = `undefined`;

exports[`blog-env-postgresql example should succeed: transaction example should succeed 1`] = `undefined`;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!dev.db
Binary file not shown.
33 changes: 33 additions & 0 deletions src/packages/client/src/__tests__/runtime-tests/chmod/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const { PrismaClient } = require('@prisma/client')
const assert = require('assert')
const { getPlatform } = require('@prisma/get-platform')
const fs = require('fs')
const path = require('path')
const stripAnsi = require('strip-ansi')

module.exports = async () => {
const platform = await getPlatform()
const binaryPath = path.join(
__dirname,
'node_modules/.prisma/client',
`query-engine-${platform}`,
)
fs.chmodSync(binaryPath, '644')

const prisma = new PrismaClient({
log: [
{
emit: 'event',
level: 'query',
},
],
})

await prisma.user.findMany()

prisma.disconnect()
}

if (require.main === module) {
module.exports()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"shouldSucceed": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
datasource db {
provider = "sqlite"
url = "file:dev.db"
default = true
}

generator client {
provider = "prisma-client-js"
}

// / User model comment
model User {
id String @default(uuid()) @id
email String @unique
// / name comment
name String?
posts Post[]
}

model Post {
id String @default(cuid()) @id
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
published Boolean
title String
content String?
authorId String?
author User? @relation(fields: [authorId], references: [id])
}
83 changes: 46 additions & 37 deletions src/packages/client/src/__tests__/runtime-tests/runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path'
import { generateInFolder } from '../../utils/generateInFolder'
import { promisify } from 'util'
import rimraf from 'rimraf'
import os from 'os'
const del = promisify(rimraf)

jest.setTimeout(35000)
Expand All @@ -11,50 +12,58 @@ jest.setTimeout(35000)

process.setMaxListeners(100)

describe('runtime works', () => {
const subDirs = getSubDirs(__dirname)
for (const dir of subDirs) {
const nodeModules = path.join(dir, 'node_modules')
const testName = path.basename(dir)
const shouldSucceed = shouldTestSucceed(dir)
let subDirs = getSubDirs(__dirname)
const folderFilter = process.argv.length > 3 ? process.argv[3] : null
if (folderFilter) {
subDirs = subDirs.filter((dir) => dir.includes(folderFilter))
console.log(
`As ${folderFilter} is provided, only ${subDirs.join(
', ',
)} is being tested`,
)
}

const testTitle = `${testName} example should${
shouldSucceed ? '' : ' not'
} succeed`
test(testTitle, async () => {
if (fs.existsSync(nodeModules)) {
await del(nodeModules)
}
const envVars = getEnvVars(dir)
process.env = { ...process.env, ...envVars }
for (const dir of subDirs) {
const nodeModules = path.join(dir, 'node_modules')
const testName = path.basename(dir)
const shouldSucceed = shouldTestSucceed(dir)

await generateInFolder({
projectDir: dir,
useLocalRuntime: false,
transpile: true,
})
const testTitle = `${testName} example should${
shouldSucceed ? '' : ' not'
} succeed`
test.concurrent(testTitle, async () => {
if (fs.existsSync(nodeModules)) {
await del(nodeModules)
}
const envVars = getEnvVars(dir)
process.env = { ...process.env, ...envVars }

await generateInFolder({
projectDir: dir,
useLocalRuntime: false,
transpile: true,
})

if (envVars) {
for (const key of Object.keys(envVars)) {
delete process.env[key]
}
if (envVars) {
for (const key of Object.keys(envVars)) {
delete process.env[key]
}
}

const filePath = path.join(dir, 'index.js')
const fn = require(filePath)
const filePath = path.join(dir, 'index.js')
const fn = require(filePath)

if (shouldSucceed) {
expect(await fn()).toMatchSnapshot(testTitle)
} else {
try {
await fn()
} catch (e) {
expect(e).toMatchSnapshot(testTitle)
}
if (shouldSucceed) {
expect(await fn()).toMatchSnapshot(testTitle)
} else {
try {
await fn()
} catch (e) {
expect(e).toMatchSnapshot(testTitle)
}
})
}
})
}
})
}

function getSubDirs(dir: string): string[] {
const files = fs.readdirSync(dir)
Expand Down
2 changes: 1 addition & 1 deletion src/packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"scripts"
],
"prisma": {
"version": "f5632770336d9cd89b6fa90204c3decf335c065a"
"version": "c65f7b5d18ee8dbd0f43f654571bd3e85b625d80"
},
"devDependencies": {
"@types/jest": "25.2.3",
Expand Down
8 changes: 8 additions & 0 deletions src/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 22aa841

Please sign in to comment.