Skip to content

Commit

Permalink
Checking jest config web api (#4529)
Browse files Browse the repository at this point in the history
* checking jest config web api

* changing from Error to console.error

* separate try catch

* function refactoring and line breaks

* path fixed

* const rwjsPaths not required

* Update packages/cli/src/commands/test.js

* Update packages/cli/src/commands/test.js

* mock config files

* mocking fs

* Update packages/cli/src/commands/__tests__/test.test.js

Co-authored-by: David Price <[email protected]>
  • Loading branch information
josemasar and thedavidprice authored Feb 26, 2022
1 parent 23bf323 commit 187e8f7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/cli/src/commands/__tests__/test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ jest.mock('execa', () =>
params,
}))
)

import execa from 'execa'

import { handler } from '../test'
Expand All @@ -19,6 +20,14 @@ jest.mock('@redwoodjs/structure', () => {
}
})

// Before rw tests run, api/ and web/ `jest.config.js` is confirmed via existsSync()
jest.mock('fs', () => {
return {
...jest.requireActual('fs'),
existsSync: () => true,
}
})

afterEach(() => {
jest.clearAllMocks()
})
Expand Down
27 changes: 27 additions & 0 deletions packages/cli/src/commands/test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import fs from 'fs'
import path from 'path'

import execa from 'execa'
import terminalLink from 'terminal-link'

Expand Down Expand Up @@ -27,6 +30,27 @@ function isInMercurialRepository() {
}
}

function isJestConfigFile(sides) {
for (let side of sides) {
try {
if (sides.includes(side)) {
if (!fs.existsSync(path.join(side, 'jest.config.js'))) {
console.error(
c.error(
`\nError: Missing Jest config file ${side}/jest.config.js` +
'\nTo add this file, run `npx @redwoodjs/codemods update-jest-config`\n'
)
)
throw new Error(`Error: Jest config file not found in ${side} side`)
}
}
} catch (e) {
errorTelemetry(process.argv, e.message)
process.exit(e?.exitCode || 1)
}
}
}

export const command = 'test [filter..]'
export const description = 'Run Jest tests. Defaults to watch mode'
export const builder = (yargs) => {
Expand Down Expand Up @@ -134,6 +158,9 @@ export const handler = async ({
jestArgs.push('--projects', ...sides)
}

//checking if Jest config files exists in each of the sides
isJestConfigFile(sides)

try {
const cacheDirDb = `file:${ensurePosixPath(
rwjsPaths.generated.base
Expand Down

0 comments on commit 187e8f7

Please sign in to comment.