Skip to content

Commit 6baa8ce

Browse files
authored
docs: how to use with testing frameworks (#1623)
1 parent d0ff3fe commit 6baa8ce

2 files changed

Lines changed: 104 additions & 0 deletions

File tree

docs/.vitepress/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ const config = defineConfig({
193193
text: 'Localization',
194194
link: '/guide/localization',
195195
},
196+
{
197+
text: 'Frameworks',
198+
link: '/guide/frameworks',
199+
},
196200
{
197201
text: 'Upgrading to v8',
198202
link: '/guide/upgrading',

docs/guide/frameworks.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Frameworks
2+
3+
Faker can easily be used with a variety of testing frameworks. Here are a few examples with popular frameworks.
4+
5+
Note that these examples use only the `en` locale for better performance. For more information visit [localization](./localization.md).
6+
7+
## Vitest and Jest
8+
9+
Since [Vitest](https://vitest.dev/) and [Jest](https://jestjs.io/) use an extremely similar notation, this section will cover both at once.
10+
The main difference is that testing methods need to be imported in Vitest.
11+
Simply crop that line out for a Jest integration.
12+
13+
These frameworks work about exactly as you would expect with Faker. Here's a minimal example:
14+
15+
```ts
16+
import { describe, it, expect } from 'vitest';
17+
import { faker } from '@faker-js/faker/locale/en';
18+
19+
describe('reverse array', () => {
20+
it('should reverse the array', () => {
21+
const title = faker.name.jobTitle();
22+
const name = faker.name.fullName();
23+
const animal = faker.animal.bear();
24+
25+
const array = [title, name, animal];
26+
27+
expect(array.reverse()).toStrictEqual([animal, name, title]);
28+
});
29+
});
30+
```
31+
32+
It can sometimes be useful to do seeded tests, where we seed our faker instance with a static value so that it will generate the same random value each time.
33+
These are especially useful in tests that are meant to be deterministic, such as snapshot tests.
34+
35+
- [Snapshots in Vitest](https://vitest.dev/guide/snapshot.html)
36+
- [Snapshots in Jest](https://jestjs.io/docs/snapshot-testing)
37+
38+
```ts
39+
import { describe, it, expect } from 'vitest';
40+
import { faker } from '@faker-js/faker/locale/en';
41+
42+
// We might want other tests to *not* be seeded. This will re-seed our faker instance after each test.
43+
afterEach(() => {
44+
faker.seed();
45+
});
46+
47+
describe('reverse array', () => {
48+
it('should reverse the array', () => {
49+
// Seed our faker instance with some static number.
50+
faker.seed(1234);
51+
const title = faker.name.jobTitle();
52+
const name = faker.name.fullName();
53+
const animal = faker.animal.bear();
54+
55+
const array = [title, name, animal];
56+
57+
expect(array.reverse()).toStrictEqual([animal, name, title]);
58+
59+
// Expect our value to always match a generated snapshot.
60+
expect(array.reverse()).toMatchSnapshot();
61+
});
62+
});
63+
```
64+
65+
## Cypress
66+
67+
[Cypress](https://www.cypress.io/) integration is fairly straighforward as well:
68+
69+
```ts
70+
import { faker } from '@faker-js/faker/locale/en';
71+
72+
describe('Testing the application', () => {
73+
it('should create an account with username and password', () => {
74+
let username = faker.internet.userName();
75+
let password = faker.internet.password();
76+
let email = faker.internet.exampleEmail();
77+
78+
// Visit the a webpage and create an account.
79+
cy.visit('https://www.example.com/register');
80+
81+
cy.get('#email-input').type(email);
82+
cy.get('#username-input').type(username);
83+
cy.get('#password-input').type(password);
84+
cy.get('#password-confirm-input').type(password);
85+
86+
cy.get('#register-submit-input').click();
87+
88+
// Now, we try to login with these credentials.
89+
cy.visit('https://www.example.com/login');
90+
91+
cy.get('#email-input').type(email);
92+
cy.get('#password-input').type(password);
93+
94+
cy.get('#login-submit-input').click();
95+
96+
// We should have logged in successfully to the dashboard page.
97+
cy.url().should('include', '/dashboard');
98+
});
99+
});
100+
```

0 commit comments

Comments
 (0)