Node.js · TypeScript · SDK
Gerador de Chave PIX em Node.js
SDK oficial fakeforge-br gera os 4 tipos de chave PIX BACEN. TypeScript types nativos. Snippets pra Jest, Vitest, NestJS, Express, Playwright E2E.
TL;DR
npm install fakeforge-br
import { FakeForge } from "fakeforge-br"
const chaves = await new FakeForge().pixKey(100)Os 4 tipos de chave PIX BACEN
import { FakeForge } from "fakeforge-br"
const ff = new FakeForge()
// Aleatório entre 4 tipos
const chaves = await ff.pixKey(20)
// Preset fintech tem cliente + 3-4 chaves por cliente correlacionadas
const [cliente] = await ff.preset("fintech", 1)
cliente.pix_keys.forEach(chave => {
console.log(`${chave.type}: ${chave.value}`)
})
// Output típico:
// cpf: 12345678909 (só dígitos)
// email: marina.souza@gmail.com
// phone: +5511987654321
// aleatoria: 8f4e2c91-a3b7-4d5e-9f2a-1c8b6d0e5a3fUso em Jest — teste de transferência PIX
// tests/pix-transfer.test.ts
import { FakeForge } from "fakeforge-br"
import { PixService } from "../src/pix.service"
const ff = new FakeForge()
describe("PixService.transferir", () => {
let clientes: Array<{ pix_keys: Array<{ type: string; value: string }> }>
beforeAll(async () => {
clientes = await ff.preset("fintech", 10)
})
test("aceita transferência entre 2 CPFs", async () => {
const origem = clientes[0].pix_keys.find(c => c.type === "cpf")!
const destino = clientes[1].pix_keys.find(c => c.type === "cpf")!
const service = new PixService()
const result = await service.transferir({
chaveOrigem: origem.value,
chaveDestino: destino.value,
valor: 100.00,
})
expect(result.status).toBe("concluido")
})
test.each(["cpf", "email", "phone", "aleatoria"])(
"valida chave tipo %s",
(tipo) => {
const chave = clientes[0].pix_keys.find(c => c.type === tipo)!
const service = new PixService()
expect(service.validarChave(chave.type, chave.value)).toBe(true)
}
)
})Uso em NestJS (seed command)
// pix.seed.command.ts
import { Command, CommandRunner } from "nest-commander"
import { FakeForge } from "fakeforge-br"
import { PixKeyService } from "./pix-key.service"
@Command({ name: "seed:pix-keys" })
export class PixKeySeedCommand extends CommandRunner {
constructor(private pix: PixKeyService) { super() }
async run(): Promise<void> {
const ff = new FakeForge()
const clientes = await ff.preset("fintech", 200)
const chaves = clientes.flatMap(c =>
c.pix_keys.map(k => ({
cpf: c.customer.cpf,
tipo: k.type,
valor: k.value,
}))
)
await this.pix.bulkCreate(chaves)
console.log(`${chaves.length} chaves PIX seeded`)
}
}Playwright E2E — teste de QR Code PIX
// e2e/pix-qr.spec.ts
import { test, expect } from "@playwright/test"
import { FakeForge } from "fakeforge-br"
const ff = new FakeForge()
test("gera QR Code PIX no checkout", async ({ page }) => {
const [cliente] = await ff.preset("fintech", 1)
const chavePix = cliente.pix_keys.find(k => k.type === "cpf")!
await page.goto("https://staging.myapp.com/checkout")
await page.selectOption("[data-test=payment-method]", "pix")
await page.fill("[data-test=pix-key]", chavePix.value)
await page.click("[data-test=generate-qr]")
await expect(page.locator("[data-test=qr-code-image]")).toBeVisible()
await expect(page.locator("[data-test=qr-code-copy-paste]")).toContainText("00020126")
})