Skip to content

Instantly share code, notes, and snippets.

@BadgerCode
Last active July 2, 2018 09:45
Show Gist options
  • Save BadgerCode/3d41966f03dcfb38975da5d63e72d225 to your computer and use it in GitHub Desktop.
Save BadgerCode/3d41966f03dcfb38975da5d63e72d225 to your computer and use it in GitHub Desktop.
Jasmine describe extensions for parameterised describes
export function testCase(...testParameters: any[]) {
// Syntactic sugar function
return testParameters;
}
export function testCases(...testCases: any[]){
// Syntactic sugar function
return testCases;
}
export function fdescribeWithParams(message: string, testCases: any[][], describeCallback: ((testParameters: any[]) => void)) {
testCases.forEach(testParameters => {
fdescribe(`${message} [${testParameters}]`, () => {
describeCallback(testParameters);
});
});
}
export function describeWithParams(message: string, testCases: any[][], describeCallback: ((testParameters: any[]) => void)) {
testCases.forEach(testParameters => {
describe(`${message} [${testParameters}]`, () => {
describeCallback(testParameters);
});
});
}
@BadgerCode
Copy link
Author

BadgerCode commented Jun 28, 2018

Usage:

describeWithParams('Given an image',
    testCases(
        testCase('http://i.imgur.com/b88DHtD.png', [310, 232]),
        testCase('http://i.imgur.com/3LMb2L6.jpg', [500, 329]),
        testCase('http://i.imgur.com/RYjVT1a.gif', [360, 240])
    ),
    (testParameters) => {
        let imageUrl: string = testParameters[0];
        let expectedDimensions: number[] = testParameters[1];

        beforeEach(() => {
            // ...
        });

        it('does something', () => {
            // ...
        });

        it('does something else', () => {
            // ...
        });

        describe('has a nested describe', () => {
            // **
        });

        fdescribeWithParams(...) // Focus spec- https://jasmine.github.io/2.1/focused_specs.html
    }
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment