Skip to content

Babel plugins not applying to Jest #183

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

Closed
dustinlacewell opened this issue Aug 10, 2020 · 4 comments
Closed

Babel plugins not applying to Jest #183

dustinlacewell opened this issue Aug 10, 2020 · 4 comments

Comments

@dustinlacewell
Copy link

I have

    babel: {
        plugins: [
            ['@babel/plugin-transform-typescript', {
                allowNamespaces: true,
            }],
        ]
    },

in my craco.config.js and this allows me to properly build my project. However, when I run npm run test I get the following error:

 FAIL  src/music/IntervalQuality/index.test.ts
  ● Test suite failed to run

    SyntaxError: /home/ldlework/src/shapes-ui/src/music/IntervalQuality/index.ts: Namespace not marked type-only declare. Non-declarative namespaces are only supported experimentally in Babel. To enable and review caveats see: https://babeljs.io/docs/en/babel-plugin-transform-typescript

      78 | }
      79 | 
    >80 | export namespace IntervalQuality {
           |                  ^^^^^^^^^^^^^^^
      81 |     export function fromString(name: string): NullableIQ {
      82 |         return qualityFromAbbr[name] || qualityFromName[name.toLowerCase()];
      83 |     }

What do I need to do so that the Babel plugin is used in the context of testing?

@stale
Copy link

stale bot commented Oct 9, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Oct 9, 2020
@stale stale bot closed this as completed Oct 16, 2020
@nmorozov
Copy link

@dustinlacewell Hi. I faced the same problem. I use linaria and CracoLinariaPlugin (it includes babel preset) and it works for project build, but not for tests. How did you resolve this problem?

@AntonioRedondo
Copy link

AntonioRedondo commented Dec 21, 2021

I have a similar issue with CRACO config not working on tests.

CRACO config added to use webpack aliases works fine with craco start. But on tests running craco test aliases fail with the error Cannot find module '@alias/file' from 'src/Component.tsx'.

#294 seems like a related issue to this.

I tried to create a repo to reproduce the error. But CRACO 6.4.3 isn't compatible with CRA 5.0.0. And if I try to use CRA 4.0.3, the CRA installation stops because it says I'm using an old version of CRA. 🤪

I'll try to reproduce the bug once CRACO is compatible with CRA 5.0.0.

@AntonioRedondo
Copy link

AntonioRedondo commented Jan 5, 2022

For those who are having issues with aliases on Jest with CRACO, they can work with the below addition on package.json:

package.json:

{
  "name": "my-project",
  "scripts": {
    "start": "craco start",
    "build": "craco build",
    "test": "craco test",
    "eject": "craco eject"
  },
  "jest": {
    "moduleNameMapper": {
      "@/(.+)": "<rootDir>/src/$1"
    }
  }
}

Then normally running the tests with npm t. No need to create a jest.config.js file and using the createJestConfig(cracoConfig) function.

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

3 participants