Skip to content

Instantly share code, notes, and snippets.

@robertleeplummerjr
Created February 22, 2024 18:48
Show Gist options
  • Select an option

  • Save robertleeplummerjr/7bfde41f06681c123fce6430c944bbed to your computer and use it in GitHub Desktop.

Select an option

Save robertleeplummerjr/7bfde41f06681c123fce6430c944bbed to your computer and use it in GitHub Desktop.

Revisions

  1. robertleeplummerjr created this gist Feb 22, 2024.
    54 changes: 54 additions & 0 deletions jest-test-permutations.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    export interface ITestCaseSubject {
    subject: string;
    }
    export interface IEachTestCase extends ITestCaseSubject {
    each: Array<() => any>;
    }

    export type ArrayMoreThanOne<T> = [T, T, ...T[]];
    export function testPermutations(subjectCases: ArrayMoreThanOne<IEachTestCase>) {
    function traverseAll(
    fn: (subject: string, permutation: () => string, subjectIndex: number, permutationIndex: number) => void,
    ): void {
    for (let subjectIndex = 0; subjectIndex < subjectCases.length; subjectIndex++) {
    const subjectCase = subjectCases[subjectIndex];
    const permutations = subjectCase.each;
    for (let permutationIndex = 0; permutationIndex < permutations.length; permutationIndex++) {
    fn(subjectCase.subject, permutations[permutationIndex], subjectIndex, permutationIndex);
    }
    }
    }
    function traverseAllBut(
    skipSubjectIndex: number,
    skipPermutationIndex: number,
    fn: (subject: string, permutation: () => string, subjectIndex: number, permutationIndex: number) => void,
    ): void {
    for (let subjectIndex = 0; subjectIndex < subjectCases.length; subjectIndex++) {
    if (subjectIndex === skipSubjectIndex) continue;
    const subjectCase = subjectCases[subjectIndex];
    const permutations = subjectCase.each;
    for (let permutationIndex = 0; permutationIndex < permutations.length; permutationIndex++) {
    if (permutationIndex === skipPermutationIndex) continue;
    fn(subjectCase.subject, permutations[permutationIndex], subjectIndex, permutationIndex);
    }
    }
    }
    return (runTest: () => void | Promise<void>) => {
    let whenDescription: string[];
    beforeEach(() => {
    whenDescription = [];
    traverseAll((subject, permutation, subjectIndex, permutationIndex) => {
    traverseAllBut(subjectIndex, permutationIndex, (relatedSubject, relatedPermutation) => {
    whenDescription.push(`${relatedSubject} = ${JSON.stringify(relatedPermutation())}`);
    });
    });
    });
    // describe(`when ${whenDescription.join(", ")} and ${subject} = ${JSON.stringify(permutation())}`, () => {
    // beforeEach(permutation);
    // runTest();
    // // it("returns true", async () => {
    // // expect(await runTest()).toBe(true);
    // // });
    // });
    };
    }