{"id":20821358,"url":"https://github.com/hexancore/mocker","last_synced_at":"2026-01-23T18:34:22.908Z","repository":{"id":59001340,"uuid":"534945862","full_name":"hexancore/mocker","owner":"hexancore","description":null,"archived":false,"fork":false,"pushed_at":"2024-11-04T19:14:39.000Z","size":1054,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-09-21T12:44:19.409Z","etag":null,"topics":["jest","mock","testing","vite"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/hexancore.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":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-09-10T09:06:08.000Z","updated_at":"2024-11-04T19:11:32.000Z","dependencies_parsed_at":"2025-05-07T16:29:00.123Z","dependency_job_id":"6749682c-c4fa-4953-a0a7-a30a64eaf6f1","html_url":"https://github.com/hexancore/mocker","commit_stats":{"total_commits":4,"total_committers":2,"mean_commits":2.0,"dds":0.25,"last_synced_commit":"72862841d7125db6a62014789c7d64f33599efe0"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/hexancore/mocker","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hexancore%2Fmocker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hexancore%2Fmocker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hexancore%2Fmocker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hexancore%2Fmocker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hexancore","download_url":"https://codeload.github.com/hexancore/mocker/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hexancore%2Fmocker/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28697429,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-23T17:25:48.045Z","status":"ssl_error","status_checked_at":"2026-01-23T17:25:47.153Z","response_time":59,"last_error":"SSL_read: 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":["jest","mock","testing","vite"],"created_at":"2024-11-17T22:12:01.409Z","updated_at":"2026-01-23T18:34:22.891Z","avatar_url":"https://github.com/hexancore.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mocker\nTwo words can describe it: **simple and magical :)**\nHelper stuff to create **mocks** in **TypeScript** in Jest or Vitest\n\n**You just need it!**\n\n## Usage:\n\n**In Jest**\n```ts\nimport { M, mock } from '@hexancore/mocker';\n```\n\n**In Vitest**\n```ts\nimport { M, mock } from '@hexancore/mocker/vitest';\n```\n\n**Example**\n```ts\nimport { M, mock } from '@hexancore/mocker';\n\ninterface TestMock {\n  a(param1: string, param2: boolean): boolean;\n  b(param1: string, param2: boolean): boolean;\n}\n\ndescribe('Example test suite', () =\u003e {\n  let m: M\u003cTestMock\u003e; // Define mock variable with your class or interface wrapped with `M` type\n\n  beforeEach(() =\u003e {\n    // in beforeEach create `Mocker` with mock() factory method\n    // you can simply mock interface or class and give it descriptive name(used in errors)\n    m = mock('my_mock');\n  });\n\n  afterEach(() =\u003e {\n    // after execute your code, you can check sets expectations results with it(for many tests call it in jest \"afterEach\")\n    mock.checkExpections();\n  });\n\n  test('Example test', () =\u003e {\n    // define method call expectation(available methods list will be shows in VS + it will hint all parameters and return type )\n    m.expects('a', 'test', true).andReturn(true);\n\n    // mock \"i\" property is object of mocked class, pass it where you need\n    m.i.a('test', true);\n  });\n```\n\n### Defining expectation\n\n`Mocker.expects(method, ...args)` returns `MethodMock` object with you can define return value by:\n*  `andReturnWith((implementation: (...args: any) =\u003e any)` - you can define your own method implementation\n*  `andReturn(value: any)` - define returns passed value once\n*  `andReturnThis()` - define returns this\n*  `andReturnResolved` - simple sugar function for andReturnWith((() =\u003e Promise.resolve(value))\n*  `andReturnRejected` - simple sugar function for andReturnWith((() =\u003e Promise.reject(error))\n\nUsing `Jest.expect` matchers in `Mocker.expects`\n```ts\nlet userRepository = mock\u003cUserRepository\u003e();\nuserRepository\n      .expects('save', expect.objectContaining({ email, username, password: hashedPassword }))\n      .andReturn(OKA(true));\n```\n\n### Reset\n\nMock expectations can be reset with `Mocker.reset()` or with passed `true` to `Mocker.checkExpections(true)`.\n\n### Mockers\nManages a group of defined mocks in a more efficient way of writing test code and simplifing injecting many mocks to class object.\n\n**For use add to your `tsconfig.json`**\n```json\n \"compilerOptions\": {\n    \"useDefineForClassFields\": true,\n }\n```\n**Example**\n```ts\nimport { mocks } from '@hexancore/mocker';\n\nclass TestMock {\n  public method(a: number): number {\n    return a * 2;\n  }\n}\n\nclass TestClassConstructor {\n  public constructor(public mock1: TestMock, public readonly b: number, public mock2: TestMock) {}\n\n  public method(): number {\n    return mock1.method(this.b)+mock2.method(this.b);\n  }\n}\n\ndescribe('Mockers', () =\u003e {\n  // define variable using `mocks` and anonymous class with mocks you want create\n  let m = mocks(\n    new (class {\n      mock1: TestMock;\n      mock2: TestMock;\n    })(),\n  );\n\n  // variable to create instance with injected mocks\n  let testClass: TestClassConstructor;\n\n\n  beforeEach(() =\u003e {\n    // use xFresh() to get new Mockers instance\n    // methods of Mockers using `x` prefix for be after your mocks names on VS list\n    m = m.xFresh();\n\n    // you can create new instance of your class with injected mocks and other parameters\n    testClass = m.xNewInject(TestClassConstructor, { b: 10 });\n\n    // other non mock parameters can be given as object or array\n    // testClass = m.xNewInject(TestClassConstructor, [10]);\n  });\n\n  afterEach(() =\u003e {\n    m.xCheckExpections(); // checks all mocks expections\n  });\n\n  test('test()', () =\u003e {\n    m.mock1.expects('method', 10).andReturn(5);\n    m.mock2.expects('method', 10).andReturn(6);\n\n    const current = testClass.method();\n  });\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhexancore%2Fmocker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhexancore%2Fmocker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhexancore%2Fmocker/lists"}