{"id":15200001,"url":"https://github.com/syd-strzempko/cra_test-eject_require-context-workaround","last_synced_at":"2026-02-03T03:34:15.703Z","repository":{"id":248195140,"uuid":"828034523","full_name":"syd-strzempko/CRA_test-eject_require-context-workaround","owner":"syd-strzempko","description":"Ejected Create-React-App with require.context Jest Testing Support","archived":false,"fork":false,"pushed_at":"2024-07-13T00:29:57.000Z","size":207,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-10T23:57:42.521Z","etag":null,"topics":["babel","babel-plugin","create-react-app","jest","reactjs","webpack"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/syd-strzempko.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-13T00:07:26.000Z","updated_at":"2024-07-13T00:30:01.000Z","dependencies_parsed_at":"2024-07-13T01:32:21.598Z","dependency_job_id":"09d48f80-157e-469b-bd4a-ecc27f43c66f","html_url":"https://github.com/syd-strzempko/CRA_test-eject_require-context-workaround","commit_stats":null,"previous_names":["syd-strzempko/cra_test-eject_require-context-workaround"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/syd-strzempko/CRA_test-eject_require-context-workaround","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syd-strzempko%2FCRA_test-eject_require-context-workaround","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syd-strzempko%2FCRA_test-eject_require-context-workaround/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syd-strzempko%2FCRA_test-eject_require-context-workaround/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syd-strzempko%2FCRA_test-eject_require-context-workaround/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/syd-strzempko","download_url":"https://codeload.github.com/syd-strzempko/CRA_test-eject_require-context-workaround/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syd-strzempko%2FCRA_test-eject_require-context-workaround/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263165947,"owners_count":23424049,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["babel","babel-plugin","create-react-app","jest","reactjs","webpack"],"created_at":"2024-09-28T02:23:40.835Z","updated_at":"2026-02-03T03:34:15.641Z","avatar_url":"https://github.com/syd-strzempko.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Ejected Create React App require.context Workaround for Jest\n\n## Overview\n\nCreate-React-App instantiated from [create-react-app](https://create-react-app.dev/) and [ejected](https://create-react-app.dev/docs/available-scripts/#npm-run-eject)) for the purposes of advanced testing configuration.\n\nNamely, `require.context` which is provided as a [webpack function](https://webpack.js.org/guides/dependency-management/#requirecontext) is not recognized by jest and thus requires a workaround. As this is a common issue there are several Babel plugins available to resolve this issue (see [below](#alternatives)). The goal of this exercise was to provide a quick blueprint for how to set up this plugin from extant ejected CRA configuration.\n\n## Steps\n\nI elected to go with [babel-plugin-require-context-hook](https://www.npmjs.com/package/babel-plugin-require-context-hook) which required these additional setup steps:\n\n__Install package__\n```\nnpm i babel-plugin-require-context-hook\n```\n\n__Modify `config\\jest\\babelTransform.js`__\n```javascript\nmodule.exports = babelJest.createTransformer({\n    ...\n    babelrc: true,\n    ...\n});\n```\n\n__Create `.babelrc`__\n```\n{\n    \"env\": {\n      \"test\": {\n        \"plugins\": [\"require-context-hook\"]\n      }\n    }\n}\n```\n\nIn __package.json__, remove any extant babel config as duplicate (copy values to `.babelrc` to consolidate into one config)\n\nIn `src/setupTests.js`, add registration instantiation:\n\n```javascript\nimport registerRequireContextHook from 'babel-plugin-require-context-hook/register';\nregisterRequireContextHook();\n```\n\n### Alternatives\n\n[babel-plugin-transform-require-context](https://github.com/asapach/babel-plugin-transform-require-context)\n\n- Creates a dummy reference to `require.context` to replace when functionality is not needed\n\n[require-context.macro](https://www.npmjs.com/package/require-context.macro)\n\n- Utilizes Babel macros (a different flavor of global overrides?), also branched off of require-context-hook\n- For use with Storybook setups; but appears to essentially swap out `require.context` call for custom global `requireContext` call that checks availability of webpack function before providing a backup/mock(?) call\n\n[Alternatives to Ejecting](https://create-react-app.dev/docs/alternatives-to-ejecting/)\n\n- Alternatively, create-react-app encourages forking from `react-scripts` to custom-add scripts without the need to maintain all aspects of configuration long-term\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyd-strzempko%2Fcra_test-eject_require-context-workaround","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsyd-strzempko%2Fcra_test-eject_require-context-workaround","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyd-strzempko%2Fcra_test-eject_require-context-workaround/lists"}