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

After updating faker, jest tests started failing #2978

Closed
8 of 10 tasks
Yamadetta opened this issue Jun 27, 2024 · 10 comments
Closed
8 of 10 tasks

After updating faker, jest tests started failing #2978

Yamadetta opened this issue Jun 27, 2024 · 10 comments
Labels
question Further information is requested

Comments

@Yamadetta
Copy link

Yamadetta commented Jun 27, 2024

Pre-Checks

Describe the bug

The faker package was updated in the project from 8.0.0 to 8.1.0.
Now, if you use the following import in jest tests:

import { faker } from '@faker-js/faker/locale/en'

or

import { faker } from '@faker-js/faker/locale/ru'

you get the following error:

 FAIL  store/__tests__/profiles.test.ts
  ● Test suite failed to run

    Cannot find module '@faker-js/faker/locale/en' from 'store/__mocks__/profile-details.mock.ts'

    Require stack:
      store/__mocks__/profile-details.mock.ts
      store/__tests__/profiles.test.ts

    > 1 | import { faker } from '@faker-js/faker/locale/en'

If you use the following, there is no error:

import { faker } from '@faker-js/faker'

There are no additional configurations for faker in jest.config.js.

Minimal reproduction code

No response

Additional Context

No response

Environment Info

System:
    OS: macOS 14.5
    CPU: (10) arm64 Apple M2 Pro
    Memory: 543.39 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.12.2 - ~/.nvm/versions/node/v20.12.2/bin/node
    npm: 10.5.0 - ~/.nvm/versions/node/v20.12.2/bin/npm
  Browsers:
    Chrome: 126.0.6478.127
    Safari: 17.5
  npmPackages:
    @faker-js/faker: ^8.1.0 => 8.1.0

Which module system do you use?

  • CJS
  • ESM

Used Package Manager

npm

@Yamadetta Yamadetta added c: bug Something isn't working s: pending triage Pending Triage labels Jun 27, 2024
@ST-DDT
Copy link
Member

ST-DDT commented Jun 27, 2024

I'm unable to reproduce the error (outside of jest).

import { faker } from '@faker-js/faker/locale/en';
import pack from './node_modules/@faker-js/faker/package.json' assert { type: 'json' };

console.log(faker.helpers.arrayElement(['test']), pack.version);
$ pnpm exe

> faker-test@ exe ~/git/Faker/faker-playground
> tsx index.ts

test 8.1.0
$ pnpm exe

> faker-test@ exe ~/git/Faker/faker-playground
> tsx index.ts

test 8.4.1

Please also check the latest version of faker v8.4.1.
If that is also affected, please check which version switch breaks it exactly (v8.0.0 has a few patch versions).

@ST-DDT ST-DDT added the s: awaiting more info Additional information are requested label Jun 27, 2024
@Yamadetta
Copy link
Author

Yamadetta commented Jun 27, 2024

The error occurs when upgrading from 8.0.2 to 8.1.0.
The error persists in version 8.4.1.
Perhaps this is a similar problem
https://stackoverflow.com/questions/77266356/unable-to-import-faker-locale-sub-package-in-react-app-react-scripts-v4

@ST-DDT
Copy link
Member

ST-DDT commented Jun 27, 2024

Could you please also test our latest alpha?

Please create a minimal reproduction.

@Yamadetta
Copy link
Author

When using the method discussed on Stack Overflow above, the error disappears. Specifically:

import { fakerRU } from '@faker-js/faker'

Instead of:

import { faker } from '@faker-js/faker/locale/ru'

The problem persists in the latest alpha.
Jest version: 27.5.1

@ST-DDT
Copy link
Member

ST-DDT commented Jun 27, 2024

Caused by 3df98a0
Issue?: Webpack 4 does not support export field.
Does jest v27 use webpack v4?

Does the following work for you?
import { faker } from '@faker-js/faker/dist/esm/locale/en';

@Yamadetta
Copy link
Author

In this case, an error occurs:

> jest store/__tests__/profiles.test.ts

 FAIL  store/__tests__/profiles.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:

    /Users/someUser/Documents/work/company/ui/website/node_modules/@faker-js/faker/dist/esm/locale/en.mjs:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import{a}from"../chunk-3LPAMF3R.mjs";import"../chunk-LSZKOVGW.mjs";import"../chunk-GTEBSQTL.mjs";export{a as faker};
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

    > 1 | import { faker } from '@faker-js/faker/dist/esm/locale/en'

Fixed by adding transformIgnorePatterns node_modules/@faker to jest.config.js
But I'm not sure if this is right

@ST-DDT ST-DDT removed the s: awaiting more info Additional information are requested label Jun 27, 2024
@ST-DDT
Copy link
Member

ST-DDT commented Jun 27, 2024

If you are using CJS code, then you have to adjust the import accordingly.

v8.1.0 is also the first version that uses node 14 features (AFAICT).

Note: fakerjs-v9 (currently alpha) has improved tree-shaking, so that the imports with paths are no longer needed.

@Yamadetta
Copy link
Author

Yamadetta commented Jun 27, 2024

If you are using CJS code, then you have to adjust the import accordingly.

Please describe in more detail what you mean

In this case, do I need to import { fakerRU } for localization?

@ST-DDT
Copy link
Member

ST-DDT commented Jun 27, 2024

If you are using CJS code, then you have to adjust the import accordingly.

Please describe in more detail what you mean

import { faker } from '@faker-js/faker/dist/cjs/locale/ru';

alternatively:

import { fakerRU } from '@faker-js/faker';

You might still run into troubles, because of JS Features > Node 12.

@Yamadetta
Copy link
Author

Got it, thank you!

@ST-DDT ST-DDT added question Further information is requested and removed c: bug Something isn't working s: pending triage Pending Triage labels Jun 27, 2024
@ST-DDT ST-DDT closed this as completed Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants