JS Interview Questions
JS Interview Questions
try {
func();
console.log(`successfully completed test suite ${testSuiteName}`);
} catch (error) {
const { testCaseName, errorMessage } = error;
console.error(
`failed running test suite ${testSuiteName} on ` +
`test case ${testCaseName} with error message ${errorMessage}`
);
}
}
try {
func();
console.log(`successfully completed test case ${testCaseName}`);
} catch (errorMessage) {
throw { testCaseName, errorMessage };
}
}
function expect(actual) {
return new ExpectFunctions(actual);
}
class ExpectFunctions {
constructor(actual) {
this.actual = actual;
this.stringifiedActual = JSON.stringify(actual);
}
toExist() {
if (this.actual == null) {
throw `expected value to exist but got ${this.stringifiedActual}`;
}
}
toBe(expected) {
if (this.actual !== expected) {
throw `expected ${this.stringifiedActual} to be ${JSON.stringify(
expected
)}`;
}
}
toBeType(type) {
if (typeof this.actual !== type) {
throw `expected ${
this.stringifiedActual
} to be of type ${type} but got ${typeof this.actual}`;
}
}
}