Skip to content

Instantly share code, notes, and snippets.

@wzr1337
Last active May 5, 2018 21:22
Show Gist options
  • Save wzr1337/b8f7a2130fb4b8aeeb267450383f432e to your computer and use it in GitHub Desktop.
Save wzr1337/b8f7a2130fb4b8aeeb267450383f432e to your computer and use it in GitHub Desktop.
How to expect optionally in jasmine. #Typescript solve: https://github.com/jasmine/jasmine/issues/1539
/**
* Create an optional expectation for a spec.
* @param actual Actual computed value to test expectations against.
*/
export function expectOptionally<T>(actual: any): jasmine.Matchers<T> {
if (!isNullOrUndefined(actual)) {
return expect(actual);
}
const voidExpect = new Proxy(expect(actual),
{
get: (target: any, prop) => {
if (Object.keys(jasmine["matchers"]).indexOf(prop as string) !== -1) {
return (actual: any, expected: any): jasmine.CustomMatcherResult => {
let passed = true;
target.addExpectationResult(passed, {
matcherName: prop as String,
passed,
expected,
actual,
message: "Nothing to tell...",
error: null
});
return;
};
}
else
return target[prop];
}
});
return voidExpect as jasmine.Matchers<T>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment