{"name":"jest-canvas-mock","version":"2.5.3","keywords":["mock","jest","jest-mock","echarts","canvas","test","unit"],"author":{"name":"hustcc"},"license":"MIT","_id":"jest-canvas-mock@2.5.3","maintainers":[{"name":"jtenner","email":"tenner.joshua@gmail.com"},{"name":"atool","email":"i@hust.cc"}],"homepage":"https://github.com/hustcc/jest-canvas-mock#readme","bugs":{"url":"https://github.com/hustcc/jest-canvas-mock/issues"},"dist":{"shasum":"bc7937454b5cb5f011477e335fbe1164eb71a0f1","tarball":"http://registry-npm.ywwl.com/jest-canvas-mock/download/jest-canvas-mock-2.5.3.tgz","fileCount":22,"integrity":"sha512-yyJ6q6ILoDjS1bx/lKxOzDJW7eCdnJ35jlv3EGblgBsl94WKf0mlUnQEu5lG0f6BQnznPj/EfqMn+/NO7i62TA==","signatures":[{"sig":"MEQCIFpyMvnYaZbmGehi4fYeeKR+icAawDPbsuSAmvR18oOPAiBYpIL6FqCFJUq8s/yte8uesd8+6H1i14qGeO0Vhd3bmA==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":112366,"size":22603,"noattachment":false,"key":"/jest-canvas-mock/-/jest-canvas-mock-2.5.3.tgz"},"jest":{"setupFiles":["./src/index.js","./__mocks__/worker.js"],"collectCoverage":true,"collectCoverageFrom":["src/classes/**/*.js","src/mock/**/*.js"]},"main":"lib/index.js","_from":"file:jest-canvas-mock-2.5.3.tgz","husky":{"hooks":{"commit-msg":"commitlint -E HUSKY_GIT_PARAMS","pre-commit":"npm run prettier && npm run test && npm run build"}},"types":"types/index.d.ts","scripts":{"test":"jest --no-cache","build":"babel src --out-dir lib","prepare":"npm run build","prettier":"prettier --write .","coveralls":"cat ./coverage/lcov.info | coveralls"},"_npmUser":{"name":"atool","email":"i@hust.cc"},"_resolved":"/workspaces/npm/jest-canvas-mock-2.5.3.tgz","_integrity":"sha512-yyJ6q6ILoDjS1bx/lKxOzDJW7eCdnJ35jlv3EGblgBsl94WKf0mlUnQEu5lG0f6BQnznPj/EfqMn+/NO7i62TA==","commitlint":{"extends":["@commitlint/config-angular"]},"deprecated":"This version was published in error, please use the latest version instead.","repository":{"url":"git+https://github.com/hustcc/jest-canvas-mock.git","type":"git"},"_npmVersion":"11.9.0","description":"Mock a canvas in your jest tests.","directories":{},"_nodeVersion":"24.14.0","dependencies":{"moo-color":"^1.0.2","cssfontparser":"^1.2.1"},"_hasShrinkwrap":false,"devDependencies":{"jest":"^25.3.0","husky":"^4.2.5","prettier":"^2.0.4","coveralls":"^3.0.11","@babel/cli":"^7.8.4","babel-jest":"^25.3.0","@babel/core":"^7.9.0","@antv/g2plot":"^2.3.11","@commitlint/cli":"^8.3.5","@babel/preset-env":"^7.9.5","babel-plugin-version":"^0.2.3","@commitlint/config-angular":"^8.3.4","@babel/plugin-proposal-class-properties":"^7.8.3"},"optionalDependencies":{"@antv/setup":"github:antvis/G2#1916faa365f2788b6e193514872d51a242876569"},"_npmOperationalInternal":{"tmp":"tmp/jest-canvas-mock_2.5.3_1779154771114_0.25212327136556856","host":"s3://npm-registry-packages-npm-production"},"readme":"# jest-canvas-mock\n\n> Mock `canvas` when run unit test cases with jest. For more browser environment.\n> - [jest-random-mock](https://github.com/hustcc/jest-random-mock) Mock `Math.random` in jest, with deterministic random generator.\n> - [jest-date-mock](https://github.com/hustcc/jest-date-mock) Mock `Date` when run unit test with jest, test `Date` easily.\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- [@pedroordep](https://github.com/pedroordep)\n\n## License\n\nMIT@[hustcc](https://github.com/hustcc).\n","publish_time":1779154771249,"_cnpm_publish_time":1779154771249,"dist-tags":{"latest":"2.5.8"}}