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

aliasJest causes my test suite to break #99

Open
jcpeden opened this issue May 16, 2023 · 2 comments
Open

aliasJest causes my test suite to break #99

jcpeden opened this issue May 16, 2023 · 2 comments

Comments

@jcpeden
Copy link

jcpeden commented May 16, 2023

I'm switching an existing package that works without issue to use react-app-alias. My colleague and I were able to get the app running after setting a bunch of aliased paths correctly, however our unit test suite now fails to run.

We've not changed the tests or any of the code that they run against and have narrowed the problem down to aliasJest() or at least something in that area.

Prior to upgrading

Test suite runs without issue but the app doesn't run as our config-overrides.js is outside the project directory (it's shared across multiple apps).

Existing Jest config

This works prior to the intorduction of react-app-alias.

jest:  override(
      replaceJestIgnore('some_regex'),
      addJestAliases(aliases),
      replaceJestBabelTransform(appJestDir('babelTransform.js')),
      addJestSetupFile(appJestDir('setupRequireContext.js')),
    ),

After switching to use react-app-alias

If we don't touch config-overrides.js, our Jest overrides are not aware of the aliased paths so we get a bunch of errors like this:

Cannot find module 'components/ContentItem' from 'src/tests/components/ContentItem.spec.js

components is aliased in jsconfig.paths.json:

"components/*": ["src/components/*"],

New Jest config

This config gets me passed the 'cannot find module' errors but the tests fail as any Babel no longer transpiles any ES6 modules.

jest:  aliasJest(
   override(
      replaceJestIgnore('some_regex'),
      addJestAliases(aliases),
      replaceJestBabelTransform(appJestDir('babelTransform.js')),
      addJestSetupFile(appJestDir('setupRequireContext.js')),
    ),
),

After switching to use react-app-alias and aliasJest()

If I wrap my Jest overrides with aliasJest(), then the aliases work but Jest fails to transpile any ES6 modules e.g:

<project_path>/redesign/Avatar/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import Avatar from './Avatar';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      2 | import React, { useEffect } from 'react';
      3 | import PropTypes from 'prop-types';
    > 4 | import Avatar from '<project_path>/redesign/Avatar';
    
@jcpeden
Copy link
Author

jcpeden commented May 18, 2023

Managed to fix this with the help of a colleague. Wrapping my (formerly working) config in aliasJest() seemed not to work so instead but not doing that meant that our aliased paths were not being provided to Jest when the test suite ran.

To get around that issue, we (rather clumsily) extracted the aliased paths from aliasJest() and then spread them into the Jest config alonside the existing config. Open to input if there is a better way fo doing this and would love to know why aliasJest was killing my existing config.

jest: (config) => {
    const aliasedConfig = aliasJest()(config);
    const { moduleNameMapper } = aliasedConfig;

    const overriddenConfig = override(
      // Ignore all node modules, apart from scoped modules and non-transpiled deps
      replaceJestIgnore('<some regex to include certain packages from node_modules>'),
      replaceJestBabelTransform(appJestDir('<custom_babel_transform_file>')),
      addJestSetupFile(appJestDir('<add_require_context>')),
    )(config);

    return {
      ...overriddenConfig,
      moduleNameMapper,
    };
  },

@oklas
Copy link
Owner

oklas commented May 19, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants