cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
46.71k stars 3.16k forks source link

Update to `@faker-js/faker` 6.3.1 or later #30143

Closed MikeMcC399 closed 2 weeks ago

MikeMcC399 commented 2 weeks ago

What would you like?

  1. Update the npm module @faker-js/faker to minimum @faker-js/faker@6.3.1.
  2. Remove the use of the deprecated npm module @types/faker.

Why is this needed?

The repo https://github.com/Marak/Faker.js sourcing the npm module @types/faker was deleted and the module is de-facto now unsupported (see https://fakerjs.dev/about/announcements/2022-01-14.html#i-heard-something-happened-what-s-the-tldr).

As described in Migrating from Faker v5 to v6 in the TypeScript section

Faker now ships with its own types! Remove @types/faker from your package.json to avoid conflicts.

Other

Item Value
Deprecated npm module @types/faker
npm registry message This is a stub types definition. faker provides its own type definitions, so you do not need this installed.
Other message
GitHub repo https://github.com/Marak/Faker.js
Repo status Deleted
Replacement npm module https://www.npmjs.com/package/@faker-js/faker
Replacement repo https://github.com/faker-js/faker
Replacement documentation https://fakerjs.dev/
Migration docs https://v6.fakerjs.dev/migration-guide-v5/

Where used:

Directory Version
packages/app "@faker-js/faker": "5.5.3"
"@types/faker": "5.5.8"
packages/frontend-shared "@faker-js/faker": "5.5.3"
"@types/faker": "5.5.8"

Related PR

MikeMcC399 commented 2 weeks ago

After attempting an upgrade to @faker-js/faker@6.3.1 the following test fails:

$ yarn workspace @packages/frontend-shared check-ts
yarn workspace v1.22.22
yarn run v1.22.22
$ vue-tsc --noEmit
script/testStubSpecs.ts:140:7 - error TS2322: Type 'keyof T' is not assignable to type 'TemplateExecutor'.
  Type 'string | number | symbol' is not assignable to type 'TemplateExecutor'.
    Type 'string' is not assignable to type 'TemplateExecutor'.

140       template: faker.random.objectElement<TemplateExecutor>(nameTemplates),
          ~~~~~~~~

script/testStubSpecs.ts:140:44 - error TS2558: Expected 2 type arguments, but got 1.

140       template: faker.random.objectElement<TemplateExecutor>(nameTemplates),
                                               ~~~~~~~~~~~~~~~~

Found 2 errors in the same file, starting at: script/testStubSpecs.ts:140

error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed.
Exit code: 2

The code is here:

https://github.com/cypress-io/cypress/blob/b2a694fc65d4c66e24891c04444c5afcf0a35640/packages/frontend-shared/script/testStubSpecs.ts#L136-L141

Unfortunately I don't have the coding skills to know how to fix this. I'm just sharing what I discovered in my failed attempt to upgrade.

raygdev commented 2 weeks ago

@MikeMcC399 If you create a type

type NameTemplate = {
  readonly [key: string]: TemplateExecutor
}

Then pass it as the generic arguments to objectElement

{
  // ...
  // this will tell TS the return type of "objectElement" is a TemplateExecutor
  // which is what's expected.
  template: faker.random.objectElement<NameTemplate, keyof NameTemplate>(nameTemplates)
}

Alternatively you could use the objectValue method from the helpers module (also where the arrayElements method moved to) :

 {
   // ...
   template: faker.helpers.objectValue<NameTemplate>(nameTemplates),
 }

This should make typescript happy.

run the check-ts again after the changes, it should be good.

MikeMcC399 commented 2 weeks ago

@raygdev

Many thanks for your solution suggestions! Please feel free to submit a PR to resolve this issue. My goal was to prepare as much as I could for the list of deprecated dependencies, not to necessarily resolve all of them myself. You have better skills than I do for this issue, so it would make sense for you to take the lead.