Last active
December 29, 2020 13:40
-
-
Save Cooperbuilt/574ba683efc2382c34e9d44c44cccbbe to your computer and use it in GitHub Desktop.
A simple JS testing framework
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
function expect(initialValue) { | |
return { | |
toStrictlyEqual(expected) { | |
return Object.is(initialValue, expected) | |
}, | |
toLooselyEqual(expected) { | |
return initialValue == expected || JSON.stringify(initialValue) === JSON.stringify(expected); | |
} | |
} | |
} | |
function test(title, callback) { | |
try { | |
const result = callback(); | |
if (result) { | |
return console.log(`PASS!: ${title}`); | |
} | |
return console.log(`FAIL: ${title}`); | |
} catch (error) { | |
console.log(`FAIL: ${title}`); | |
console.log(`ERROR: Error Message: ${error}`); | |
} | |
} | |
/** | |
------------- | |
EXAMPLE USAGE | |
------------- | |
test('adds a number (should pass)', () => { | |
return expect('1').toLooselyEqual(1) | |
}) | |
test('adds a number (should fail)', () => { | |
return expect('1').toStrictlyEqual(1) | |
}) | |
test('compares arrays (should pass)', () => { | |
return expect(['first', 'second']).toLooselyEqual(['first', 'second']) | |
}) | |
**/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment