{"_id":"@codeceptjs/pactum-mock-server","_rev":"2-b0a7821abfc1f017399a28a6e1dee2db","name":"@codeceptjs/pactum-mock-server","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@codeceptjs/pactum-mock-server","version":"1.0.0","author":{"name":"kobenguyent@gmail.com"},"license":"MIT","_id":"@codeceptjs/pactum-mock-server@1.0.0","maintainers":[{"name":"egor_bodnar","email":"maxameron@gmail.com"},{"name":"davert","email":"davert.ua@gmail.com"}],"homepage":"https://github.com/codeceptjs/pactum-mock-server#readme","bugs":{"url":"https://github.com/codeceptjs/pactum-mock-server/issues"},"dist":{"shasum":"fd958f04a507df1fb4e7bebacf4705475694b860","tarball":"https://registry.npmjs.org/@codeceptjs/pactum-mock-server/-/pactum-mock-server-1.0.0.tgz","fileCount":4,"integrity":"sha512-e/xeZVbUED0sog9ZpNrUE0bJKCwuNqHEu4aV6S1E1U1Hr+sR0sHyBTFyzpVP2hxWgZ9D/xwZfqcu3HWBu+zWoQ==","signatures":[{"sig":"MEUCIQDUmN0gmVjppBnPzO5pQf/iS2dFRPtflEw6TOrI1jYx5QIgU+hwa7B6kAw7KKCwkBFyK5upoHzgpXszc8JQ+VjuMOg=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":17399},"jest":{"transform":{}},"main":"index.js","type":"module","exports":{"import":"./index.js","require":"./index.cjs"},"gitHead":"f18307679a1d0662df777d124e99b227bb1bebed","scripts":{"docs":"npx documentation readme index.cjs --section=MockServer --shallow","test":"node --experimental-vm-modules node_modules/jest/bin/jest.js","buildcjs":"rollup index.js --file index.cjs --format cjs"},"_npmUser":{"name":"davert","email":"davert.ua@gmail.com"},"repository":{"url":"git+https://github.com/codeceptjs/pactum-mock-server.git","type":"git"},"_npmVersion":"10.9.0","description":"Mock Server helper for CodeceptJS - powered by pactum","directories":{},"_nodeVersion":"20.18.0","dependencies":{"pactum":"^3.7.3"},"_hasShrinkwrap":false,"devDependencies":{"chai":"^5.1.2","jest":"^29.7.0","rollup":"^4.24.4","documentation":"11.0.1"},"_npmOperationalInternal":{"tmp":"tmp/pactum-mock-server_1.0.0_1730800056071_0.4370183426721814","host":"s3://npm-registry-packages"}}},"time":{"created":"2024-11-05T09:47:35.978Z","modified":"2026-04-13T13:03:45.542Z","1.0.0":"2024-11-05T09:47:36.230Z"},"bugs":{"url":"https://github.com/codeceptjs/pactum-mock-server/issues"},"author":{"name":"kobenguyent@gmail.com"},"license":"MIT","homepage":"https://github.com/codeceptjs/pactum-mock-server#readme","repository":{"url":"git+https://github.com/codeceptjs/pactum-mock-server.git","type":"git"},"description":"Mock Server helper for CodeceptJS - powered by pactum","maintainers":[{"email":"davert.ua@gmail.com","name":"davert"}],"readme":"## MockServer\n\n<!-- Generated by documentation.js. Update this documentation by updating the source code. -->\n\n#### Table of Contents\n\n-   [config](#config)\n-   [Configuration](#configuration)\n    -   [Properties](#properties)\n-   [MockServer](#mockserver)\n    -   -   [Examples](#examples)\n        -   [Adding Interactions](#adding-interactions)\n        -   [Request Matching](#request-matching)\n            -   [Strong Match on Query Params](#strong-match-on-query-params)\n            -   [Loose Match on Body](#loose-match-on-body)\n-   [Methods](#methods)\n    -   [Parameters](#parameters)\n    -   [startMockServer](#startmockserver)\n        -   [Parameters](#parameters-1)\n    -   [stopMockServer](#stopmockserver)\n    -   [addInteractionToMockServer](#addinteractiontomockserver)\n        -   [Parameters](#parameters-2)\n\n### config\n\n### Configuration\n\nThis helper should be configured in codecept.conf.(js|ts)\n\nType: [object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)\n\n#### Properties\n\n-   `port` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** Mock server port\n-   `host` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** Mock server host\n-   `httpsOpts` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** key & cert values are the paths to .key and .crt files\n\n### MockServer\n\nMockServer powered by [pactum](https://www.npmjs.com/package/pactum)\n\nThe MockServer Helper in CodeceptJS empowers you to mock any server or service via HTTP or HTTPS, making it an excellent tool for simulating REST endpoints and other HTTP-based APIs.\n\n<!-- configuration -->\n\n##### Examples\n\nYou can seamlessly integrate MockServer with other helpers like REST or Playwright. Here's a configuration example inside the `codecept.conf.js` file:\n\n```javascript\n{\n  helpers: {\n    REST: {...},\n    MockServer: {\n      require: '@codeceptjs/pactum-mock-server',\n      // default mock server config\n      port: 9393,\n      host: '0.0.0.0',\n      httpsOpts: {\n        key: '',\n        cert: '',\n      },\n    },\n  }\n}\n```\n\n##### Adding Interactions\n\nInteractions add behavior to the mock server. Use the `I.addInteractionToMockServer()` method to include interactions. It takes an interaction object as an argument, containing request and response details.\n\n```javascript\nI.addInteractionToMockServer({\n   request: {\n     method: 'GET',\n     path: '/api/hello'\n   },\n   response: {\n     status: 200,\n     body: {\n       'say': 'hello to mock server'\n     }\n   }\n});\n```\n\n##### Request Matching\n\nWhen a real request is sent to the mock server, it matches the received request with the interactions. If a match is found, it returns the specified response; otherwise, a 404 status code is returned.\n\n-   Strong match on HTTP Method, Path, Query Params & JSON body.\n-   Loose match on Headers.\n\n###### Strong Match on Query Params\n\nYou can send different responses based on query parameters:\n\n```javascript\nI.addInteractionToMockServer({\n  request: {\n    method: 'GET',\n    path: '/api/users',\n    queryParams: {\n      id: 1\n    }\n  },\n  response: {\n    status: 200,\n    body: 'user 1'\n  }\n});\n\nI.addInteractionToMockServer({\n  request: {\n    method: 'GET',\n    path: '/api/users',\n    queryParams: {\n      id: 2\n    }\n  },\n  response: {\n    status: 200,\n    body: 'user 2'\n  }\n});\n```\n\n-   GET to `/api/users?id=1` will return 'user 1'.\n-   GET to `/api/users?id=2` will return 'user 2'.\n-   For all other requests, it returns a 404 status code.\n\n###### Loose Match on Body\n\nWhen `strict` is set to false, it performs a loose match on query params and response body:\n\n```javascript\nI.addInteractionToMockServer({\n  strict: false,\n  request: {\n    method: 'POST',\n    path: '/api/users',\n    body: {\n      name: 'john'\n    }\n  },\n  response: {\n    status: 200\n  }\n});\n```\n\n-   POST to `/api/users` with the body containing `name` as 'john' will return a 200 status code.\n-   POST to `/api/users` without the `name` property in the body will return a 404 status code.\n\nHappy testing with MockServer in CodeceptJS! 🚀\n\n### Methods\n\n#### Parameters\n\n-   `passedConfig`  \n\n#### startMockServer\n\nStart the mock server\n\n##### Parameters\n\n-   `port` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** start the mock server with given port\n\nReturns **any** void\n\n#### stopMockServer\n\nStop the mock server\n\nReturns **any** void\n\n#### addInteractionToMockServer\n\nAn interaction adds behavior to the mock server\n\n```js\nI.addInteractionToMockServer({\n   request: {\n     method: 'GET',\n     path: '/api/hello'\n   },\n   response: {\n     status: 200,\n     body: {\n       'say': 'hello to mock server'\n     }\n   }\n});\n```\n\n```js\n// with query params\nI.addInteractionToMockServer({\n   request: {\n     method: 'GET',\n     path: '/api/hello',\n     queryParams: {\n      id: 2\n    }\n   },\n   response: {\n     status: 200,\n     body: {\n       'say': 'hello to mock server'\n     }\n   }\n});\n```\n\n##### Parameters\n\n-   `interaction` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** add behavior to the mock server\n\nReturns **any** void\n","readmeFilename":"README.md"}