Skip to content

Commit

Permalink
Test require.context
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanBacon committed Jul 19, 2022
1 parent 091870a commit 16796e3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,13 @@ describe('require', () => {
expect(fn.mock.calls.length).toBe(1);
});

it('throws when using require.context directly', () => {
createModuleSystem(moduleSystem, false, '');
expect(() => moduleSystem.__r.context('foobar')).toThrow(
'The experimental Metro feature `require.context` is not enabled in your project.',
);
});

it('throws an error when trying to require an unknown module', () => {
createModuleSystem(moduleSystem, false, '');

Expand Down
2 changes: 1 addition & 1 deletion packages/metro-runtime/src/polyfills/require.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ metroRequire.importAll = metroImportAll;
// The `require.context()` syntax is never executed in the runtime because it is converted
// to `require()` in `metro/src/ModuleGraph/worker/collectDependencies.js` after collecting
// dependencies. If the feature flag is not enabled then the conversion never takes place and this error is thrown (development only).
metroRequire.context = () => {
metroRequire.context = function fallbackRequireContext() {
if (__DEV__) {
throw new Error(
`The experimental Metro feature \`require.context\` is not enabled in your project.\nThis can be enabled by setting the \`transformer.unstable_allowRequireContext\` property to \`true\` in your Metro configuration.`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe('DeltaCalculator', () => {
let fileWatcher;

const options = {
unstable_allowRequireContext: true,
experimentalImportBundleSupport: false,
onProgress: null,
resolve: (from: string, to: string) => {
Expand Down

0 comments on commit 16796e3

Please sign in to comment.