Closed AndriyOnyshchenko closed 4 years ago
Thanks for filing an issue @AndriyOnyshchenko. Please use an issue template next time your file an issue — it ensures that all relevant information is included so that we can help you faster and the consistent format helps others to browse the issues.
It seems like Jest isn't able to parse the Circuit UI code, likely because we changed how it is transpiled in v2 (see the migration guide). Try including Circuit UI in Jest's transpilation by excluding it from the transformIgnorePatterns
:
// jest.config.js
"transformIgnorePatterns": ["/node_modules/(?!@sumup).+\\.js$"]
From https://github.com/facebook/jest/issues/6229#issuecomment-403539460
I fixed it by changing path to
import Toggle from '@sumup/circuit-ui/dist/es/components/Toggle/Toggle';
You could try import Toggle from '@sumup/circuit-ui/dist/cjs/components/Toggle/Toggle';
(note the cjs
) instead. However, using the absolute path to the component file has two major downsides: it prevents your bundler from choosing the best format (CJS vs ES) and this will break if the file structure in the /dist
folder changes (which is likely to happen eventually).
@connor-baer I have added current line "transformIgnorePatterns": ["/node_modules/(?!@sumup).+\\.js$"]
but it not helped.
About absolute path you are right, at some parts of application it fails. Thanks, didn't notice that.
So basically with imports from index jest gets undefined.
@AndriyOnyshchenko Hm, that's too bad. I would need to look at the code/project to be able to debug it further. Do you have a reproducible test case? Can you share the project?
:tada: This issue has been resolved in version 2.1.0-canary.3 :tada:
The release is available on:
Your semantic-release bot :package::rocket:
Update for future reference: @AndriyOnyshchenko provided access to the project which enabled me to debug the issue further. It turned out the problem wasn't solved with the bugfix above.
Rather, it was the configuration for absolute import paths that was too greedy. It matched all imports starting with Components
which included the ComponentsContext
component from Circuit UI. Changing the config to only match imports from the Components/
folder fixed the issue:
jest.config.js
- Components: '<rootDir>/client/static/react/components/',`
+ '^Components/(.*)$': '<rootDir>/client/static/react/components/$1',
:tada: This issue has been resolved in version 2.1.0-beta.1 :tada:
The release is available on:
Your semantic-release bot :package::rocket:
:tada: This issue has been resolved in version 2.1.0 :tada:
The release is available on:
Your semantic-release bot :package::rocket:
First problem I faced is that imports from index file not working in tests (like this
import { Toggle } from '@sumup/circuit-ui'
). In this case import statement returnsundefined
. I fixed it by changing path toimport Toggle from '@sumup/circuit-ui/dist/es/components/Toggle/Toggle';
Right now jest cannot import modules inside circuit component
/node_modules/@sumup/circuit-ui/dist/es/components/Toggle/Toggle.js:15 import React from 'react'; SyntaxError: Cannot use import statement outside a module
jest.config.js
babel.config.json
package.json dependencies