diff --git a/cli/configCra.js b/cli/configCra.js index 52d6adb4..31b8488b 100755 --- a/cli/configCra.js +++ b/cli/configCra.js @@ -1,10 +1,13 @@ #!/usr/bin/env node +// @ts-check // TODO: To revamp the CLI name (currently: config-cra. A proposal: `jest-preview config-cra`) const path = require('path'); const fs = require('fs'); // Append current node_modules to the module search path, so require('react-scripts') can work. module.paths.push(path.resolve(process.cwd(), './node_modules')); +// Create `jest.config.js` +// @ts-expect-error This is meant to run where react-scripts is installed const createJestConfig = require('react-scripts/scripts/utils/createJestConfig.js'); const jestConfig = createJestConfig( (filePath) => path.posix.join('', filePath), @@ -28,6 +31,7 @@ const jestConfigFileContent = `module.exports = ${JSON.stringify( 2, )}\n`; fs.writeFileSync('jest.config.js', jestConfigFileContent); +console.log(`Added jest.config.js to the project.`); // Try to prettier `jest.config.js` const execSync = require('child_process').execSync; @@ -36,3 +40,28 @@ try { } catch (error) { // Just ignore if user doesn't have prettier installed } + +// Create `scripts/test.js` +// https://github.com/facebook/create-react-app/blob/f99167c014a728ec856bda14f87181d90b050813/packages/react-scripts/scripts/eject.js#L158-L162 + +const testFile = path.resolve( + process.cwd(), + './node_modules/react-scripts/scripts/test.js', +); +let content = fs.readFileSync(testFile, 'utf8'); + +content = + content + // Remove dead code from .js files on eject + .replace( + /\/\/ @remove-on-eject-begin([\s\S]*?)\/\/ @remove-on-eject-end/gm, + '', + ) + // Require `env` direct from `react-scripts` + .replace( + `require('../config/env');`, + `require('react-scripts/config/env');`, + ) + .trim() + '\n'; +console.log(`Added scripts/test.js to the project.`); +fs.writeFileSync(path.resolve(process.cwd(), 'scripts/test.js'), content); diff --git a/examples/create-react-app/scripts/test.js b/examples/create-react-app/scripts/test.js index 161e6c16..b64daae2 100644 --- a/examples/create-react-app/scripts/test.js +++ b/examples/create-react-app/scripts/test.js @@ -1,6 +1,5 @@ -// TODO: Can we have a script then it would copy `react-scripts/scripts/test.js` => `scripts/test.js` then remove @remove-on-eject-begin -// Refer to how CRA ejects -// We can provide a script like `jest-preview create-cra-test` (TODO: Come up with a better name) +'use strict'; + // Do this as the first thing so that any code reading it knows the right env. process.env.BABEL_ENV = 'test'; process.env.NODE_ENV = 'test';