question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Jest does not work with macros -- `build` babel config is not used for `test`

See original GitHub issue

Current Behavior

For example: Using styled-components/macros breaks the test. Using styled-components does not.

Suggested solution(s)

https://kulshekhar.github.io/ts-jest/user/config/babelConfig

Additional context

I can make a PR for this, but not sure how to tell it to find the babel config that tsdx uses

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
zenflowcommented, Feb 4, 2021

@iDavidB I got it to work but it was quite a while ago… I’m just looking at the setup in my project… Maybe you need to add the @babel/preset-env to your .babelrc… (Or since it was a while ago and I haven’t updated packages, possibly something has changed and the fix doesn’t work now for whatever reason.)

So try adding the @babel/preset-env to your .babelrc. I think without this, your babel config for testing doesn’t use @babel/preset-env at all, and therefore might lack some necessary plugins (e.g. syntax plugins), which could be the source of your errors.

.babelrc

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "10"
        }
      }
    ]
  ],
  "plugins": [
    ["macros"]
  ]
}

jest.config.js

module.exports = {
  globals: {
    'ts-jest': {
      babelConfig: '.babelrc',
    },
  },
}

Let me know if that does the trick! Otherwise, let us know what particular errors you are getting… It’s pretty hard to guess your problem without knowing the errors…

2reactions
arvigeuscommented, Dec 19, 2019

@ambroseus I was thinking the same, but I will need to untangle the config from that function. Haven’t done something like that before, should be fun

Read more comments on GitHub >

github_iconTop Results From Across the Web

How does Jest give babel.rc to babel-jest? - Stack Overflow
I looked at the function getBabelRC , and by console.logging, I seem to confirm that the correct .babelrc (with plugin: macros) is loaded...
Read more >
Babel Config option | ts-jest - GitHub Pages
ts-jest by default does NOT use Babel. ... The option is babelConfig and it works pretty much as the ... babelConfig: 'babelrc.test.js',
Read more >
Getting Started - Jest
Note: Jest documentation uses yarn commands, but npm will also work. ... It will not use development section like Babel does by default...
Read more >
Change Log - TSDX
The world's leading companies use TSDX to build and test TypeScript packages. ... use-cases like styled-components/macro , which previously did not work due ......
Read more >
TypeScript With Babel: A Beautiful Marriage - I Am Turns
No problem, Babel converts the code and makes everything a-okay. ... ts-jest, ts-karma, create-react-app-typescript, etc, and use the Babel ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found