{"name":"jest-canvas-mock","version":"2.5.2","description":"Mock a canvas in your jest tests.","main":"lib/index.js","types":"types/index.d.ts","scripts":{"test":"jest --no-cache","build":"babel src --out-dir lib","coveralls":"cat ./coverage/lcov.info | coveralls","prepare":"npm run build","prettier":"prettier --write ."},"dependencies":{"cssfontparser":"^1.2.1","moo-color":"^1.0.2"},"devDependencies":{"@antv/g2plot":"^2.3.11","@babel/cli":"^7.8.4","@babel/core":"^7.9.0","@babel/plugin-proposal-class-properties":"^7.8.3","@babel/preset-env":"^7.9.5","@commitlint/cli":"^8.3.5","@commitlint/config-angular":"^8.3.4","babel-jest":"^25.3.0","babel-plugin-version":"^0.2.3","coveralls":"^3.0.11","husky":"^4.2.5","jest":"^25.3.0","prettier":"^2.0.4"},"commitlint":{"extends":["@commitlint/config-angular"]},"husky":{"hooks":{"commit-msg":"commitlint -E HUSKY_GIT_PARAMS","pre-commit":"npm run prettier && npm run test && npm run build"}},"jest":{"collectCoverage":true,"collectCoverageFrom":["src/classes/**/*.js","src/mock/**/*.js"],"setupFiles":["./src/index.js","./__mocks__/worker.js"]},"repository":{"type":"git","url":"git+https://github.com/hustcc/jest-canvas-mock.git"},"keywords":["mock","jest","jest-mock","echarts","canvas","test","unit"],"author":{"name":"hustcc"},"license":"MIT","bugs":{"url":"https://github.com/hustcc/jest-canvas-mock/issues"},"homepage":"https://github.com/hustcc/jest-canvas-mock#readme","gitHead":"dc35dbcf009fa04eb1aad442b88cfff3860186da","_id":"jest-canvas-mock@2.5.2","_nodeVersion":"14.21.3","_npmVersion":"6.14.18","dist":{"integrity":"sha512-vgnpPupjOL6+L5oJXzxTxFrlGEIbHdZqFU+LFNdtLxZ3lRDCl17FlTMM7IatoRQkrcyOTMlDinjUguqmQ6bR2A==","shasum":"7e21ebd75e05ab41c890497f6ba8a77f915d2ad6","tarball":"http://registry-npm.ywwl.com/jest-canvas-mock/download/jest-canvas-mock-2.5.2.tgz","fileCount":17,"unpackedSize":112254,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCY416TK23KN4sB0gUyU9rzvq/dpNv6bnIz331KD4sEgQIhAKf9cfUtUIIqGlIpqy6FTxLn+lqTAG63KyItPi5Vi5Yr"}],"size":22318,"noattachment":false,"key":"/jest-canvas-mock/-/jest-canvas-mock-2.5.2.tgz"},"_npmUser":{"name":"atool","email":"i@hust.cc"},"directories":{},"maintainers":[{"name":"jtenner","email":"tenner.joshua@gmail.com"},{"name":"atool","email":"i@hust.cc"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/jest-canvas-mock_2.5.2_1687916126824_0.11922068403166941"},"_hasShrinkwrap":false,"readme":"# jest-canvas-mock\n\n> Mock `canvas` when run unit test cases with jest. For more browser environment, you can use [jest-electron](https://github.com/hustcc/jest-electron) for real browser runtime.\n\n[![Build Status](https://github.com/hustcc/jest-canvas-mock/workflows/build/badge.svg)](https://github.com/hustcc/jest-canvas-mock/actions/workflows/build.yml)\n[![Coverage Status](https://coveralls.io/repos/github/hustcc/jest-canvas-mock/badge.svg?branch=master)](https://coveralls.io/github/hustcc/jest-canvas-mock)\n[![npm](https://img.shields.io/npm/v/jest-canvas-mock.svg)](https://www.npmjs.com/package/jest-canvas-mock)\n[![npm](https://img.shields.io/npm/dm/jest-canvas-mock.svg)](https://www.npmjs.com/package/jest-canvas-mock)\n[![Mentioned in Awesome Jest](https://awesome.re/mentioned-badge.svg)](https://github.com/jest-community/awesome-jest)\n\n## Install\n\nThis should only be installed as a development dependency (`devDependencies`) as it is only designed for testing.\n\n```bash\nnpm i --save-dev jest-canvas-mock\n```\n\n## Setup\n\nIn your `package.json` under the `jest`, create a `setupFiles` array and add `jest-canvas-mock` to the array.\n\n```json\n{\n  \"jest\": {\n    \"setupFiles\": [\"jest-canvas-mock\"]\n  }\n}\n```\n\nIf you already have a `setupFiles` attribute you can also append `jest-canvas-mock` to the array.\n\n```json\n{\n  \"jest\": {\n    \"setupFiles\": [\"./__setups__/other.js\", \"jest-canvas-mock\"]\n  }\n}\n```\n\nMore about in [configuration section](https://facebook.github.io/jest/docs/en/configuration.html#content).\n\n## Setup file\n\nAlternatively you can create a new setup file which then requires this module or\nadd the `require` statement to an existing setup file.\n\n`__setups__/canvas.js`\n\n```js\nimport 'jest-canvas-mock';\n// or\nrequire('jest-canvas-mock');\n```\n\nAdd that file to your `setupFiles` array:\n\n```json\n\"jest\": {\n  \"setupFiles\": [\n    \"./__setups__/canvas.js\"\n  ]\n}\n```\n\n## Reset\n\nIf you reset the jest mocks (for example, with `jest.resetAllMocks()`), you can\ncall `setupJestCanvasMock()` to re-create it.\n\n```\nimport { setupJestCanvasMock } from 'jest-canvas-mock';\n\nbeforeEach(() => {\n  jest.resetAllMocks();\n  setupJestCanvasMock();\n});\n```\n\n## Mock Strategy\n\nThis mock strategy implements all the canvas functions and actually verifies the parameters. If a\nknown condition would cause the browser to throw a `TypeError` or a `DOMException`, it emulates the\nerror. For instance, the `CanvasRenderingContext2D#arc` function will throw a `TypeError` if the\nradius is negative, or if it was not provided with enough parameters.\n\n```ts\n// arc throws a TypeError when the argument length is less than 5\nexpect(() => ctx.arc(1, 2, 3, 4)).toThrow(TypeError);\n\n// when radius is negative, arc throws a dom exception when all parameters are finite\nexpect(() => ctx.arc(0, 0, -10, 0, Math.PI * 2)).toThrow(DOMException);\n```\n\nThe function will do `Number` type coercion and verify the inputs exactly like the browser does. So\nthis is valid input.\n\n```ts\nexpect(() => ctx.arc('10', '10', '20', '0', '6.14')).not.toThrow();\n```\n\nAnother part of the strategy is to validate input types. When using the\n`CanvasRenderingContext2D#fill` function, if you pass it an invalid `fillRule` it will throw a\n`TypeError` just like the browser does.\n\n```ts\nexpect(() => ctx.fill('invalid!')).toThrow(TypeError);\nexpect(() => ctx.fill(new Path2D(), 'invalid!')).toThrow(TypeError);\n```\n\nWe try to follow the ECMAScript specification as closely as possible.\n\n## Snapshots\n\nThere are multiple ways to validate canvas state using snapshots. There are currently three methods\nattached to the `CanvasRenderingContext2D` class. The first way to use this feature is by using the\n`__getEvents` method.\n\n```ts\n/**\n * In order to see which functions and properties were used for the test, you can use `__getEvents`\n * to gather this information.\n */\nconst events = ctx.__getEvents();\n\nexpect(events).toMatchSnapshot(); // jest will assert the events match the snapshot\n```\n\nThe second way is to inspect the current path associated with the context.\n\n```ts\nctx.beginPath();\nctx.arc(1, 2, 3, 4, 5);\nctx.moveTo(6, 7);\nctx.rect(6, 7, 8, 9);\nctx.closePath();\n\n/**\n * Any method that modifies the current path (and subpath) will be pushed to an event array. When\n * using the `__getPath` method, that array will sliced and usable for snapshots.\n */\nconst path = ctx.__getPath();\nexpect(path).toMatchSnapshot();\n```\n\nThe third way is to inspect all of the successful draw calls submitted to the context.\n\n```ts\nctx.drawImage(img, 0, 0);\n\n/**\n * Every drawImage, fill, stroke, fillText, or strokeText function call will be logged in an event\n * array. This method will return those events here for inspection.\n */\nconst calls = ctx.__getDrawCalls();\nexpect(calls).toMatchSnapshot();\n```\n\nIn some cases it may be useful to clear the events or draw calls that have already been logged.\n\n```ts\n// Clear events\nctx.__clearEvents();\n\n// Clear draw calls\nctx.__clearDrawCalls();\n```\n\nFinally, it's possible to inspect the clipping region calls by using the `__getClippingRegion`\nfunction.\n\n```ts\nconst clippingRegion = ctx.__getClippingRegion();\nexpect(clippingRegion).toMatchSnapshot();\n```\n\nThe clipping region cannot be cleared because it's based on the stack values and when the `.clip()`\nfunction is called.\n\n## Override default mock return value\n\nYou can override the default mock return value in your test to suit your need. For example, to override return value of `toDataURL`:\n\n```ts\ncanvas.toDataURL.mockReturnValueOnce(\n  'data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='\n);\n```\n\n## Contributors\n\n- [@hustcc](https://github.com/hustcc)\n- [@jtenner](https://github.com/jtenner)\n- [@evanoc0](https://github.com/evanoc0)\n- [@lekha](https://github.com/lekha)\n- [@yonatankra](https://github.com/yonatankra)\n- [@LitoMore](https://github.com/LitoMore)\n- [@hrd543](https://github.com/hrd543)\n- [@danielrentz](https://github.com/danielrentz)\n\n## License\n\nMIT@[hustcc](https://github.com/hustcc).\n","publish_time":1687916126994,"_cnpm_publish_time":1687916126994,"dist-tags":{"latest":"2.5.8"}}