-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
coding_interviews/frontend/js-coding-questions/testing-framework/testing-framework.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
function describe(testSuiteName, func) { | ||
console.log(`beginning test suite ${testSuiteName}`); | ||
try { | ||
func(); | ||
console.log(`successfully completed test suite ${testSuiteName}`); | ||
} catch ({ testCaseName, errorMessage }) { | ||
console.error( | ||
`failed running test suite ${testSuiteName} on test case ${testCaseName} with error message ${errorMessage}` | ||
); | ||
} | ||
} | ||
|
||
function it(testCaseName, func) { | ||
console.log(`beginning test case ${testCaseName}`); | ||
try { | ||
func(); | ||
console.log(`successfully completed test case ${testCaseName}`); | ||
} catch (errorMessage) { | ||
throw { testCaseName, errorMessage }; | ||
} | ||
} | ||
|
||
function expect(actual) { | ||
return { | ||
toExist: () => { | ||
if (actual === null || actual === undefined) { | ||
throw `expected value to exist but got ${actual}`; | ||
} | ||
}, | ||
toBe: (expected) => { | ||
if (actual !== expected) { | ||
throw `expected ${JSON.stringify(actual)} to be ${JSON.stringify( | ||
expected | ||
)}`; | ||
} | ||
}, | ||
toBeType: (type) => { | ||
if (typeof actual !== type) { | ||
throw `expected ${JSON.stringify( | ||
actual | ||
)} to be of type ${type} but got ${typeof actual}`; | ||
} | ||
}, | ||
}; | ||
} |