Skip to content

Instantly share code, notes, and snippets.

@drazisil
Created August 27, 2024 01:27
Show Gist options
  • Save drazisil/3061f04e572fb8ee63ed279de33f3a9c to your computer and use it in GitHub Desktop.
Save drazisil/3061f04e572fb8ee63ed279de33f3a9c to your computer and use it in GitHub Desktop.
function throwme() {
const err = new Error("foo");
err.cause = "bar";
throw err;
};
const expect = (fn) => {
const self = {};
self.fn = fn;
let thrown = false
try {
self.res = self.fn.call();
} catch (e) {
self.res = e;
self.thrown = true;
}
return {
toThrowWithCause: (c) => {
if (self.thrown !== true) {
throw new Error(`Expected ${self.fn} to throw, but it did not!`)
}
console.dir(Object.getOwnPropertyDescriptors(c));
console.dir(Object.getOwnPropertyDescriptors(self.res));
console.log('===========')
if (self.res instanceof Error && String(self.res.cause) === String(c)) {
return true;
}
throw new Error(`Expected ${String(c)}, recieved ${String(self.res.cause)}`)
}
}
};
const expected = 'foo'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment