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

Add a documentation page for how to use faker with testing frameworks #1324

Closed
Shinigami92 opened this issue Sep 2, 2022 · 11 comments · Fixed by #1623
Closed

Add a documentation page for how to use faker with testing frameworks #1324

Shinigami92 opened this issue Sep 2, 2022 · 11 comments · Fixed by #1623
Assignees
Labels
c: docs Improvements or additions to documentation good first issue Good for newcomers p: 1-normal Nothing urgent s: accepted Accepted feature / Confirmed bug
Milestone

Comments

@Shinigami92
Copy link
Member

We would like to have a documentation page that contains examples how to use faker in combination with testing frameworks like vitest and Cypress

@Shinigami92 Shinigami92 added the c: docs Improvements or additions to documentation label Sep 2, 2022
@Shinigami92 Shinigami92 added this to the v7 - Current Major milestone Sep 2, 2022
@ST-DDT ST-DDT added p: 1-normal Nothing urgent s: accepted Accepted feature / Confirmed bug labels Sep 2, 2022
@ST-DDT
Copy link
Member

ST-DDT commented Sep 2, 2022

Maybe in a subsection of the Usage-Page.

@Shinigami92
Copy link
Member Author

I would vote against creating a 100km scrollable usage page
It's a much better UX (especially on mobile screen) to have separate categories
I'm somewhat used to other VitePress docs like vuejs.org and vueuse.org
I usually search something quickly when sitting in the bus or other places where to use a mobile phone
On mobile you then also benefit from section switches at the bottom of the page, and you have a HARD time when scrolling to huge pages just to find the correct place, because you don't have the right context menu (yet) on mobile

@Shinigami92
Copy link
Member Author

One of the best UX I had while searching and learning through docs on a mobile screen while being in a bug was: https://smallrye.io/smallrye-mutiny
I LOVE these arrows:

@ST-DDT
Copy link
Member

ST-DDT commented Sep 2, 2022

I rarely try to debug an error/develop software while on the phone.
That's what large/multiple screens are for.
If I have a small screen then the search function is also very useful, if there is content that you can search in.
Especially the usage section, I look for code that looks like it might match and start reading. If I have to scroll both vertically and "horizontally" then I might miss the important piece because the headline was misleading.

@ST-DDT
Copy link
Member

ST-DDT commented Sep 5, 2022

This should also include some "strange" caveeats of some of the test frameworks:
https://discord.com/channels/929487054990110771/929544565348777984/1016301559551893534

E.g. jest immutably caches the faker instance, thus two parralel test files will generate the exact same values, sometimes.
Calling faker.seed() early in the tests seems to fix the issue.

@codan84
Copy link

codan84 commented Sep 5, 2022

Just had a nice convo on discord regarding an issue of using faker with Jest.

The issue was faker returning duplicate values when calling faker.random.alphaNumeric:

  1. When called multiple times in the same test file, all values would be unique
  2. When called in multiple test files, values would be duplicated between test files in about 30% of runs

Simple way to replicate the issue - 2 test files and a module shared between them:

// helpers/random.ts
import { faker } from '@faker-js/faker'

export const getRandomSalesForceId = () => {
  return faker.random.alphaNumeric(18)
}
// ======

// test1.spec.ts
import { getRandomSalesForceId } from './helpers/random'

describe('some test 1', () => {
  test('some test 1', () => {
    const sfId = getRandomSalesForceId()
    console.log(`>>> some test 1 >>> ${sfId}`)

    expect(sfId).not.toBeNull()
  })
})
// ======

// test2.spec.ts
import { getRandomSalesForceId } from './helpers/random'

describe('some test 2', () => {
  test('some test 2', () => {
    const sfId = getRandomSalesForceId()
    console.log(`>>> some test 1 >>> ${sfId}`)

    expect(sfId).not.toBeNull()
  })
})
// ======

My suspicion is that this is due to Jest spinning up a pool of worker child processes and running each spec file in a separate worker. This would imply fresh instance of every module used by tests (including faker). If these were initialized close together we could run into an issue with the same seed used?

2 possible solutions found:

  1. run jest with --runInBand option (https://jestjs.io/docs/cli#--runinband)
  2. seed faker early in tests, for example:
// helpers/random.ts:
import { faker } from '@faker-js/faker'

faker.seed()

export const getRandomSalesForceId = () => {
  return faker.random.alphaNumeric(18)
}

// or directly in test:
import { faker } from '@faker-js/faker'

describe('some test', () => {
  beforeAll(() => {
    faker.seed()
  })
  
  test('some test', () => {
    // use faker
  })
})

Maybe worth adding to the abovementioned docs :)

@Shinigami92 Shinigami92 removed their assignment Oct 5, 2022
@ST-DDT ST-DDT added the good first issue Good for newcomers label Oct 6, 2022
@brenno263
Copy link
Contributor

I'd like to be assigned this issue please.

@brenno263
Copy link
Contributor

brenno263 commented Nov 2, 2022

The issue was faker returning duplicate values when calling faker.random.alphaNumeric:

  • When called multiple times in the same test file, all values would be unique
  • When called in multiple test files, values would be duplicated between test files in about 30% of runs

Simple way to replicate the issue - 2 test files and a module shared between

I haven't been able to recreate this behavior. Running the provided example 30 times, I did not observe any matching values. Are you still able to recreate?

@ST-DDT
Copy link
Member

ST-DDT commented Nov 2, 2022

The issue described #1324 (comment) should be fixed via #1334 (v7.6.0).

The original issue "Add a documentation page for how to use faker with testing frameworks" still needs some documentation.

@brenno263
Copy link
Contributor

brenno263 commented Dec 1, 2022

#1623

It's not clear to me how to link the PR to this issue.

@ST-DDT
Copy link
Member

ST-DDT commented Dec 1, 2022

#1623

It's not clear to me how to link the PR to this issue.

By adding Fixes #xyz to the description of the PR. Alternatively, we reference it for you.

brenno263 added a commit to brenno263/faker that referenced this issue Dec 5, 2022
brenno263 added a commit to brenno263/faker that referenced this issue Dec 6, 2022
brenno263 added a commit to brenno263/faker that referenced this issue Dec 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: docs Improvements or additions to documentation good first issue Good for newcomers p: 1-normal Nothing urgent s: accepted Accepted feature / Confirmed bug
Projects
No open projects
Status: Done
Development

Successfully merging a pull request may close this issue.

4 participants