Last active
May 5, 2018 21:22
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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