Jest · fixture · BACEN
Gerador de Chave PIX em Jest
Fixture pra chave PIX válida em testes Jest — cobre os 4 tipos BACEN (CPF, email, telefone, aleatória). Session fixture, globalSetup, mock de banco central. SDK fakeforge-br.
TL;DR
npm install --save-dev fakeforge-br
// tests/fixtures/pix.ts
import { FakeForge } from "fakeforge-br"
export const ff = new FakeForge()Fixture — chaves por tipo
// tests/fixtures/pix-fixture.ts
import { FakeForge } from "fakeforge-br"
const ff = new FakeForge()
interface PixKey { type: "cpf" | "email" | "phone" | "aleatoria"; value: string }
let cache: PixKey[] | null = null
export async function getPixKeys(qty = 20): Promise<PixKey[]> {
if (!cache || cache.length < qty) {
// Usa preset fintech que traz 3-4 chaves por cliente correlacionadas
const clientes = await ff.preset("fintech", Math.ceil(qty / 3))
cache = clientes.flatMap(c => c.pix_keys)
}
return cache.slice(0, qty)
}
export async function getPixKeyByType(type: PixKey["type"]): Promise<PixKey> {
const keys = await getPixKeys(50)
const found = keys.find(k => k.type === type)
if (!found) throw new Error(`Nenhuma chave tipo ${type}`)
return found
}Teste — validador de chave PIX
// tests/pix-validator.test.ts
import { getPixKeys, getPixKeyByType } from "./fixtures/pix-fixture"
import { PixValidator } from "../src/pix.validator"
describe("PixValidator", () => {
const validator = new PixValidator()
test.each(["cpf", "email", "phone", "aleatoria"] as const)(
"aceita chave tipo %s",
async (type) => {
const chave = await getPixKeyByType(type)
expect(validator.isValid(chave.type, chave.value)).toBe(true)
}
)
test("rejeita CPF inválido", () => {
expect(validator.isValid("cpf", "111.111.111-11")).toBe(false)
})
test("rejeita email malformado", () => {
expect(validator.isValid("email", "not-an-email")).toBe(false)
})
test("rejeita phone sem +55", () => {
expect(validator.isValid("phone", "11987654321")).toBe(false)
})
})Teste E2E — transferência PIX
// tests/pix-transfer.test.ts
import request from "supertest"
import app from "../src/app"
import { getPixKeys } from "./fixtures/pix-fixture"
describe("POST /pix/transferir", () => {
test("transfere entre 2 chaves", async () => {
const [chaveOrigem, chaveDestino] = await getPixKeys(2)
const res = await request(app)
.post("/pix/transferir")
.send({
chaveOrigem: chaveOrigem.value,
chaveDestino: chaveDestino.value,
valorCentavos: 10000,
descricao: "Test transfer",
})
expect(res.status).toBe(200)
expect(res.body.status).toBe("concluido")
expect(res.body.id_end2end).toMatch(/^E\d{8}\d{20}$/) // formato BACEN
})
})Mock de banco central
// tests/pix-service.test.ts
import { getPixKeys } from "./fixtures/pix-fixture"
jest.mock("../src/bacen.client", () => ({
BacenClient: jest.fn().mockImplementation(() => ({
consultarChave: jest.fn().mockResolvedValue({
nome: "MARINA SOUZA OLIVEIRA",
cpf: "***.789.***",
banco: "260",
}),
})),
}))
test("PixService consulta chave no BACEN antes de transferir", async () => {
const { PixService } = await import("../src/pix.service")
const [chave] = await getPixKeys(1)
const service = new PixService()
const info = await service.consultarChave(chave.value)
expect(info.nome).toBe("MARINA SOUZA OLIVEIRA")
})