Skip to content

Commit

Permalink
Merge pull request redwoodjs#691 from RobertBroersma/rb-api-test
Browse files Browse the repository at this point in the history
Allow configuring test database
  • Loading branch information
RobertBroersma authored Jul 1, 2020
2 parents d3710ff + 5a27400 commit 2d6f53e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages/cli/src/commands/dbCommands/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export const builder = (yargs) => {
yargs
.positional('name', {
description: 'Name of the migration',
type: 'array',
type: 'string',
default: 'migration',
})
.option('verbose', {
alias: 'v',
Expand All @@ -24,7 +25,7 @@ export const builder = (yargs) => {
)
}

export const handler = async ({ name, verbose = true }) => {
export const handler = async ({ name = 'migration', verbose = true }) => {
await runCommandTask(
[
{
Expand Down
7 changes: 5 additions & 2 deletions packages/cli/src/commands/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ export const builder = (yargs) => {
}

export const handler = async ({ side }) => {
const { base: BASE_DIR } = getPaths()
const { base: BASE_DIR, cache: CACHE_DIR } = getPaths()

const DB_URL = process.env.TEST_DATABASE_URL || `file:${CACHE_DIR}/test.db`

const execCommands = {
api: {
cwd: `${BASE_DIR}/api`,
cmd: 'yarn jest',
cmd: `DATABASE_URL=${DB_URL} yarn rw db up && yarn jest`,
args: [
'--passWithNoTests',
'--config ../node_modules/@redwoodjs/core/config/jest.config.api.js',
Expand Down
4 changes: 4 additions & 0 deletions packages/core/config/jest.config.api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// used by cli `rw test` command
// note: roodDir is a workaround for jest working directory weirdness

const path = require('path')

module.exports = {
resolver: 'jest-directory-named-resolver',
rootDir: process.cwd(),
setupFilesAfterEnv: [path.resolve(__dirname, './jest.setup.api.js')],
}
21 changes: 21 additions & 0 deletions packages/core/config/jest.setup.api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const path = require('path')

const camelCase = require('lodash/camelCase')
const { getPaths } = require('@redwoodjs/internal')

const redwoodPaths = getPaths()

const { db } = require(path.join(redwoodPaths.api.src, 'lib', 'db'))

/**
* Loop through all db models and clear the tables before every test
*/
beforeEach(async () => {
for (let model of db.dmmf.datamodel.models) {
await db[camelCase(model.name)].deleteMany()
}
})

afterAll(async () => {
await db.disconnect()
})
10 changes: 10 additions & 0 deletions packages/internal/src/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface BrowserTargetPaths {
}

export interface Paths {
cache: string
base: string
web: BrowserTargetPaths
api: NodeTargetPaths
Expand Down Expand Up @@ -103,8 +104,17 @@ export const resolveFile = (
export const getPaths = (BASE_DIR: string = getBaseDir()): Paths => {
const routes = resolveFile(path.join(BASE_DIR, PATH_WEB_ROUTES)) as string

// We store ambient type declerations and our test database over here.
const cache = path.join(BASE_DIR, 'node_modules', '.redwood')
try {
fs.mkdirSync(cache)
} catch (e) {
// noop
}

return {
base: BASE_DIR,
cache,
api: {
base: path.join(BASE_DIR, 'api'),
db: path.join(BASE_DIR, PATH_API_DIR_DB),
Expand Down

0 comments on commit 2d6f53e

Please sign in to comment.