Skip to content

Commit

Permalink
Merge pull request #332 from kenchi/cra-5
Browse files Browse the repository at this point in the history
Support for CRA 5
  • Loading branch information
patricklafrance committed Oct 12, 2021
2 parents b510d69 + b68f771 commit 3649bf1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const babelJest = require("babel-jest");
const babelJest = require("babel-jest").default;

const { isArray } = require("../../utils");
/**
Expand Down
7 changes: 5 additions & 2 deletions packages/craco/lib/features/jest/jest-babel-transform.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { loadCracoConfigAsync } = require("../../config");
const { createJestBabelTransform } = require("./create-jest-babel-transform");

let jestBabelTransform;
Expand All @@ -7,9 +8,11 @@ let jestBabelTransform;
// uses that to process files.
module.exports = {
...createJestBabelTransform(),
process(src, filename, config, transformOptions) {
async processAsync(src, filename, config, transformOptions) {
if (!jestBabelTransform) {
jestBabelTransform = createJestBabelTransform(config.globals._cracoConfig);
const context = { env: process.env.NODE_ENV };
const cracoConfig = await loadCracoConfigAsync(context);
jestBabelTransform = createJestBabelTransform(cracoConfig);
}

return jestBabelTransform.process(src, filename, config, transformOptions);
Expand Down
9 changes: 2 additions & 7 deletions packages/craco/lib/features/jest/merge-jest-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ const { projectRoot } = require("../../paths");

const BABEL_TRANSFORM_ENTRY_KEY = "^.+\\.(js|jsx|mjs|cjs|ts|tsx)$";

function overrideBabelTransform(jestConfig, cracoConfig, transformKey) {
// The cracoConfig needs to be available within the jest-babel-transform in order to honor its settings.
// This approach is based on https://github.com/facebook/jest/issues/1468#issuecomment-384825178
jestConfig.globals = jestConfig.globals || {};
jestConfig.globals._cracoConfig = cracoConfig;

function overrideBabelTransform(jestConfig, transformKey) {
jestConfig.transform[transformKey] = require.resolve("./jest-babel-transform");

log("Overrided Jest Babel transformer.");
Expand All @@ -27,7 +22,7 @@ function configureBabel(jestConfig, cracoConfig) {

if (isArray(presets) || isArray(plugins)) {
if (jestConfig.transform[BABEL_TRANSFORM_ENTRY_KEY]) {
overrideBabelTransform(jestConfig, cracoConfig, BABEL_TRANSFORM_ENTRY_KEY);
overrideBabelTransform(jestConfig, BABEL_TRANSFORM_ENTRY_KEY);
} else {
throw new Error(`craco: Cannot find Jest transform entry for Babel ${BABEL_TRANSFORM_ENTRY_KEY}.`);
}
Expand Down

0 comments on commit 3649bf1

Please sign in to comment.