Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

272
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!