{"id":17287670,"url":"https://github.com/pact-foundation/nestjs-pact","last_synced_at":"2025-04-09T23:19:04.780Z","repository":{"id":40501359,"uuid":"323065308","full_name":"pact-foundation/nestjs-pact","owner":"pact-foundation","description":"Injectable Pact.js Consumer/Producer for NestJS","archived":false,"fork":false,"pushed_at":"2024-10-14T14:36:37.000Z","size":1247,"stargazers_count":49,"open_issues_count":10,"forks_count":14,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-10-30T00:00:23.731Z","etag":null,"topics":["consumer-driven-contracts","nestjs","pact-foundation","pact-js","pact-node","test-framework"],"latest_commit_sha":null,"homepage":"","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/pact-foundation.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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}},"created_at":"2020-12-20T12:21:13.000Z","updated_at":"2024-09-03T05:13:14.000Z","dependencies_parsed_at":"2024-01-02T15:05:50.607Z","dependency_job_id":"d517a1a8-152b-46d1-9ae6-9c691934252d","html_url":"https://github.com/pact-foundation/nestjs-pact","commit_stats":{"total_commits":58,"total_committers":9,"mean_commits":6.444444444444445,"dds":0.7068965517241379,"last_synced_commit":"9d42e5c8494a506e33458a470c09c659f85b5102"},"previous_names":["omermorad/nestjs-pact"],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pact-foundation%2Fnestjs-pact","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pact-foundation%2Fnestjs-pact/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pact-foundation%2Fnestjs-pact/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pact-foundation%2Fnestjs-pact/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pact-foundation","download_url":"https://codeload.github.com/pact-foundation/nestjs-pact/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247318742,"owners_count":20919484,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["consumer-driven-contracts","nestjs","pact-foundation","pact-js","pact-node","test-framework"],"created_at":"2024-10-15T10:06:00.982Z","updated_at":"2025-04-09T23:19:04.747Z","avatar_url":"https://github.com/pact-foundation.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![ISC license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)\n[![npm version](http://img.shields.io/npm/v/nestjs-pact.svg?style=flat)](https://npmjs.org/package/nestjs-pact \"View this project on npm\")\n[![Codecov Coverage](https://img.shields.io/codecov/c/github/omermorad/nestjs-pact/master.svg?style=flat-square)](https://codecov.io/gh/omer-morad-ni/nestjs-pact)\n[![CircleCI](https://circleci.com/gh/omermorad/nestjs-pact.svg?style=shield)](https://circleci.com/gh/circleci/circleci-docs)\n[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)\n\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"logo.jpg\" alt=\"NestJS + Pact Logo\" width=\"500\" /\u003e\n\n  \u003ch3 align=\"center\"\u003e\n    NestJS + Pact\n  \u003c/h3\u003e\n\n  \u003cp align=\"center\"\u003e\n    \u003cstrong\u003eInjectable Pact.js Consumer/Provider for NestJS\u003c/strong\u003e\n  \u003c/p\u003e\n\u003c/p\u003e\n\n## Table Of Contents\n\n- [Table Of Contents](#table-of-contents)\n- [Installation](#installation)\n- [Example](#example)\n- [About](#about)\n- [Introduction](#introduction)\n  - [Consumer](#consumer)\n  - [Provider](#provider)\n- [License](#license)\n- [Acknowledgements](#acknowledgements)\n\n\n## Installation\n\nPact-JS v10.x+ users\n\n```bash\nnpm i -D nestjs-pact @pact-foundation/pact\n```\n\nPact-JS v9.x users\n\n```bash\nnpm i -D nestjs-pact@2.2.2 @pact-foundation/pact@9.18.1\n```\n\n## Example\nIf you want to see a fully working end-to-end example with NestJS and Pact I recommend you to\n[jump to the NestJS official examples at the PactJS Github repository](https://github.com/pact-foundation/pact-js/tree/master/examples)\n\n## About\n\nThis package enables you to consume Pact.js in a way that can be used very easily in NestJS. \\\nLike the nature of Pact, this package is for testing purposes only.\n\nIf you are not familiar with Pact, Pact is fast, easy and reliable testing framework for integrating web apps, APIs and microservices.\nRead more on [Pact official website](https://pact.io/)\n\nThere are two main modules suggested; one for the `Provider` role (`Verifier`), and one for the `Consumer` role (creating Pact files and publish), each loaded separately.\nOf course, you can also use both modules and play the role of `Consumer` and `Provider` at the same time.\n\n## Introduction\n\nThe use of each of the modules suggested here, is made in the common and accepted form of NestJS modules.\nThe simplest way is to use the `register` method and pass the settings directly.\nIt is also enable to use the `registerAsync` method to pass the settings in the form of `useFactory` or `useClass` for example.\n\nOne more thing - the usage of the modules is for testing purposes only, which is not quite common in the use of NestJS modules, so there are some good examples down below.\nThe obvious advantage of this package is that Pact can be used in combination with the techniques and benefits offered by NestJS.\n\n### Consumer\n\nIn order to use the `Consumer` module, you need to follow a few simple steps, let's go over it!\n\nFirst, create a file called `pact.module.ts` in your `test` folder (or wherever you put your tests), and simply\nload the `PactConsumerModule` like below:\n\n**test/pact/pact.module.ts**\n\n```typescript\nimport { Module } from '@nestjs/common';\nimport { PactConsumerModule } from 'nestjs-pact';\n\n@Module({\n  imports: [\n    PactConsumerModule.register({ ... }),\n  ],\n})\nexport class PactModule {}\n```\n\nYay, now let's create the test file! let's call it `my-test.spec.ts`\n\n**test/pact/my-test.spec.ts**\n\n```typescript\nimport { Pact } from '@pact-foundation/pact';\nimport { Test } from '@nestjs/testing';\nimport { PactFactory } from 'nestjs-pact';\nimport { PactModule } from '@test/pact/pact.module';\n\ndescribe('Pact', () =\u003e {\n  let pactFactory: PactFactory;\n  let provider: Pact;\n\n  beforeAll(async () =\u003e {\n    const moduleRef = await Test.createTestingModule({\n      imports: [SomeOtherModule, AndAnotherModuleYouNeed, PactModule],\n    }).compile();\n\n    pactFactory = moduleRef.get(PactFactory);\n\n    provider = pactFactory.createContractBetween({\n      consumer: 'Consumer Service Name',\n      provider: 'Provider Service Name',\n    });\n\n    await provider.setup();\n  });\n\n  afterEach(() =\u003e provider.verify());\n\n  afterAll(() =\u003e provider.finalize());\n\n  describe('when something happens', () =\u003e {\n    describe('and another thing happens too', () =\u003e {\n      beforeAll(() =\u003e provider.addInteraction({ ... }));\n\n      it('should do something', () =\u003e {\n        return expect( ... );\n      });\n    });\n  });\n});\n```\n\nNow let's look at how we can publish the pacts created from the test file to a Pact broker!\n\n**test/pact/publish-pacts.ts**\n\n```typescript\nimport { NestFactory } from '@nestjs/core';\nimport { Logger, LoggerService } from '@nestjs/common';\nimport { Publisher } from '@pact-foundation/pact-cli';\nimport { PactModuleProviders } from 'nestjs-pact';\nimport { PactModule } from '@test/pact/pact.module';\n\n(async () =\u003e {\n  const app = await NestFactory.createApplicationContext(PactModule);\n\n  const publisher: Publisher = app.get(PactModuleProviders.PactPublisher);\n  const logger: LoggerService = app.get(Logger);\n\n  if (process.env.CI !== 'true') {\n    logger.log('Skipping Pact publish as not on CI');\n    process.exit(0);\n  }\n\n  try {\n    await publisher.publishPacts();\n\n    logger.log('Pact contract publishing complete!');\n    logger.log('');\n    logger.log('Head over to [https://test.pact.dius.com.au/ and login with](https://test.pactflow.io/login?code=98f7810e-c7dc-493b-9c3d-7849952f1d9a\u0026utm_medium=web\u0026utm_source=nestjs-pact-readme)');\n    logger.log('to see your published contracts.');\n  } catch (e) {\n    logger.error('Pact contract publishing failed: ', e);\n  }\n})();\n```\n\n`npx ts-node test/pact/publish-pacts.ts`\n\nRun the file and you are good to go :)\n\nNote: in your `tsconfig.json` file make sure you set `allowJs` to `true` in order to run the file\n\n### Provider\n\nThe usage in the `Provider` service is quite easy; In your `/test` folder (or wherever you put your tests)\ncreate a simple test module with NestJS `Test.createTestingModule` method and import the `PactProviderModule` module\nfrom `nestjs-pact`.\n\nYou can use `register` or `registerAsync` method, make sure you stick to `PactProviderOptions` interface options. \\\nAfter creating the Nest application from the testing module, pass the app instance to the `verify` method,\nit will generate a random (available) port, spin up the application and run the verifier against the application url.\n\nYou can read more about Pact Verification in the [official Pact documentation](https://docs.pact.io/getting_started/verifying_pacts/)\n\nHere is a quick and simple example:\n\n```typescript\nimport { Test } from '@nestjs/testing';\nimport { INestApplication, Logger, LoggerService } from '@nestjs/common';\nimport { PactProviderModule, PactVerifierService } from 'nestjs-pact';\nimport { AppModule } from '@app/app.module';\n\ndescribe('Pact Verification', () =\u003e {\n  let verifierService: PactVerifierService;\n  let logger: LoggerService;\n  let app: INestApplication;\n\n  beforeAll(async () =\u003e {\n    const moduleRef = await Test.createTestingModule({\n      imports: [AppModule, PactProviderModule.register({ ... })],\n    }).compile();\n\n    verifierService = moduleRef.get(PactVerifierService);\n    logger = moduleRef.get(Logger);\n\n    app = moduleRef.createNestApplication();\n\n    await app.init();\n  });\n\n  it('validates the expectations of Matching Service', async () =\u003e {\n    const { output } = await verifierService.verify(app);\n\n    logger.log('Pact Verification Completed!');\n    logger.log(output);\n  });\n\n  afterAll(async () =\u003e {\n    await app.close();\n  });\n});\n```\n\n## License\n\nDistributed under the MIT License. See `LICENSE` for more information.\n\n## Acknowledgements\n\n- [@pact-foundation/pact](https://github.com/pact-foundation/pact-js)\n- [NestJS](https://github.com/nestjs/nest)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpact-foundation%2Fnestjs-pact","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpact-foundation%2Fnestjs-pact","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpact-foundation%2Fnestjs-pact/lists"}