Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(tests): use kont for tests #149

Merged
merged 13 commits into from
Sep 18, 2021
43 changes: 21 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,38 +69,40 @@
"@prisma/sdk": "^3.0.2",
"@types/debug": "^4.1.7",
"@types/expand-tilde": "^2.0.0",
"@types/jest": "26.0.24",
"@types/lodash": "^4.14.171",
"@types/node": "^16.4.3",
"@types/jest": "27.0.1",
"@types/lodash": "^4.14.173",
"@types/node": "^16.9.1",
"@types/pluralize": "^0.0.29",
"@types/semver": "^7.3.8",
"@types/strip-ansi": "^5.2.1",
"@typescript-eslint/eslint-plugin": "^4.28.5",
"@typescript-eslint/parser": "^4.28.5",
"arg": "^5.0.0",
"@typescript-eslint/eslint-plugin": "^4.31.1",
"@typescript-eslint/parser": "^4.31.1",
"arg": "^5.0.1",
"cross-env": "^7.0.3",
"dripip": "0.10.0",
"eslint": "^7.31.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-only-warn": "^1.0.3",
"execa": "^5.1.1",
"graphql": "^15.5.1",
"graphql": "^15.5.3",
"graphql-request": "^3.5.0",
"graphql-tag": "^2.12.5",
"jest": "27.0.6",
"jest": "27.2.0",
"jest-watch-select-projects": "^2.0.0",
"jest-watch-typeahead": "0.6.4",
"kont": "^0.2.0",
"markdown-toc": "^1.2.0",
"nexus": "^1.1.0",
"nodemon": "^2.0.12",
"prettier": "2.3.2",
"prettier": "2.4.0",
"prisma": "3.0.2",
"strip-ansi": "^6",
"ts-jest": "27.0.4",
"ts-node": "^10.1.0",
"type-fest": "^1.2.2",
"typescript": "4.3.5",
"zod": "^3.5.1"
"read-pkg-up": "7",
"strip-ansi": "6",
"ts-jest": "27.0.5",
"ts-node": "^10.2.1",
"type-fest": "^2.3.2",
"typescript": "4.4.3",
"zod": "^3.8.2"
},
"prettier": "@prisma-labs/prettier-config",
"peerDependencies": {
Expand All @@ -113,17 +115,14 @@
"optional": true
}
},
"resolutions": {
"comment-parser": "1.1.5"
},
"dependencies": {
"@prisma/generator-helper": "^3.0.2",
"debug": "^4.3.2",
"decimal.js": "^10.3.1",
"dindist": "^1.0.2",
"expand-tilde": "^2.0.2",
"fs-jetpack": "^4.1.0",
"graphql-scalars": "^1.10.0",
"fs-jetpack": "^4.1.1",
"graphql-scalars": "^1.10.1",
"kleur": "^4.1.4",
"lodash": "^4.17.21",
"ono": "^7.1.3",
Expand All @@ -132,7 +131,7 @@
"semver": "^7.3.5",
"setset": "^0.0.7",
"ts-replace-all": "^1.0.0",
"tslib": "^2.3.0"
"tslib": "^2.3.1"
},
"nodemonConfig": {
"events": {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const getPrismaClientDmmf = (packageLoader: {
prismaClientPackage = packageLoader.require()
} catch (error) {
// prettier-ignore
throw ono(error, dedent`
throw ono(error as Error, dedent`
Failed to get Prisma Client DMMF. An error occured while trying to import it from ${printedImportId}.
`)
}
Expand Down
173 changes: 0 additions & 173 deletions tests/__helpers__/testProject.ts

This file was deleted.

18 changes: 18 additions & 0 deletions tests/__providers__/graphqlClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { GraphQLClient } from 'graphql-request'
import { createDynamicProvider, Nothing } from 'kont'

export type Needs = Nothing

export type Contributes = {
graphQLClient: GraphQLClient
}

export const graphQLClient = () =>
createDynamicProvider<Needs, Contributes>((register) =>
register.before(() => {
const graphQLClient = new GraphQLClient(`http://localhost:4000/graphql`)
return {
graphQLClient,
}
})
)
103 changes: 103 additions & 0 deletions tests/__providers__/project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import * as Execa from 'execa'
import { createDynamicProvider } from 'kont'
import { Providers } from 'kont/providers'
import { merge } from 'lodash'
import readPkgUp from 'read-pkg-up'
import { PackageJson, TsConfigJson } from 'type-fest'
import { assertBuildPresent } from '../__helpers__/helpers'

type Project = {
thisPackageName: string
fixture: {
use(path: string): void
}
packageJson: {
merge(fields: PackageJson): void
create(fields: PackageJson): void
}
tsconfig: {
merge(fields: TsConfigJson): void
create(fields: TsConfigJson): void
}
}

export type Needs = Providers.Dir.Contributes & Providers.Run.Contributes

export type Contributes = Project

export const project = () =>
createDynamicProvider<Needs, Contributes>((register) =>
register.before((ctx) => {
assertBuildPresent()
Execa.commandSync(`yalc publish --no-scripts`)

const thisPackageJson = readPkgUp.sync({ cwd: __dirname })?.packageJson

if (!thisPackageJson) {
throw new Error(`Failed to get own package.json`)
}

const thisPackageName = thisPackageJson.name

const api: Project = {
thisPackageName,
fixture: {
use: (path) => {
ctx.fs.copy(path, ctx.fs.cwd(), {
overwrite: true,
})
},
},
packageJson: {
create: (packageJson) => {
const fileName = 'package.json'
ctx.fs.write(fileName, packageJson, { jsonIndent: 2 })
},
merge: (fields) => {
const fileName = 'package.json'
const PackageJson = ctx.fs.read(fileName, 'json')
const PackageJsonNew = merge(PackageJson, fields)
ctx.fs.write(fileName, PackageJsonNew, { jsonIndent: 2 })
},
},
tsconfig: {
create: (tsconfig) => {
const fileName = 'tsconfig.json'
ctx.fs.write(fileName, tsconfig, { jsonIndent: 2 })
},
merge: (fields) => {
const fileName = 'tsconfig.json'
const tsconfig = ctx.fs.read(fileName, 'json')
const tsconfigNew = merge(tsconfig, fields)
ctx.fs.write(fileName, tsconfigNew, { jsonIndent: 2 })
},
},
}

api.packageJson.create({
name: 'some-test-project',
version: '1.0.0',
})

api.tsconfig.create({
compilerOptions: {
strict: true,
target: 'ES2018',
module: 'CommonJS',
moduleResolution: 'node',
rootDir: 'src',
outDir: 'build',
esModuleInterop: true, // for ApolloServer b/c ws dep :(
},
include: ['src'],
})

// d(`starting project setup`)
// Execa.commandSync(`yalc publish --no-scripts`)
// ctx.runOrThrow(`yalc add ${thisPackageName}`)
// ctx.runOrThrow(`npm install --legacy-peer-deps`)
// d(`done project setup`)

return api
})
)
Loading