From 187e8f791477b24320c885319764b000f2f5c6fb Mon Sep 17 00:00:00 2001 From: Josema Sar <65446320+josemasar@users.noreply.github.com> Date: Sun, 27 Feb 2022 00:44:09 +0100 Subject: [PATCH] Checking jest config web api (#4529) * 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 --- .../cli/src/commands/__tests__/test.test.js | 9 +++++++ packages/cli/src/commands/test.js | 27 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/packages/cli/src/commands/__tests__/test.test.js b/packages/cli/src/commands/__tests__/test.test.js index 3f11ba1617a5..d38f60d8a3d6 100644 --- a/packages/cli/src/commands/__tests__/test.test.js +++ b/packages/cli/src/commands/__tests__/test.test.js @@ -7,6 +7,7 @@ jest.mock('execa', () => params, })) ) + import execa from 'execa' import { handler } from '../test' @@ -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() }) diff --git a/packages/cli/src/commands/test.js b/packages/cli/src/commands/test.js index 61d595e76014..c46559383db8 100644 --- a/packages/cli/src/commands/test.js +++ b/packages/cli/src/commands/test.js @@ -1,3 +1,6 @@ +import fs from 'fs' +import path from 'path' + import execa from 'execa' import terminalLink from 'terminal-link' @@ -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) => { @@ -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