Skip to content
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

Jest: Cannot find module '@lingui/loader!locales/de/messages.po' #1304

Open
tonytoqio opened this issue Nov 23, 2022 · 5 comments
Open

Jest: Cannot find module '@lingui/loader!locales/de/messages.po' #1304

tonytoqio opened this issue Nov 23, 2022 · 5 comments

Comments

@tonytoqio
Copy link

Describe the bug
When running a test using jest it fails to import the message catalog and throws the following error

Test suite failed to run
Cannot find module '@lingui/loader!locales/de/messages.po' from 'src/components/i18n/i18nService.js'

To Reproduce

import { i18n } from "@lingui/core";
import { de as dePlurals } from 'make-plural/plurals'
import catalogDe from '@lingui/loader!locales/de/messages.po';

i18n.loadLocaleData({ de: { plurals: dePlurals } });
i18n.load({ de: catalogDe.messages });
i18n.activate(DEFAULT_LANGUAGE);

export default i18n;

Expected behavior
That the message.po catalog is imported without any error

  • jsLingui version @lingui/loader@3.8.10
  • Jest version jest@29.0.3
  • Babel version @babel/core@7.14.3
  • Your Babel config
{
  "presets": [
    "@lingui/babel-preset-react",
    [
      "@babel/preset-env"
    ],
    [
      "@babel/preset-react",
      {
        "runtime": "automatic"
      }
    ]
  ],
  "plugins": [
    "macros",
    "@babel/plugin-syntax-dynamic-import",
    "@babel/plugin-proposal-class-properties",
    "@babel/plugin-transform-async-to-generator"
  ],
  "env": {
    "test": {
      "plugins": [
        "dynamic-import-node"
      ]
    }
  }
}
@thekip
Copy link
Collaborator

thekip commented Dec 12, 2022

@lingui/loader! syntax in @lingui/loader!locales/de/messages.po import is Webpack specific and would not work in Jest.

You either should compile your catalogs beforehand with lingui compile and import compiled js files or write a special module mapper for jest which will replace actual catalogs to some mocks.

Something like (I did not test that):

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
  moduleNameMapper: {
    '^@lingui/loader!': '<rootDir>/path/to/mock',
  },
// ...
};

@andrii-bodnar andrii-bodnar closed this as not planned Won't fix, can't repro, duplicate, stale Dec 29, 2022
@thekip
Copy link
Collaborator

thekip commented Dec 29, 2022

@andrii-bodnar I think some action points still needed here. Indeed if you use loader (webpack/snowpack/vite/etc) and don't have explicit compile step in your pipeline it is not that straightforward how to setup testing with jest. Compile catalogs before running tests and have separate setup for loading? Looks very complicated. I would suggest implement official transformer for jest the same way as loader works to be more aligned with the rest of tooling.

@andrii-bodnar andrii-bodnar reopened this Dec 29, 2022
@andrii-bodnar
Copy link
Contributor

@thekip thanks for the details! Reopened the issue

@gvillenave
Copy link

@thekip is addressing this still on the roadmap? Thanks!

@thekip
Copy link
Collaborator

thekip commented Oct 2, 2023

Not really. I personally test only with default language, others could just mock catalogs with stubs to make tests more independent of translations and thus more reliable. So i think using a transformer to compile catalogs in Jest would bring more harm them help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants