Skip to content

aliasJest causes my test suite to break #99

@jcpeden

Description

@jcpeden

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';
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions