{"id":16222148,"url":"https://github.com/hugodf/mock-mongo-object-id","last_synced_at":"2026-02-11T00:01:54.970Z","repository":{"id":48271702,"uuid":"172398753","full_name":"HugoDF/mock-mongo-object-id","owner":"HugoDF","description":"Examples of mocking Mongo ObjectId (test framework agnostic)","archived":false,"fork":false,"pushed_at":"2021-08-03T19:46:51.000Z","size":517,"stargazers_count":0,"open_issues_count":11,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-20T19:50:54.435Z","etag":null,"topics":["ava","javascript","javascript-testing","mongo-nodejs","mongodb","nodejs"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/HugoDF.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-02-24T22:36:26.000Z","updated_at":"2019-02-24T23:10:31.000Z","dependencies_parsed_at":"2022-08-23T15:21:25.361Z","dependency_job_id":null,"html_url":"https://github.com/HugoDF/mock-mongo-object-id","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/HugoDF/mock-mongo-object-id","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HugoDF%2Fmock-mongo-object-id","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HugoDF%2Fmock-mongo-object-id/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HugoDF%2Fmock-mongo-object-id/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HugoDF%2Fmock-mongo-object-id/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/HugoDF","download_url":"https://codeload.github.com/HugoDF/mock-mongo-object-id/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HugoDF%2Fmock-mongo-object-id/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29322733,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-10T20:44:44.282Z","status":"ssl_error","status_checked_at":"2026-02-10T20:44:43.393Z","response_time":65,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ava","javascript","javascript-testing","mongo-nodejs","mongodb","nodejs"],"created_at":"2024-10-10T12:11:27.284Z","updated_at":"2026-02-11T00:01:54.940Z","avatar_url":"https://github.com/HugoDF.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mock Mongo ObjectId\n\nShowcase of 3 approaches to mocking Mongo ObjectId\n\nHere's the report according to 3 tests:\n\n![Report of passing/failing tests per approach](img/report.png)\n\n## Identity mocking\n\n`const identityObjectId = data =\u003e data;` (see [./src/identity-object-id.test.js](./src/identity-object-id.test.js))\n\nConclusions:\n- super simple\n- passes `toString` test, returns right values since `'abc'.toString() === 'abc'`\n- fails the `ObjectId('a')` is an `object` test (it's a `string`)\n- passes `ObjectId('a') === ObjectId('a')` test, `'a' === 'a'`\n\n## Naive mocking\n\nSee [./src/naive-object-id.test.js](./src/naive-object-id.test.js)\n\n```js\nconst naiveObjectId = data =\u003e {\n  return {\n    name: data,\n    toString: () =\u003e data\n  };\n};\n```\n\nConclusions:\n- More involved\n- passes `toString` test, returns right values since `.toString()` returns whatever was passed into constructor\n- fails the `ObjectId('a') === ObjectId('a')` test, since toString is a new function\n- passes the `ObjectId('a')` is an `object` test\n\n## Working mock\n\nSee [./src/object-id-mock.test.js](./src/object-id-mock.test.js)\n\n```js\nconst mockObjectId = data =\u003e {\n  const oid = {\n    name: data\n  };\n  Object.defineProperty(oid, \"toString\", {\n    value: () =\u003e data\n  });\n  return oid;\n};\n```\n\nConclusions:\n- Object.defineProperty() hackery...\n- passes `toString` test, returns right values since `.toString()` returns whatever was passed into constructor\n- passes the `ObjectId('a') === ObjectId('a')` test, since toString is not an enumerable property\n- passes the `ObjectId('a')` is an `object` test\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhugodf%2Fmock-mongo-object-id","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhugodf%2Fmock-mongo-object-id","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhugodf%2Fmock-mongo-object-id/lists"}