microsoft / fluentui

Fluent UI web represents a collection of utilities, React components, and web components for building web applications.
https://react.fluentui.dev
Other
18.5k stars 2.73k forks source link

[Bug]: Can't use @fluentui/web-components with jest tests #31543

Open morsh opened 5 months ago

morsh commented 5 months ago

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/@fluentui+web-components@2.6.1/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

jamesikanos commented 2 months ago

+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

WPMBE commented 2 months ago

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