symfony / ux

Symfony UX initiative: a JavaScript ecosystem for Symfony
https://ux.symfony.com/
MIT License
787 stars 279 forks source link

[Translator] Issue when using with jest/typescript/pnp #1094

Open pkruithof opened 10 months ago

pkruithof commented 10 months ago

Apologies if this turns out to be a stupid configuration issue at our end, but hopefully this will help others besides us, if it gets resolved. Some background: we are using Yarn PnP and Zero-installs with Typescript, so our dependencies live in .yarn/cache/, committed in git, including the new ux-translator component. We have integrated it in our app and it's working great, the only issue is that Jest does not understand how to load it:

/app # yarn test translations
 FAIL  tests/jest/translations.test.ts
  ● 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:

    /app/.yarn/__virtual__/@symfony-ux-translator-virtual-6fd6e637ac/0/cache/@symfony-ux-translator-file-b4c39ccf49-0efdb98a62.zip/node_modules/@symfony/ux-translator/dist/translator_controller.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { IntlMessageFormat } from 'intl-messageformat';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      at Runtime.createScriptFromCode (../../.yarn/cache/jest-runtime-npm-29.6.4-1004e57255-93deacd06f.zip/node_modules/jest-runtime/build/index.js:1505:14)

I have tried all sorts of config tweaks, using both typescript and just javascript, different typescript loaders (currently ts-jest, but we tried babel as well). But honestly I don't know enough about frontend these days to actually debug and fix this. I hope that someone recognizes this error and can point me in the right direction to fix it. Because I doubt this is a bug in the translator code.

harkor commented 5 months ago

Exactly same issue for me, any clue ?

StevenRenaux commented 5 months ago

In your jest.config.js you can try and finally it work for me

    transform: {
        // Transform ES6 and TypeScript files
        '^.+\\.(js|jsx|ts|tsx)$': 'babel-jest',
    },transformIgnorePatterns: [
        // Do not ignore ES6 modules within node_modules that need to be transformed
        "node_modules/(?!(@symfony/ux-translator)/)"
    ],