Sometimes we need to mock an ObjectId for testing.

new ObjectId("randomstring") would give an error Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters.

So lets create appropriate mock:

const genericMongoObjectId = "a00000000000000000000000";
export const mockMongoObjectId = (suffix: string = "") => {
    if (suffix.length >= genericMongoObjectId.length) {
        return suffix.substring(0, genericMongoObjectId.length);
    return `${genericMongoObjectId.substring(0, genericMongoObjectId.length - suffix.length)}${suffix}`;

Happy coding!