{"_id":"@asanovr/ts-sinon","name":"@asanovr/ts-sinon","dist-tags":{"latest":"0.2.0"},"versions":{"0.2.0":{"name":"@asanovr/ts-sinon","version":"0.2.0","description":"<p align=\"center\"> <a href=\"https://www.npmjs.com/package/@golevelup/ts-sinon\"><img src=\"https://img.shields.io/npm/v/@golevelup/ts-sinon.svg?style=flat\" alt=\"version\" /></a> <a href=\"https://www.npmjs.com/package/@golevelup/ts-sinon\"><img alt=\"downloads\"","author":{"name":"Omer Morad","email":"omer.moradd@gmail.com"},"homepage":"https://github.com/golevelup/nestjs#readme","license":"MIT","keywords":["NestJS","testing","utilities","levelup","sinon"],"main":"lib/index.js","typings":"lib/index.d.ts","directories":{"lib":"lib","test":"__tests__"},"repository":{"type":"git","url":"git+https://github.com/asanovr/nestjs.git"},"scripts":{"build":"tsc --build tsconfig.build.json","build:watch":"tsc --build tsconfig.build.json --watch","test":"jest"},"bugs":{"url":"https://github.com/golevelup/nestjs/issues"},"peerDependencies":{"sinon":"^14.x"},"devDependencies":{"@types/sinon":"^10.0.15","sinon":"^14.x"},"publishConfig":{"access":"public"},"jest":{"moduleFileExtensions":["js","json","ts"],"rootDir":"src","testRegex":".spec.ts$","transform":{"^.+\\.ts$":"ts-jest"},"coverageDirectory":"../coverage","testEnvironment":"node"},"gitHead":"83abc8861cf5f8df94b8cd39964f41ce728aff6d","_id":"@asanovr/ts-sinon@0.2.0","_nodeVersion":"16.15.1","_npmVersion":"lerna/3.22.1/node@v16.15.1+arm64 (darwin)","dist":{"integrity":"sha512-fnOXN3tUcTmUiJv9jEHffFGeMJ5YjvIvho+SDrW5QvFbPmxn1qb4V2omYDJAxrvh+nWalkRy22SP0yRjwPhKKg==","shasum":"28920acc552aceae74a498f2cf71369d7f0f828d","tarball":"https://registry.npmjs.org/@asanovr/ts-sinon/-/ts-sinon-0.2.0.tgz","fileCount":12,"unpackedSize":14145,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCEIqFnOk3nW5vV9IorWIHDKfjbTku0cIvdIMGp6mNiwgIhAN/6XDSfdrj9zOTOjIqKcdin1UT8msBfpKiXiQH5SYM9"}]},"_npmUser":{"name":"asanovr","email":"bioforge91@gmail.com"},"maintainers":[{"name":"asanovr","email":"bioforge91@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/ts-sinon_0.2.0_1693545916338_0.9994043036616593"},"_hasShrinkwrap":false}},"time":{"created":"2023-09-01T05:25:16.235Z","0.2.0":"2023-09-01T05:25:16.576Z","modified":"2023-09-01T05:25:16.894Z"},"maintainers":[{"name":"asanovr","email":"bioforge91@gmail.com"}],"description":"<p align=\"center\"> <a href=\"https://www.npmjs.com/package/@golevelup/ts-sinon\"><img src=\"https://img.shields.io/npm/v/@golevelup/ts-sinon.svg?style=flat\" alt=\"version\" /></a> <a href=\"https://www.npmjs.com/package/@golevelup/ts-sinon\"><img alt=\"downloads\"","homepage":"https://github.com/golevelup/nestjs#readme","keywords":["NestJS","testing","utilities","levelup","sinon"],"repository":{"type":"git","url":"git+https://github.com/asanovr/nestjs.git"},"author":{"name":"Omer Morad","email":"omer.moradd@gmail.com"},"bugs":{"url":"https://github.com/golevelup/nestjs/issues"},"license":"MIT","readme":"# @golevelup/ts-sinon\n\n<p align=\"center\">\n<a href=\"https://www.npmjs.com/package/@golevelup/ts-sinon\"><img src=\"https://img.shields.io/npm/v/@golevelup/ts-sinon.svg?style=flat\" alt=\"version\" /></a>\n<a href=\"https://www.npmjs.com/package/@golevelup/ts-sinon\"><img alt=\"downloads\" src=\"https://img.shields.io/npm/dt/@golevelup/ts-sinon.svg?style=flat\"></a>\n<img alt=\"license\" src=\"https://img.shields.io/npm/l/@golevelup/ts-sinon.svg\">\n</p>\n\n## Motivation\n\nWith `@golevelup/ts-sinon`'s `createMock` utility function, you can easily generate deeply nested mock objects for unit\ntesting, especially useful for mocking complex types like those found in NestJS.\n\n## Usage\n\nThis package is particularly handy when unit testing components in NestJS, but it's not limited to that. It can\nessentially mock any TypeScript interface!\n\n### Installation\n\n```sh\nnpm i @golevelup/ts-sinon --save-dev\n```\n\nor\n\n```sh\nyarn add @golevelup/ts-sinon --dev\n```\n\n### Creating Mocks\n\n1. Import the `createMock` function into your test class.\n2. Create a variable and set it equal to the `createMock` function with its generic type input.\n3. Use the mock, Luke.\n\nHere's an example with NestJS' `ExecutionContext`:\n\n```ts\nimport { createMock } from '@golevelup/ts-sinon';\nimport { ExecutionContext } from '@nestjs/common';\n\ndescribe('Mocked Execution Context', () => {\n  it('should have a fully mocked Execution Context', () => {\n    const mockExecutionContext = createMock<ExecutionContext>();\n    expect(mockExecutionContext.switchToHttp()).toBeDefined();\n  });\n});\n```\n\n`createMock` generates all sub-properties as `sinon.stub()`, so you can chain method calls:\n\n```ts\nit('should correctly resolve mocked providers', async () => {\n  const request = {\n    key: 'val',\n  };\n\n  mockExecutionContext.switchToHttp.returns(\n    createMock<HttpArgumentsHost>({\n      getRequest: () => request,\n    })\n  );\n\n  const mockResult = mockExecutionContext.switchToHttp().getRequest();\n  expect(mockResult).toBe(request);\n});\n```\n\nYou can also easily provide your own mocks:\n\n```ts\nconst mockExecutionContext = createMock<ExecutionContext>({\n  switchToHttp: () => ({\n    getRequest: () => ({\n      headers: {\n        authorization: 'auth',\n      },\n    }),\n    getResponse: sinon.stub().returns({ data: 'res return data' }),\n  }),\n});\n```\n\n> **Note**: When providing your own mocks, the number of times a parent mock function was called includes the times\n> needed to set your mocks.\n\n## Contribute\n\nContributions welcome! Read the [contribution guidelines](../../../CONTRIBUTING.md) first.\n\n## License\n\n[MIT License](../../../LICENSE)\n","readmeFilename":"README.md"}