Jest · fixture · UF

Gerador de RG em Jest

Fixture pra RG válido em testes Jest — formato por UF via SDK fakeforge-br.

npm install --save-dev fakeforge-br

Fixture

// tests/fixtures/rg-fixture.ts
import { FakeForge } from "fakeforge-br"

const ff = new FakeForge()
let cache: string[] | null = null

export async function getRGs(qty = 20): Promise<string[]> {
  if (!cache || cache.length < qty) {
    cache = await ff.rg(Math.max(qty, 100))
  }
  return cache.slice(0, qty)
}

export async function getPessoaCompleta() {
  const [customer] = await ff.preset("customer", 1)
  const [rg] = await ff.rg(1)
  return { ...customer, rg, ufEmissor: customer.endereco.estado }
}

Teste E2E — cadastro com RG

// tests/rg-signup.test.ts
import request from "supertest"
import app from "../src/app"
import { getPessoaCompleta } from "./fixtures/rg-fixture"

describe("POST /identidades", () => {
  test("aceita cadastro com RG válido", async () => {
    const pessoa = await getPessoaCompleta()

    const res = await request(app).post("/identidades").send({
      cpf: pessoa.cpf,
      nome: pessoa.nome,
      rg: pessoa.rg,
      ufEmissor: pessoa.ufEmissor,
    })

    expect(res.status).toBe(201)
  })
})

globalSetup

// tests/global-setup.ts
import { FakeForge } from "fakeforge-br"
import fs from "fs"

export default async function () {
  const ff = new FakeForge()
  const [rgs, customers] = await Promise.all([
    ff.rg(500),
    ff.preset("customer", 500),
  ])
  fs.writeFileSync(".fixtures.json", JSON.stringify({ rgs, customers }))
}