Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

274
Vistas
How do I parameterize test cases in TypeScript unit tests?

I have a simple passing unit test:

describe("Example", () => {
    it("Return digits or empty string", () => {
        let actual = SmokeTest.ProblemOne("1");
        assert.equal(actual, "1");
    })
})

I now wish to test many other cases (empty string, more complex inputs and edge-cases). I have read about how to implement fixtures in TypeScript but this seems like overkill for an array of test-input, expected-output values.

What is the idiomatic way to define such an array in TypeScript / Mocha?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can just make a function that you can reuse.

function assertProblemOne(input: string, expected: string): void {
    let actual = SmokeTest.ProblemOne(input);
    assert.equal(expected, actual);
}

describe("Example", () => {
    it("Return digits or empty string", () => {
        assertProblemOne("input here", "expected result here")
    })
})

However, I'm not sure that's really much better than simply.

assert.equal(SmokeTest.ProblemOne("1"), "1");

The only thing worse than debugging your code is debugging your tests. So unless there is a large amount of code that is shared between tests, I would recommend keep the code that runs in each tests as structurally simple as possible.

about 4 years ago · Juan Pablo Isaza Denunciar

0

From the Mocha documentation:

No special syntax is required... to parameterize tests [you can] generate them with a closure.

describe("Example", () => {

    // define a closure that calls the method we are testing
    const testProblemOne = ({arg, expected}) =>
        function () {
            const actual = SmokeTest.ProblemOne(arg);
            assert.equal(actual, expected);
        }

    // invoke the closure for each test case
    it("Empty returns empty", testProblemOne({arg: "", expected: ""}));
    it("Single digit identity", testProblemOne({arg: "1", expected: "1"}));
    // additional test cases here...
})

This technique avoids using foreach in favor of readability and broader IDE integration.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda