{"_id":"wirepig","name":"wirepig","dist-tags":{"latest":"0.1.0"},"versions":{"0.1.0":{"name":"wirepig","version":"0.1.0","description":"Better testing through the power of sockets.","keywords":["test","testing","mock","mocking","stub","stubbing","http","tcp","nock","localstack","docker-compose","network","dependency","dependencies"],"author":{"name":"will myers"},"license":"MIT","repository":{"type":"git","url":"git+https://github.com/griffinmyers/wirepig.git"},"type":"module","exports":{"import":"./src/index.js","require":"./dist/index.cjs"},"scripts":{"test":"NODE_ENV=test mocha && yarn test-format && yarn lint && yarn test-ts-types","format":"prettier --write \"**/*.{js,ts,d.ts}\"","test-format":"prettier --check \"**/*.{js,ts,d.ts}\"","lint":"eslint ./","test-ts-types":"tsc --project test/ts","build-cjs":"esbuild src/index.js --platform=node --bundle --format=cjs --outfile=dist/index.cjs","clean":"rm -rf ./dist","prepack":"yarn build-cjs","postpack":"yarn clean"},"devDependencies":{"@aws-sdk/client-sqs":"^3.204.0","@button/divvy-client":"^1.0.1","esbuild":"^0.17.8","eslint":"^8.12.0","eslint-plugin-mocha":"^10.1.0","memcached":"^2.2.2","mocha":"^9.2.2","mysql":"^2.18.1","pg":"^8.7.3","prettier":"^2.6.1","redis":"^4.0.6","typescript":"^4.9.5"},"dependencies":{"@types/node":"^18.14.1"},"prettier":{"singleQuote":true,"trailingComma":"es5"},"eslintConfig":{"env":{"browser":true,"commonjs":true,"es2021":true,"mocha":true},"extends":"eslint:recommended","parserOptions":{"ecmaVersion":"latest","sourceType":"module"},"plugins":["mocha"],"rules":{"mocha/no-exclusive-tests":"error"}},"types":"./index.d.ts","gitHead":"fff34364afe0a235edd3a7ed0c44d80f828f05cc","bugs":{"url":"https://github.com/griffinmyers/wirepig/issues"},"homepage":"https://github.com/griffinmyers/wirepig#readme","_id":"wirepig@0.1.0","_nodeVersion":"18.14.1","_npmVersion":"9.3.1","dist":{"integrity":"sha512-vdqwSxzfNy0YvKHMLdHxUgD2EVzoaeT9y2hV9esvJl+rWIPRUyfNl4R5evlo47gixX4/v2VdGbPgLQed2IAEEw==","shasum":"b6621786e79cabfe5c3ab72e78fc7c35b5b0f8de","tarball":"https://registry.npmjs.org/wirepig/-/wirepig-0.1.0.tgz","fileCount":14,"unpackedSize":70192,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDpAVJGdYHExeC0MAjb1R9epFsAD7rQhMEpFMne+WKi9gIgDTXDu3BThIytOMI1icSRlog5LnabfY/Zmk9sq+2xCYI="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJj/Sp/ACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmpH3Q/+MMhAlnqQhFMgDc5PhrSCTZsdqTUZs8thwmnhuOWhVWlcCgeQ\r\n/AhnKJTGozjKy1Wc65u7EqXOEdU/1wJbNeiHe902Jv3xRQc6kC1p/b7bZJGJ\r\nNL6oxByCzUAiv6Ghk+bDNqjZHABdEMfyNqawZWcBhsSfrKH9B4DBMXEBs0Bo\r\n1CFV/md0XEVImHVgNW6S7N8im6twEsofBsED07AUJNvAKBnAhpaIlqi/eHy8\r\nkMPhG+vt3hg6kV7qrw9vCjSDP90j6SoeyxC8ikFIiVYNzUJwtsfokIYEgm7/\r\nyddHHfJOSlg8lHcsLunEZlD+SZjThamohCFplREpqtN+hcsn1MxhDLkRf0hC\r\nEcUhJJ/QUwzRfHV43atDgZfrlSqywSVNDPSr3Tqf/4SSz7EX0FZRpaJ5mMWG\r\nStjGTCPrfpe7RGfyzCQpv/lrlAfOqxSbxUgyXIByaYhw6HpvHMMF+lAYqqJX\r\ncqEGKZyRgK3rpQKjINWY35096vFecTDKRaj2gvCfpxE1kONavAVs+D8s0r1i\r\nAaTdGxML+qdew4FJ1IBnyTNbEo4xmdAEG+guWlKyQlDjR+UoFX3XlbLsTwwz\r\nj5A2z7LI0rTdgpobwC+55d7KG4Rz+XGbwEDzJdVS69dpgc+6r2VBfO66F74Q\r\nCEtmU5YxFUTweJ6BdhlNW/7bNDogAwmvNEQ=\r\n=8GFE\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"griffinmyers","email":"griffin.myers@gmail.com"},"directories":{},"maintainers":[{"name":"griffinmyers","email":"griffin.myers@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/wirepig_0.1.0_1677535871668_0.8529801067996903"},"_hasShrinkwrap":false}},"time":{"created":"2023-02-27T22:11:11.482Z","0.1.0":"2023-02-27T22:11:11.849Z","modified":"2023-02-27T22:11:12.039Z"},"maintainers":[{"name":"griffinmyers","email":"griffin.myers@gmail.com"}],"description":"Better testing through the power of sockets.","homepage":"https://github.com/griffinmyers/wirepig#readme","keywords":["test","testing","mock","mocking","stub","stubbing","http","tcp","nock","localstack","docker-compose","network","dependency","dependencies"],"repository":{"type":"git","url":"git+https://github.com/griffinmyers/wirepig.git"},"author":{"name":"will myers"},"bugs":{"url":"https://github.com/griffinmyers/wirepig/issues"},"license":"MIT","readme":"# wirepig [![test](https://github.com/griffinmyers/wirepig/actions/workflows/test.yaml/badge.svg)](https://github.com/griffinmyers/wirepig/actions/workflows/test.yaml)\n\nWirepig helps you test software that relies on something over a network. If\nyour code talks to any of the following, then wirepig can help:\n\n* **HTTP Services** (AWS, Twilio, another application you're building, ...)\n* **Databases** (mysql, postgres, ...)\n* **Caches** (redis, memcached, ...)\n* **Rate Limiters** (divvy, ...)\n* anything else accessed by a TCP connection\n\nWirepig spins up an HTTP or TCP server that can programmatically behave like your\nsoftware's actual dependencies. Simply tell wirepig what kinds of requests to\nemulate and point your application at it. No monkey-patching the runtime, no\nexternal processes, no dependency injection, just honest-to-goodness sockets.\n\n<div width=\"50%\" align=center><img src=\"./docs/assets/arch.svg\" /></div>\n\nRequires [NodeJS `>=14`](https://nodejs.dev/en/about/releases/).\n\n* [Documentation](/docs)\n* [Types](/index.d.ts)\n\n### Quick Start\n\nInstall with your favorite package manager:\n\n###### npm\n```console\nnpm install --save-dev wirepig\n```\n\n###### yarn\n```console\nyarn add --dev wirepig\n```\n\nPull in however you get your modules:\n\n###### ESM\n```js\nimport { http, tcp, helpers, errors } from 'wirepig';\n```\n\n###### CommonJS\n```js\nconst { http, tcp, helpers, errors } = require('wirepig');\n```\n\nAnd get to mocking:\n\n```js\nconst dep = await http();\n\nconst mock = dep.mock({\n  req: { method: 'POST', pathname: '/bloop' },\n  res: { statusCode: 200, body: 'bloop' },\n});\n\nconst res = await request.post(`http://localhost:${dep.port}/bloop`)\n\nassert.strictEqual(res.statusCode, 200);\nassert.strictEqual(res.text, 'bloop');\n\nawait dep.teardown();\n```\n\nHere we:\n\n1. Initialized an HTTP server as a substitute for our dependency. It's listening\n   on an ephemeral port and ready to go. See the [docs](./docs/http.md) if you\n   need to start wirepig on a well-known port.\n2. Declared a request we expect our server to receive from our application and\n   the response to emit when it does. This mock will only handle a single\n   request.\n3. Instructed our application to make an HTTP request to our server and asserted\n   the response we received. In reality, this network call would probably be\n   buried deep within your application.\n4. Closed our HTTP server.\n\nIt's **highly** recommended that you utilize a test runner's lifecycle hooks to\nproperly manage your mocks:\n\n```js\nbefore(async function () {\n  this.dep = await tcp();\n});\n\nafterEach(function () {\n  this.dep.reset();\n});\n\nafter(async function () {\n  await this.dep.teardown();\n});\n\nit('sends a GET request to redis', async function() {\n  this.dep.mock({\n    req: ['*2', '$3', 'GET', '$8', 'ns:bloop', ''].join('\\r\\n'),\n    res: ['$17', 'bloop-the-big-one', ''].join('\\r\\n'),\n  });\n\n  const client = createClient({ url: `redis://localhost:${this.dep.port}` });\n  await client.connect();\n\n  assert.strictEqual(await client.get('ns:bloop'), 'bloop-the-big-one');\n\n  await client.disconnect();\n});\n```\n\n* **Before** the test suite runs, spin up the server you need.\n* **After Each** test runs, reset the server. This will assert that all\n  previously declared mocks have been satisfied and clear any internal state.\n* **After** the test suite runs, close the server.\n* **During** a test, declare any mocks needed.\n\nMaking sense? Head over to the [docs](/docs) for a full accounting of\nwhat wirepig can do.\n\n### Troubleshooting\n\nWhen things get confounding, set the `NODE_DEBUG` environment variable to one\nof the following:\n\n| `$NODE_DEBUG` | Behavior |\n| -- | -- |\n| `wirepig`| Logs general diagnostics |\n| `wirepig.match`| Logs diagnostics related to request matching |\n| `wirepig*`| Logs all diagnostics |\n\nWith the expanded output in your console, you ought to be able to figure out\nwhat's going on.\n\nAlternatively, a tool like [Wireshark](https://www.wireshark.org/) can help you\nmonitor all packets flowing between your application and wirepig, and might\nyield a clue.\n\n### A Brief Note on the Design\n\nWirepig occupies a space similar to [nock](https://github.com/nock/nock) except\ninstead of overriding node internals, wirepig operates as an independent server\nyour application sends real requests to. Since you spin up wirepig in the same\nprocess as your tests, you still have programmatic control over it.\n\nI'm optimistic this design will offer a few notable upsides:\n\n1. **Better Tests:** Overriding node internals in our tests means we aren't\n  actually testing the networking stack used in production. Wirepig pushes the\n  mocking boundary out of your application code and into the network itself\n  while still being easy to operate.\n2. **More Robust:** Node internals change on every release and may break any\n  overriding (example: https://github.com/nock/nock/pull/1182). Since wirepig\n  steers clear of that technique, it'll likely be robust to new versions of\n  node.\n3. **Tooling:** By sending packets out over a network, we get to\n  leverage the deep bench of excellent networking tools available to us (like\n  [Wireshark](https://www.wireshark.org/)).\n\nThe wirepig API strives to offer every desirable feature in a simple package,\noften at the expense of terseness. For example, there's no built-in magic for\nhandling JSON, even though it's an exceptionally common serialization format\n(there are some handy [helpers](./docs/helpers.md), though).\nFirst and foremost, wirepig wants to be a strong foundation other projects can\nbuild on top of (for example, a redis-specific mocking library).\n\nOf course, wirepig isn't the best tool for every job, and even though it can\ntechnically mock a postgres database, that might not be the best idea depending\non your needs. Other tools I highly recommend:\n\n* **[Docker Compose](https://docs.docker.com/compose/)** is a nice way to run\n  tests against real applications like redis or mysql, if a bit heavyweight.\n* **[Localstack](https://github.com/localstack/localstack)** does a remarkable\n  job emulating AWS' public API.\n\n### Contributing\n\nWirepig is open source, but only cautiously open to code contributions. Please do\nnot open a pull request as a way to introduce a change (if you do, I will\nlikely close it and ask you to please file an\n[issue](https://github.com/griffinmyers/wirepig/issues)).\n\nThat said, I'm eager for any feature requests, bug reports, or general feedback.\nPlease submit an [issue](https://github.com/griffinmyers/wirepig/issues) to get\nin touch and we'll take things from there.\n","readmeFilename":"README.md"}