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 fails to import detectors when testing in NodeJS #140

Closed
phil714 opened this issue Dec 13, 2021 · 9 comments
Closed

Jest fails to import detectors when testing in NodeJS #140

phil714 opened this issue Dec 13, 2021 · 9 comments
Labels
bug Something isn't working external-issue An issue that caused by external packages and can't be fixed in this repo

Comments

@phil714
Copy link

phil714 commented Dec 13, 2021

Describe the bug

Hi,

I've been trying to run my test since I added typesafe-i18n to my backend but I've been running into a problem. While testing manually to see if the translations were working right, everything seems to be working well but while running the tests, my tests that were testing files that used the translation were failing. It seems to fail to import the detectors in the utils file and causing everything else to fail.

Reproduction

cross-env NODE_ENV=test jest --verbose --maxWorkers=3

Logs

FAIL  tests/integration/mutations/productMutation/productCreate.test.ts
  ● Test suite failed to run

    Cannot find module 'typesafe-i18n/detectors' from 'src/i18n/i18n-util.ts'

    Require stack:
      src/i18n/i18n-util.ts
      src/i18n/i18n-node.ts
      src/i18n/index.ts
      src/graphql/resolvers/auditLogQuery/resolvers.ts
      src/graphql/resolvers/query/resolvers.ts
      src/graphql/index.ts
      tests/helpers/apolloServerHelper.ts
      tests/integration/mutations/productMutation/productCreate.test.ts

       6 | import type { Translation, TranslationFunctions, Formatters, Locales } from './i18n-types'
       7 | import type { LocaleDetector } from 'typesafe-i18n/detectors'
    >  8 | import { detectLocale as detectLocaleFn } from 'typesafe-i18n/detectors'
         | ^
       9 | import { initFormatters } from './formatters'
      10 |
      11 | export const baseLocale: Locales = 'en'

      at Resolver.resolveModule (node_modules/jest-resolve/build/resolver.js:311:11)
      at Object.<anonymous> (src/i18n/i18n-util.ts:8:1)

Config

{
   "adapter": "node",
   "loadLocalesAsync": false,
   "$schema": "https://unpkg.com/typesafe-i18n@2.53.2/schema/typesafe-i18n.json"
}

Additional information

I'm on MacOS and using NodeJS 16.9.0 and we're using Jest 27.0.4 & cross-env 7.0.3 for the project.

I also tried adding typesafe-i18n --no-watch && in front of my test command to generate the translations before the tests but with no result. My suspicion is that there is a problem with how the subdirectory is imported (or setup on your side, I'm not really sure) because it fails at the line where the detector is imported but not the one where the i18nString, i18nObject function are imported.

We also added a small wrapper around i18nObject which is why it's going through src/i18n/index.ts.

// src/i18n/index.ts
import { i18nObject } from './i18n-node';

export const LL = i18nObject;

We don't use the detector but we can't remove this particular code because it is generated each time. I'm a bit at a lost on what to do honestly so if you have any ideas on what can causes this, let me know.

@phil714 phil714 added the bug Something isn't working label Dec 13, 2021
@ivanhofer
Copy link
Owner

Hi @phil714,
thanks for creating this issue.

I think this is still the same issue as described here:
#71 (comment).
It seems that jest does not support modern import resolutions.
Nobody could provide me with more details so I closed the issue back then.

I'm no jest user, so I don't really know how to start investigating this.
Could you please create a small repo, with jest so I can test a few things?

As a workaround you could enable the generateonlytypes option and then manually remove the detector-import from the i18n-util.ts file.
When running the generator again, only the i18n-types.ts file get's recreated.

@phil714
Copy link
Author

phil714 commented Dec 14, 2021

Hi,

Thank you for the quick response.

I've tried upgrading all our Jest-related librairies to the latest version and the issue seems to remain. I have looked at some issues linked in your other comments and like you said, (from what I understood) it doesn't look like their library support ESM resolution. It look like they are working on it but it might take a while.

jestjs/jest#9430
jestjs/jest#9771

I think for now I'll follow your advice of adding the flag and removing the detector from my util file. If you still think this is worth investigating on your side, I can make a small test repository with the problem.

@ivanhofer
Copy link
Owner

Would be great if you could provide me with an example repo, so I can take a look at how easy or hard it is to fix it 😊

@phil714
Copy link
Author

phil714 commented Dec 16, 2021

Here goes @ivanhofer https://github.com/phil714/stunning-octo-chainsaw-jest-typesafe-i18n-test.

I've manage to reproduce it with minimal configuration. You should just have to install everything doing npm i and then run the test with npm run test. I've copied a large part of our tsconfig.json file since I'm honestly not an expert on how all of these options can affect the problems or not. Let me know if there's more I can do.

@ivanhofer
Copy link
Owner

@phil714 thanks for the repo! I will take a deeper look when I find some time :)

@thanoskrg
Copy link

Not sure whether this is the same issue I had with react where jest could not find module typesafe-i18n/adapters/adapter-react but this is how I managed to solve it by using the moduleNameMapper config option likewise:

// jest.config.js

module.exports = {
  preset: "ts-jest",
  testEnvironment: "jsdom",
  transformIgnorePatterns: ["node_modules"],
  setupFilesAfterEnv: ["<rootDir>/setupTests.ts"],
  moduleNameMapper: {
    "typesafe-i18n/adapters/(.*)": "typesafe-i18n/adapters/$1.cjs",
    "typesafe-i18n/detectors": "typesafe-i18n/detectors/index.cjs",
  },
};

Hope this helps.

@ivanhofer
Copy link
Owner

@thanoskrg thanks for sharing your solution 👍

@ivanhofer
Copy link
Owner

@phil714 I just tested the moduleNameMapper solution above and it works for me. Can you please verify if this is also working in your project?

@ivanhofer ivanhofer added the external-issue An issue that caused by external packages and can't be fixed in this repo label Dec 27, 2021
@ivanhofer
Copy link
Owner

I have added a troubleshoot section to the docs: https://github.com/ivanhofer/typesafe-i18n#tests-are-not-running-with-jest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working external-issue An issue that caused by external packages and can't be fixed in this repo
Projects
None yet
Development

No branches or pull requests

3 participants