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

using ts-jest causes error in @lingui/macro #1197

Closed
cameronbraid opened this issue Jan 20, 2022 · 3 comments
Closed

using ts-jest causes error in @lingui/macro #1197

cameronbraid opened this issue Jan 20, 2022 · 3 comments

Comments

@cameronbraid
Copy link

Describe the bug
Error is

 FAIL  src/App.test.tsx
  ● Test suite failed to run

    TypeError: /Users/cameronbraid/workspace-new/ts-jest-issue/examples/create-react-app/src/App.tsx: @lingui/macro: Cannot read properties of undefined (reading 'attributes') Learn more: https://www.npmjs.com/package/@lingui/macro

      at MacroJSX.stripMacroAttributes (node_modules/@lingui/macro/macroJsx.js:146:44)
          at Array.forEach (<anonymous>)

To Reproduce

git clone https://github.com/cameronbraid/js-lingui ts-jest-issue
cd ts-jest-issue
git checkout ts-jest-issue
cd examples/create-react-app
yarn
yarn jest
@HRDepartment
Copy link

I was able to fix this issue by setting compilerOptions.jsx to "preserve". When this is set to a value like "jsx", Babel TypeScript appears to do JSX transformations before handing over control to plugins like macros. Lingui's macro operates on the AST that still contains JSX nodes.

@cameronbraid
Copy link
Author

@caresx You Beauty ! Thanks heaps. Confirmed that it works for me in my other project too.

@molokovev
Copy link
Contributor

For ones having this issue, I did manage to set up ts-jest with lingui (though I still had to use babel-jest).

First, you need to change the compilerOptions['jsx'] to "preserve", as it was mentioned earlier. And then you should tell ts-jest to use babel (like this), so to put the lingui code though the babel-plugin-macros. Your babel config should contain all the stuff required for lingui, of course.

Both actions could be done only for tests in the jest.config.js like this:

globals: {
    'ts-jest': {
      tsconfig: {
        jsx: 'preserve',
      },
      babelConfig: true,
    },
  },

Be prepared to fix some babel-related errors (like variable declarations being or not being hoisted, read here) after the setup changes.

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