Examples of mocking Mongo ObjectId (test framework agnostic)
MIT License
Showcase of 3 approaches to mocking Mongo ObjectId
Here's the report according to 3 tests:
const identityObjectId = data => data;
(see ./src/identity-object-id.test.js)
Conclusions:
toString
test, returns right values since 'abc'.toString() === 'abc'
ObjectId('a')
is an object
test (it's a string
)ObjectId('a') === ObjectId('a')
test, 'a' === 'a'
See ./src/naive-object-id.test.js
const naiveObjectId = data => {
return {
name: data,
toString: () => data
};
};
Conclusions:
toString
test, returns right values since .toString()
returns whatever was passed into constructorObjectId('a') === ObjectId('a')
test, since toString is a new functionObjectId('a')
is an object
testSee ./src/object-id-mock.test.js
const mockObjectId = data => {
const oid = {
name: data
};
Object.defineProperty(oid, "toString", {
value: () => data
});
return oid;
};
Conclusions:
toString
test, returns right values since .toString()
returns whatever was passed into constructorObjectId('a') === ObjectId('a')
test, since toString is not an enumerable propertyObjectId('a')
is an object
test