Skip to content

Commit

Permalink
deps: upgrade to latest prisma & fix tests (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Jun 21, 2021
1 parent 7d1c18d commit aad2e2f
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 112 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"build:module-facades": "ts-node scripts/build-module-facades",
"build": "yarn clean && yarn build:module-facades && tsc",
"test": "cross-env DEBUG=e2e jest",
"test:ci": "cross-env DEBUG=e2e jest --coverage --forceExit",
"test:ci": "cross-env DEBUG=e2e jest --coverage --forceExit --runInBand",
"tdd": "jest --watch",
"tdd:e2e:debug": "cross-env test_project_reuse=true jest --watch e2e",
"clean": "rm -rf dist && rm -rf node_modules/.cache",
Expand All @@ -44,8 +44,8 @@
"devDependencies": {
"@homer0/prettier-plugin-jsdoc": "^4.0.0",
"@prisma-labs/prettier-config": "0.1.0",
"@prisma/client": "2.24.0",
"@prisma/sdk": "^2.24.0",
"@prisma/client": "2.25.0",
"@prisma/sdk": "^2.25.0",
"@types/debug": "^4.1.5",
"@types/jest": "26.0.23",
"@types/lodash": "^4.14.170",
Expand All @@ -71,7 +71,7 @@
"nexus": "^1.0.0",
"nodemon": "^2.0.7",
"prettier": "2.3.1",
"prisma": "2.24.0",
"prisma": "2.25.0",
"strip-ansi": "^6",
"ts-jest": "27.0.3",
"ts-node": "^10.0.0",
Expand All @@ -81,11 +81,11 @@
},
"prettier": "@prisma-labs/prettier-config",
"peerDependencies": {
"@prisma/client": "2.17.x || 2.18.x || 2.19.x || 2.20.x || 2.21.x || 2.22.x || 2.23.x || 2.24.x",
"@prisma/client": "2.17.x || 2.18.x || 2.19.x || 2.20.x || 2.21.x || 2.22.x || 2.23.x || 2.24.x || 2.25.x",
"nexus": "^1.0.0"
},
"dependencies": {
"@prisma/generator-helper": "^2.24.0",
"@prisma/generator-helper": "^2.25.0",
"debug": "^4.3.1",
"dindist": "^1.0.2",
"fs-jetpack": "^4.1.0",
Expand Down
122 changes: 78 additions & 44 deletions tests/__helpers__/testers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as fs from 'fs-jetpack'
import { DocumentNode, execute, printSchema } from 'graphql'
import { core } from 'nexus'
import { AllNexusTypeDefs } from 'nexus/dist/core'
import * as Path from 'path'
import { generateRuntime } from '../../src/generator/generate'
import { Gentime } from '../../src/generator/gentime/settingsSingleton'
import * as ModelsGenerator from '../../src/generator/models'
Expand Down Expand Up @@ -109,44 +110,6 @@ export function testGraphqlSchema(params: {
* Low Level
*/

export function createPrismaSchema({
content,
datasourceProvider = {
provider: 'postgres',
url: 'env("DB_URL")',
},
clientOutput,
nexusPrisma = true,
}: {
content: string
datasourceProvider?: { provider: 'sqlite'; url: string } | { provider: 'postgres'; url: string }
clientOutput?: string
nexusPrisma?: boolean
}): string {
return dedent`
datasource db {
provider = "${datasourceProvider.provider}"
url = ${datasourceProvider.url}
}
generator client {
provider = "prisma-client-js"${clientOutput ? `\noutput = ${clientOutput}` : ''}
}
${
nexusPrisma
? dedent`
generator nexusPrisma {
provider = "nexus-prisma"
}
`
: ``
}
${content}
`
}

/**
* Given a Prisma schema and Nexus type definitions return a GraphQL schema.
*/
Expand All @@ -157,22 +120,30 @@ export async function integrationTest({
apiClientQuery,
}: IntegrationTestParams) {
const dir = fs.tmpDir().cwd()
const prismaClientImportId = `${dir}/client`
const dirToBinDirRelativePath = Path.relative(
Path.join(dir, '/force-dotdot'),
Path.join(process.cwd(), 'node_modules/.bin')
)
const dirClientOutput = Path.join(dir, 'client')
const prismaSchemaContents = createPrismaSchema({
content: datasourceSchema,
datasourceProvider: {
provider: 'sqlite',
url: `"file:./db.sqlite"`,
url: `file:./db.sqlite`,
},
nexusPrisma: false,
clientOutput: `"./client"`,
clientOutput: dirClientOutput.replace(/\\/g, '/').replace(/^[A-za-z]:/, ''),
// clientOutput: `./client`,
})

fs.write(`${dir}/schema.prisma`, prismaSchemaContents)

execa.commandSync(`yarn -s prisma db push --force-reset --schema ${dir}/schema.prisma`)
// This will run the prisma generators
execa.commandSync(`${dirToBinDirRelativePath}/prisma db push --force-reset --schema ${dir}/schema.prisma`, {
cwd: dir,
})

const prismaClientPackage = require(prismaClientImportId)
const prismaClientPackage = require(dirClientOutput)
const prismaClient = new prismaClientPackage.PrismaClient()
await datasourceSeed(prismaClient)

Expand All @@ -184,7 +155,7 @@ export async function integrationTest({
...settingsDefaults,
gentime: {
...settingsDefaults.gentime,
prismaClientImportId: prismaClientImportId,
prismaClientImportId: dirClientOutput,
},
}) as any

Expand Down Expand Up @@ -229,6 +200,69 @@ function stripNexusQueryOk(sdl: string): string {
)
}

/**
* Create the contents of a Prisma Schema file.
*/
export function createPrismaSchema({
content,
datasourceProvider,
clientOutput,
nexusPrisma,
}: {
content: string
/**
* The datasource provider block. Defaults to postgres provider with DB_URL envar lookup.
*/
datasourceProvider?: { provider: 'sqlite'; url: string } | { provider: 'postgres'; url: string }
/**
* Specify the prisma client generator block output configuration. By default is unspecified and uses the Prisma client generator default.
*/
clientOutput?: string
/**
* Should the Nexus Prisma generator block be added.
*
* @default true
*/
nexusPrisma?: boolean
}): string {
const outputConfiguration = clientOutput ? `\n output = "${clientOutput}"` : ''
const nexusPrisma_ = nexusPrisma ?? true
const datasourceProvider_ = datasourceProvider
? {
...datasourceProvider,
url: datasourceProvider.url.startsWith('env')
? datasourceProvider.url
: `"${datasourceProvider.url}"`,
}
: {
provider: 'postgres',
url: 'env("DB_URL")',
}

return dedent`
datasource db {
provider = "${datasourceProvider_.provider}"
url = ${datasourceProvider_.url}
}
generator client {
provider = "prisma-client-js"${outputConfiguration}
}
${
nexusPrisma_
? dedent`
generator nexusPrisma {
provider = "nexus-prisma"
}
`
: ``
}
${content}
`
}

/**
* For the given Prisma Schema generate the derived source code.
*/
Expand Down
Loading

0 comments on commit aad2e2f

Please sign in to comment.