Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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