Appearance
框架
¥Frameworks
Faker 可以轻松地与各种测试框架一起使用。以下是一些流行框架的示例。
¥Faker can easily be used with a variety of testing frameworks. Here are a few examples with popular frameworks.
请注意,这些示例仅使用 en
语言环境以获得更好的性能。有关更多信息,请访问 localization。
¥Note that these examples use only the en
locale for better performance. For more information visit localization.
Vitest 和 Jest
¥Vitest and Jest
由于 Vitest 和 Jest 使用极其相似的符号,因此本节将同时介绍两者。主要区别在于需要在 Vitest 中导入测试方法。只需裁剪该行即可进行 Jest 集成。
¥Since Vitest and Jest use an extremely similar notation, this section will cover both at once. The main difference is that testing methods need to be imported in Vitest. Simply crop that line out for a Jest integration.
这些框架的工作方式与你对 Faker 的期望完全一致。这是一个最小的例子:
¥These frameworks work about exactly as you would expect with Faker. Here's a minimal example:
ts
import { faker } from '@faker-js/faker/locale/en';
import { describe, expect, it } from 'vitest';
describe('reverse array', () => {
it('should reverse the array', () => {
const title = faker.person.jobTitle();
const name = faker.person.fullName();
const animal = faker.animal.bear();
const array = [title, name, animal];
expect(array.reverse()).toStrictEqual([animal, name, title]);
});
});
有时进行种子测试会很有用,我们用静态值为我们的 faker 实例播种,以便它每次都会生成相同的随机值。这些在旨在确定性的测试中特别有用,例如快照测试。
¥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. These are especially useful in tests that are meant to be deterministic, such as snapshot tests.
ts
import { faker } from '@faker-js/faker/locale/en';
import { afterEach, describe, expect, it } from 'vitest';
// We might want other tests to *not* be seeded. This will re-seed our faker instance after each test.
afterEach(() => {
faker.seed();
});
describe('reverse array', () => {
it('should reverse the array', () => {
// Seed our faker instance with some static number.
faker.seed(1234);
const title = faker.person.jobTitle();
const name = faker.person.fullName();
const animal = faker.animal.bear();
const array = [title, name, animal];
expect(array.reverse()).toStrictEqual([animal, name, title]);
// Expect our value to always match a generated snapshot.
expect(array.reverse()).toMatchSnapshot();
});
});
Cypress
Cypress 集成也相当简单:
¥Cypress integration is fairly straightforward as well:
ts
import { faker } from '@faker-js/faker/locale/en';
describe('Testing the application', () => {
it('should create an account with username and password', () => {
let username = faker.internet.username(); // before version 9.1.0, use userName()
let password = faker.internet.password();
let email = faker.internet.exampleEmail();
// Visit the a webpage and create an account.
cy.visit('https://www.example.com/register');
cy.get('#email-input').type(email);
cy.get('#username-input').type(username);
cy.get('#password-input').type(password);
cy.get('#password-confirm-input').type(password);
cy.get('#register-submit-input').click();
// Now, we try to login with these credentials.
cy.visit('https://www.example.com/login');
cy.get('#email-input').type(email);
cy.get('#password-input').type(password);
cy.get('#login-submit-input').click();
// We should have logged in successfully to the dashboard page.
cy.url().should('include', '/dashboard');
});
});
Playwright
与 Playwright 的集成也很容易:
¥Integration with Playwright is also easy:
ts
import { faker } from '@faker-js/faker/locale/en';
import { expect, test } from '@playwright/test';
test.describe('Testing the application', () => {
test('should create an account with username and password', async ({
page,
}) => {
const username = faker.internet.username(); // before version 9.1.0, use userName()
const password = faker.internet.password();
const email = faker.internet.exampleEmail();
// Visit the webpage and create an account.
await page.goto('https://www.example.com/register');
await page.getByLabel('email').fill(email);
await page.getByLabel('username').fill(username);
await page.getByLabel('password', { exact: true }).fill(password);
await page.getByLabel('confirm password').fill(password);
await page.getByRole('button', { name: 'Register' }).click();
// Now, we try to login with these credentials.
await page.goto('https://www.example.com/login');
await page.getByLabel('email').fill(email);
await page.getByLabel('password').fill(password);
await page.getByRole('button', { name: 'Login' }).click();
// We should have logged in successfully to the dashboard page.
await expect(page).toHaveURL(/.*dashboard/);
});
});