fluentui
fluentui copied to clipboard
[Bug]: Can't use @fluentui/web-components with jest tests
Library
Web Components (@fluentui/web-components)
System Info
System:
OS: macOS 14.5
CPU: (10) arm64 Apple M1 Pro
Memory: 154.98 MB / 32.00 GB
Shell: 5.9 - /bin/zsh
Browsers:
Chrome: 125.0.6422.141
Edge: 125.0.2535.79
Safari: 17.5
Are you reporting Accessibility issue?
no
Reproduction
https://codesandbox.io/p/github/morsh/svelte-typescript-jest/main?import=true
Bug Description
Actual Behavior
In test setup:
import { allComponents, provideFluentDesignSystem } from '@fluentui/web-components';
provideFluentDesignSystem().register(allComponents);
when running pnpm test:
SyntaxError: Unexpected token 'export'
Expected Behavior
Should run and pass tests.
Usually, with jest, you can set moduleNameMapper for a cjs module, but web-components don't supply one.
Logs
● Test suite failed to run
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
/Users/morshemesh/tmp/svelte-typescript-jest/node_modules/.pnpm/@[email protected]/node_modules/@fluentui/web-components/dist/esm/index.js:4
export * from './custom-elements';
^^^^^^
SyntaxError: Unexpected token 'export'
1 | import { fireEvent, render } from "@testing-library/svelte";
> 2 | import { allComponents, provideFluentDesignSystem } from '@fluentui/web-components';
Requested priority
Blocking
Products/sites affected
No response
Are you willing to submit a PR to fix?
yes
Validations
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [X] The provided reproduction is a minimal reproducible example of the bug.
+1 for this. I was just about to raise this issue but saw this.
I'm using an Angular project with a Jest test around it. I'll spend some time looking for a workaround and report back if I find one
I'm getting the same error but with @fluentui/react-components. I suspect the issue is within a dependency, since reverting the version of react-components did not fix the issue for me
Can't use it either from a sveltekit project. frustrating.
I was fighting with this issue in Angular project. I've tried transformIgnorePatterns and it does not work. I have tried also add babel to compile that library, but it did not work for me.
This issue has not had activity for over 180 days! We're adding Soft close label and will close it soon for house-keeping purposes. Still require assistance? Please add comment - "keep open".
@chrisdholt, @layershifter This issue hasn't been answered / addressed by anyone, please take a look.