Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for CRA 5 #332

Merged
merged 1 commit into from Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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