-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Issues with Jest, Typescript and Emotion Babel Plugin #687
Comments
Hey @samcooke98 - I just gave it a quick spin and it works. With standard commonjs modules config for jest. Here's some guesswork: // This passes and snapshots looks ok
it('shorthand div', () => {
const Box = styled.div({
padding: '20px',
backgroundColor: 'silver',
})
const tree = renderer.create(<Box> Box </Box>).toJSON()
expect(tree).toMatchSnapshot()
}) I had same problem as you for a minute and it was connected with ENV variables, I'm pretty sure your's too. If you followed official docs your {
"env": {
"production": {
"plugins": [["emotion", { "hoist": true }]]
},
"development": {
"plugins": [["emotion", { "sourceMap": true, "autoLabel": true, "labelFormat": "⚡[local]" }]]
},
}
} And I'm pretty sure that without other configs when you run test your env could be set to
4?) I 'm also using |
I've created a repo here to show the issue. I tried what you suggested @vadistic but maybe I'm missing something, It didn't appear to solve the issue. Maybe it's an issue with babel versions? |
@samcooke98 I think it's too late but nvm. I solved this by following @vadistic 's solution but in my case, I still have es2015 modules set. |
Related: #344 |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. |
The problem seems to be that, because jest requires that modules are |
Is there any new to this? I've tried @vadistic advice but without success, i keep getting Component selectors can only be used in conjunction with babel-plugin-emotion. Also, if i change "module": "commonjs" to "module": "es2015" in my tsconfig.jest.json, jest throw this -->
This is frustrasting as hell |
The most likely issue that has caused is exactly what Jessica has described - module transformer running before babel. The issue got stale and repro case uses emotion@9 so I think it's appropriate to close this now - sorry that we couldn't get to it sooner. If the problem persists please report a new issue with a fresh repro case. Thank you for your understanding. |
emotion
version: 9.1.3react
version:16.3.2babel-plugin-emotion@9.1.2
What you did:
I have a project that uses Typescript & React with Babel for testing, and Webpack to build.
I had a basic component, using
emotion.div({styles}
, and was attempting to write a basic snapshot test.To accomplish the testing, I use TS-jest as recommended in the tsconfig repo. That is, according to the last couple of paragraphs here.
TS loader compiles the modules to commonjs (cjs). Then the babel plugin is run on the cjs code. At this point, I began to get errors and struggle.
What happened:
At this point, I was getting errors about needing to include the babel-plugin-emotion . This confused me, as I had it in my babelrc, and it was working when I built the code with Webpack.
It appears to be erroring because babel-plugin-emotion cannot run on cjs code.
Reproduction:
https://github.com/samcooke98/tsjest-emotion-issue
Problem description:
As ts-jest will silently update the module format to be commonjs, unless the user specifies a different one. According to #535 - Babel-plugin-emotion doesn't work on commonjs. This can result in confusing errors and difficulty getting the right configuration for Typescript and Jest.
Suggested solution:
I believe the first step would be to place something in the documentation about configuring Emotion with Typescript\Jest
I think a better solution, would be to make babel-plugin-emotion work with cjs code, but I am unsure of the technical complexity.
Hopefully, this is all clear.
The text was updated successfully, but these errors were encountered: