Skip to content

Instantly share code, notes, and snippets.

@ruddha2001
Last active July 6, 2020 13:27
Show Gist options
  • Save ruddha2001/33d14c177cdeefcfaee4ffa043ead850 to your computer and use it in GitHub Desktop.
Save ruddha2001/33d14c177cdeefcfaee4ffa043ead850 to your computer and use it in GitHub Desktop.
import { checkNumber } from "../checkNumber";
import * as chai from "chai";
const expect = chai.expect;
/**
* Takes in a function and checks for error
* @param {Function} method - The function to check
* @param {any[]} params - The array of function parameters
* @param {string} message - Optional message to match with error message
*/
const expectThrowsAsync = async (
method: Function,
params: any[],
message?: string
) => {
let err = null;
try {
await method(...params);
} catch (error) {
err = error;
}
if (message) {
expect(err.message).to.be.equal(message);
} else {
expect(err).to.be.an("Error");
}
};
describe("Simple Number Test", function () {
// Will Pass
it("Check Positive Number", async function () {
await checkNumber(10);
});
// Will Pass
it("Check Negative Number without message", async function () {
await expectThrowsAsync(checkNumber, [-50]);
});
// Will Pass
it("Check Negative Number with the right message", async function () {
await expectThrowsAsync(
checkNumber,
[-45],
"Negative numbers are not accepted"
);
});
// Will Fail
it("Check Negative Number with the wrong message", async function () {
await expectThrowsAsync(
checkNumber,
[-45],
"Some other error message which will cause the test to fail"
);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment