Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Cooperbuilt
Last active December 29, 2020 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cooperbuilt/574ba683efc2382c34e9d44c44cccbbe to your computer and use it in GitHub Desktop.
Save Cooperbuilt/574ba683efc2382c34e9d44c44cccbbe to your computer and use it in GitHub Desktop.
A simple JS testing framework
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