storybook-addon-vis 0.16.0 → 0.16.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,14 +11,11 @@ if (globalThis.__vitest_browser__) {
11
11
  }
12
12
  var commands = new Proxy({}, {
13
13
  get(_target, prop) {
14
- return browserContext?.commands?.[prop] ?? /* v8 ignore start : used in storybook, not in vitest */
15
- (() => {
16
- throw new Error(`Command '${String(prop)}' not found`);
17
- })();
14
+ return browserContext?.commands?.[prop];
18
15
  }
19
16
  });
20
17
  var getCurrentTest = () => vitestSuite?.getCurrentTest();
21
18
 
22
19
  export { commands, getCurrentTest };
23
- //# sourceMappingURL=chunk-GZMKOHHR.js.map
24
- //# sourceMappingURL=chunk-GZMKOHHR.js.map
20
+ //# sourceMappingURL=chunk-Q6DPWZ7S.js.map
21
+ //# sourceMappingURL=chunk-Q6DPWZ7S.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/client/vitest_proxy.ts"],"names":[],"mappings":";AAoBA,IAAI,cAAA;AACJ,IAAI,WAAA;AAEJ,IAAK,WAAmB,kBAAoB,EAAA;AAC3C,EAAA,OAAO,yBAAyB,CAAA,CAAE,IAAK,CAAA,CAAC,CAAM,KAAA;AAC7C,IAAiB,cAAA,GAAA,CAAA;AAAA,GACjB,CAAA;AACD,EAAA,OAAO,cAAc,CAAA,CAAE,IAAK,CAAA,CAAC,CAAM,KAAA;AAClC,IAAc,WAAA,GAAA,CAAA;AAAA,GACd,CAAA;AACF;AAEO,IAAM,QAAW,GAAA,IAAI,KAAuB,CAAA,EAAW,EAAA;AAAA,EAC7D,GAAA,CAAI,SAAS,IAAM,EAAA;AAClB,IAAQ,OAAA,cAAA,EAAgB,WAAmB,IAAI,CAAA;AAAA;AAEjD,CAAC;AAEY,IAAA,cAAA,GAAiB,MAAM,WAAA,EAAa,cAAe","file":"chunk-Q6DPWZ7S.js","sourcesContent":["import type { BrowserCommands } from '@vitest/browser/context'\nimport type {\n\tHasImageSnapshotCommand,\n\tImageSnapshotNextIndexCommand,\n\tMatchImageSnapshotCommand,\n\tPrepareImageSnapshotComparisonCommand,\n\tSetupVisSuiteCommand,\n\tWriteImageSnapshotCommand,\n} from 'vitest-plugin-vis/commands'\n\ndeclare module '@vitest/browser/context' {\n\tinterface BrowserCommands\n\t\textends MatchImageSnapshotCommand,\n\t\t\tHasImageSnapshotCommand,\n\t\t\tPrepareImageSnapshotComparisonCommand,\n\t\t\tImageSnapshotNextIndexCommand,\n\t\t\tWriteImageSnapshotCommand,\n\t\t\tSetupVisSuiteCommand {}\n}\n\nlet browserContext: Awaited<typeof import('@vitest/browser/context')>\nlet vitestSuite: Awaited<typeof import('vitest/suite')>\n\nif ((globalThis as any).__vitest_browser__) {\n\timport('@vitest/browser/context').then((m) => {\n\t\tbrowserContext = m\n\t})\n\timport('vitest/suite').then((m) => {\n\t\tvitestSuite = m\n\t})\n}\n\nexport const commands = new Proxy<BrowserCommands>({} as any, {\n\tget(_target, prop) {\n\t\treturn (browserContext?.commands as any)?.[prop]\n\t},\n})\n\nexport const getCurrentTest = () => vitestSuite?.getCurrentTest()\n"]}
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import './matcher.js';
1
2
  export * from 'vitest-plugin-vis';
2
3
  import { ToMatchImageSnapshotOptions, ImageSnapshotIdOptions } from 'vitest-plugin-vis/client';
3
4
  export { ToMatchImageSnapshotOptions } from 'vitest-plugin-vis/client';
@@ -9,9 +10,11 @@ declare function defineAutoSnapshotParam(snapshot: ToMatchImageSnapshotOptions):
9
10
  snapshot: ToMatchImageSnapshotOptions;
10
11
  };
11
12
 
13
+ declare function isSnapshotEnabled(tags: string[]): boolean;
14
+
12
15
  /**
13
16
  * Check if the snapshot image exists.
14
17
  */
15
18
  declare function hasImageSnapshot(options?: ImageSnapshotIdOptions | undefined): false | Promise<boolean>;
16
19
 
17
- export { defineAutoSnapshotParam, hasImageSnapshot };
20
+ export { defineAutoSnapshotParam, hasImageSnapshot, isSnapshotEnabled };
package/dist/index.js CHANGED
@@ -1,13 +1,36 @@
1
- import { getCurrentTest, commands } from './chunk-GZMKOHHR.js';
1
+ import { getCurrentTest, commands } from './chunk-Q6DPWZ7S.js';
2
+ import { expect } from '@storybook/test';
3
+ import { success, imageSnapshotMatcher, toTaskId } from 'vitest-plugin-vis/client';
2
4
  export * from 'vitest-plugin-vis';
3
- import { toTaskId } from 'vitest-plugin-vis/client';
5
+
6
+ function toMatchImageSnapshot(subject, options) {
7
+ const test = getCurrentTest();
8
+ if (!test) return Promise.resolve(success);
9
+ return imageSnapshotMatcher(commands)(test, subject, options).then(() => success);
10
+ }
11
+
12
+ // src/client/storybook/expect_extend.ts
13
+ expect.extend({ toMatchImageSnapshot });
4
14
 
5
15
  // src/client/storybook/param.ts
6
16
  function defineAutoSnapshotParam(snapshot) {
7
17
  return { snapshot };
8
18
  }
19
+ function isSnapshotEnabled(tags) {
20
+ return tags.lastIndexOf("!snapshot") < tags.lastIndexOf("snapshot");
21
+ }
22
+
23
+ // src/client/ctx.ts
24
+ var ctx = {
25
+ getCurrentTest,
26
+ __test__reset() {
27
+ ctx.getCurrentTest = getCurrentTest;
28
+ }
29
+ };
30
+
31
+ // src/client/has_image_snapshot.ts
9
32
  function hasImageSnapshot(options) {
10
- const test = getCurrentTest();
33
+ const test = ctx.getCurrentTest();
11
34
  if (!test) return false;
12
35
  const taskId = toTaskId(test);
13
36
  if (options?.customizeSnapshotId) {
@@ -16,6 +39,6 @@ function hasImageSnapshot(options) {
16
39
  return commands.hasImageSnapshot(taskId);
17
40
  }
18
41
 
19
- export { defineAutoSnapshotParam, hasImageSnapshot };
42
+ export { defineAutoSnapshotParam, hasImageSnapshot, isSnapshotEnabled };
20
43
  //# sourceMappingURL=index.js.map
21
44
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/client/storybook/param.ts","../src/client/has_image_snapshot.ts"],"names":[],"mappings":";;;;;AAKO,SAAS,wBAAwB,QAAuC,EAAA;AAC9E,EAAA,OAAO,EAAE,QAAS,EAAA;AACnB;ACDO,SAAS,iBAAiB,OAA8C,EAAA;AAC9E,EAAA,MAAM,OAAO,cAAe,EAAA;AAC5B,EAAI,IAAA,CAAC,MAAa,OAAA,KAAA;AAElB,EAAM,MAAA,MAAA,GAAS,SAAS,IAAI,CAAA;AAC5B,EAAA,IAAI,SAAS,mBAAqB,EAAA;AACjC,IAAA,OAAO,QACL,CAAA,sBAAA,CAAuB,MAAM,CAAA,CAC7B,KAAK,CAAC,KAAA,KAAU,QAAS,CAAA,gBAAA,CAAiB,QAAQ,OAAQ,CAAA,mBAAA,CAAqB,MAAQ,EAAA,KAAK,CAAC,CAAC,CAAA;AAAA;AAGjG,EAAO,OAAA,QAAA,CAAS,iBAAiB,MAAM,CAAA;AACxC","file":"index.js","sourcesContent":["import type { ToMatchImageSnapshotOptions } from 'vitest-plugin-vis/client'\n\n/**\n * Define snapshot parameter for auto snapshot.\n */\nexport function defineAutoSnapshotParam(snapshot: ToMatchImageSnapshotOptions) {\n\treturn { snapshot }\n}\n\n// This fix the inferred type cannot be named error\nexport type { ToMatchImageSnapshotOptions }\n","import { type ImageSnapshotIdOptions, toTaskId } from 'vitest-plugin-vis/client'\nimport { commands, getCurrentTest } from './vitest_proxy.ts'\n\n/**\n * Check if the snapshot image exists.\n */\nexport function hasImageSnapshot(options?: ImageSnapshotIdOptions | undefined) {\n\tconst test = getCurrentTest()\n\tif (!test) return false\n\n\tconst taskId = toTaskId(test)\n\tif (options?.customizeSnapshotId) {\n\t\treturn commands\n\t\t\t.imageSnapshotNextIndex(taskId)\n\t\t\t.then((index) => commands.hasImageSnapshot(taskId, options.customizeSnapshotId!(taskId, index)))\n\t}\n\n\treturn commands.hasImageSnapshot(taskId)\n}\n"]}
1
+ {"version":3,"sources":["../src/client/expect/to_match_image_snapshot.ts","../src/client/storybook/expect_extend.ts","../src/client/storybook/param.ts","../src/client/ctx.ts","../src/client/has_image_snapshot.ts"],"names":[],"mappings":";;;;;AAIO,SAAS,oBAAA,CAKf,SACA,OACyB,EAAA;AACzB,EAAA,MAAM,OAAO,cAAe,EAAA;AAG5B,EAAA,IAAI,CAAC,IAAA,EAAa,OAAA,OAAA,CAAQ,QAAQ,OAAO,CAAA;AAGzC,EAAO,OAAA,oBAAA,CAAqB,QAAQ,CAAE,CAAA,IAAA,EAAM,SAAS,OAAO,CAAA,CAAE,IAAK,CAAA,MAAM,OAAO,CAAA;AACjF;;;AChBA,MAAO,CAAA,MAAA,CAAO,EAAE,oBAAA,EAAsB,CAAA;;;ACE/B,SAAS,wBAAwB,QAAuC,EAAA;AAC9E,EAAA,OAAO,EAAE,QAAS,EAAA;AACnB;AAIO,SAAS,kBAAkB,IAAgB,EAAA;AACjD,EAAA,OAAO,KAAK,WAAY,CAAA,WAAW,CAAI,GAAA,IAAA,CAAK,YAAY,UAAU,CAAA;AACnE;;;ACXO,IAAM,GAAM,GAAA;AAAA,EAClB,cAAA;AAAA,EACA,aAAgB,GAAA;AACf,IAAA,GAAA,CAAI,cAAiB,GAAA,cAAA;AAAA;AAEvB,CAAA;;;ACAO,SAAS,iBAAiB,OAA8C,EAAA;AAC9E,EAAM,MAAA,IAAA,GAAO,IAAI,cAAe,EAAA;AAChC,EAAI,IAAA,CAAC,MAAa,OAAA,KAAA;AAElB,EAAM,MAAA,MAAA,GAAS,SAAS,IAAI,CAAA;AAC5B,EAAA,IAAI,SAAS,mBAAqB,EAAA;AACjC,IAAA,OAAO,QACL,CAAA,sBAAA,CAAuB,MAAM,CAAA,CAC7B,KAAK,CAAC,KAAA,KAAU,QAAS,CAAA,gBAAA,CAAiB,QAAQ,OAAQ,CAAA,mBAAA,CAAqB,MAAQ,EAAA,KAAK,CAAC,CAAC,CAAA;AAAA;AAGjG,EAAO,OAAA,QAAA,CAAS,iBAAiB,MAAM,CAAA;AACxC","file":"index.js","sourcesContent":["import type { AsyncExpectationResult } from '@vitest/expect'\nimport { type ToMatchImageSnapshotOptions, imageSnapshotMatcher, success } from 'vitest-plugin-vis/client'\nimport { commands, getCurrentTest } from '../vitest_proxy.ts'\n\nexport function toMatchImageSnapshot(\n\t/**\n\t * The element or locator to take the snapshot of,\n\t * or the base64 value of the image to compare against.\n\t */\n\tsubject: any,\n\toptions?: ToMatchImageSnapshotOptions | undefined,\n): AsyncExpectationResult {\n\tconst test = getCurrentTest()\n\n\t/* v8 ignore start: stub as success when not in a test (e.g. in a story) */\n\tif (!test) return Promise.resolve(success)\n\t/* v8 ignore end */\n\n\treturn imageSnapshotMatcher(commands)(test, subject, options).then(() => success)\n}\n","import { expect } from '@storybook/test'\nimport { toMatchImageSnapshot } from '../expect/to_match_image_snapshot.ts'\n\nexpect.extend({ toMatchImageSnapshot })\n","import type { ToMatchImageSnapshotOptions } from 'vitest-plugin-vis/client'\n\n/**\n * Define snapshot parameter for auto snapshot.\n */\nexport function defineAutoSnapshotParam(snapshot: ToMatchImageSnapshotOptions) {\n\treturn { snapshot }\n}\n\n// This fix the inferred type cannot be named error\nexport type { ToMatchImageSnapshotOptions }\nexport function isSnapshotEnabled(tags: string[]) {\n\treturn tags.lastIndexOf('!snapshot') < tags.lastIndexOf('snapshot')\n}\n","import { getCurrentTest } from './vitest_proxy.ts'\n\nexport const ctx = {\n\tgetCurrentTest,\n\t__test__reset() {\n\t\tctx.getCurrentTest = getCurrentTest\n\t},\n}\n","import { type ImageSnapshotIdOptions, toTaskId } from 'vitest-plugin-vis/client'\nimport { ctx } from './ctx.ts'\nimport { commands } from './vitest_proxy.ts'\n\n/**\n * Check if the snapshot image exists.\n */\nexport function hasImageSnapshot(options?: ImageSnapshotIdOptions | undefined) {\n\tconst test = ctx.getCurrentTest()\n\tif (!test) return false\n\n\tconst taskId = toTaskId(test)\n\tif (options?.customizeSnapshotId) {\n\t\treturn commands\n\t\t\t.imageSnapshotNextIndex(taskId)\n\t\t\t.then((index) => commands.hasImageSnapshot(taskId, options.customizeSnapshotId!(taskId, index)))\n\t}\n\n\treturn commands.hasImageSnapshot(taskId)\n}\n"]}
@@ -0,0 +1,8 @@
1
+ import { ImageSnapshotMatcher } from 'vitest-plugin-vis/client';
2
+
3
+ declare global {
4
+ namespace jest {
5
+ interface Matchers<R, T> extends ImageSnapshotMatcher {
6
+ }
7
+ }
8
+ }
@@ -0,0 +1,3 @@
1
+
2
+ //# sourceMappingURL=matcher.js.map
3
+ //# sourceMappingURL=matcher.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"matcher.js"}
package/dist/preview.js CHANGED
@@ -1,2 +1,2 @@
1
- import {expect}from'@storybook/test';import {matchImageSnapshot,success}from'vitest-plugin-vis/client';import {setAutoSnapshotOptions}from'vitest-plugin-vis';var s,a;globalThis.__vitest_browser__&&(import('@vitest/browser/context').then(t=>{s=t;}),import('vitest/suite').then(t=>{a=t;}));new Proxy({},{get(t,e){return s?.commands?.[e]??(()=>{throw new Error(`Command '${String(e)}' not found`)})()}});var n=()=>a?.getCurrentTest();function m(t,e){let o=n();return o?matchImageSnapshot(o,t,e).then(()=>success):Promise.resolve(success)}expect.extend({toMatchImageSnapshot:m});var i={beforeEach(t){let e=t.tags,o=e?e.lastIndexOf("!snapshot")<e.lastIndexOf("snapshot"):!1,p=n();setAutoSnapshotOptions(p,{enable:o,...t.parameters?.snapshot});}};var O=i;export{O as default};//# sourceMappingURL=preview.js.map
1
+ import {expect}from'@storybook/test';import {imageSnapshotMatcher,success}from'vitest-plugin-vis/client';import {setAutoSnapshotOptions}from'vitest-plugin-vis';var a,s;globalThis.__vitest_browser__&&(import('@vitest/browser/context').then(t=>{a=t;}),import('vitest/suite').then(t=>{s=t;}));var r=new Proxy({},{get(t,o){return a?.commands?.[o]}}),e=()=>s?.getCurrentTest();function p(t,o){let n=e();return n?imageSnapshotMatcher(r)(n,t,o).then(()=>success):Promise.resolve(success)}expect.extend({toMatchImageSnapshot:p});function i(t){return t.lastIndexOf("!snapshot")<t.lastIndexOf("snapshot")}var h={beforeEach(t){setAutoSnapshotOptions(e(),{enable:i(t.tags),...t.parameters?.snapshot});}};var T=h;export{T as default};//# sourceMappingURL=preview.js.map
2
2
  //# sourceMappingURL=preview.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/client/vitest_proxy.ts","../src/client/expect/to_match_image_snapshot.ts","../src/client/storybook/expect_extend.ts","../src/preview/vis_annotation.ts","../src/preview.ts"],"names":["browserContext","vitestSuite","m","_target","prop","getCurrentTest","toMatchImageSnapshot","subject","options","test","matchImageSnapshot","success","expect","visAnnotations","ctx","tags","enable","setAutoSnapshotOptions","preview_default"],"mappings":"8JAgBA,IAAIA,CAAAA,CACAC,CAEC,CAAA,UAAA,CAAmB,qBACvB,OAAO,yBAAyB,CAAE,CAAA,IAAA,CAAMC,CAAM,EAAA,CAC7CF,CAAiBE,CAAAA,EAClB,CAAC,CACD,CAAA,OAAO,cAAc,CAAA,CAAE,KAAMA,CAAM,EAAA,CAClCD,CAAcC,CAAAA,EACf,CAAC,CAGK,CAAA,CAAiB,IAAI,KAAuB,CAAA,EAAW,CAAA,CAC7D,IAAIC,CAASC,CAAAA,CAAAA,CAAM,CAClB,OACEJ,CAAgB,EAAA,QAAA,GAAmBI,CAAI,CAAA,EAAA,CAEvC,IAAM,CACN,MAAM,IAAI,KAAA,CAAM,CAAY,SAAA,EAAA,MAAA,CAAOA,CAAI,CAAC,aAAa,CACtD,CAAA,GAGF,CACD,CAAC,CAEYC,KAAAA,CAAAA,CAAiB,IAAMJ,CAAAA,EAAa,iBC3B1C,SAASK,CAKfC,CAAAA,CAAAA,CACAC,CACyB,CAAA,CACzB,IAAMC,CAAAA,CAAOJ,GAGb,CAAA,OAAKI,CAGEC,CAAAA,kBAAAA,CAAmBD,CAAMF,CAAAA,CAAAA,CAASC,CAAO,CAAA,CAAE,KAAK,IAAMG,OAAO,CAHlD,CAAA,OAAA,CAAQ,OAAQA,CAAAA,OAAO,CAI1C,CCzBAC,OAAO,MAAO,CAAA,CAAE,oBAAAN,CAAAA,CAAqB,CAAC,CCHtC,CAGO,IAAMO,CAAAA,CAAiB,CAC7B,UAAA,CAAWC,CAAmB,CAAA,CAC7B,IAAMC,CAAAA,CAAOD,EAAI,IACXE,CAAAA,CAAAA,CAAUD,CAAeA,CAAAA,CAAAA,CAAK,YAAY,WAAW,CAAA,CAAIA,CAAK,CAAA,WAAA,CAAY,UAAU,CAAnE,CAAA,CAAA,CAAA,CACjBN,CAAOJ,CAAAA,CAAAA,EACbY,CAAAA,sBAAAA,CAAuBR,CAAM,CAAA,CAAE,OAAAO,CAAQ,CAAA,GAAGF,CAAI,CAAA,UAAA,EAAY,QAAS,CAAC,EACrE,CACD,CAAA,KCROI,CAAQL,CAAAA","file":"preview.js","sourcesContent":["import type { BrowserCommands } from '@vitest/browser/context'\nimport type {\n\tHasImageSnapshotCommand,\n\tImageSnapshotNextIndexCommand,\n\tMatchImageSnapshotCommand,\n\tSetupVisSuiteCommand,\n} from 'vitest-plugin-vis/commands'\n\ndeclare module '@vitest/browser/context' {\n\tinterface BrowserCommands\n\t\textends MatchImageSnapshotCommand,\n\t\t\tHasImageSnapshotCommand,\n\t\t\tImageSnapshotNextIndexCommand,\n\t\t\tSetupVisSuiteCommand {}\n}\n\nlet browserContext: Awaited<typeof import('@vitest/browser/context')>\nlet vitestSuite: Awaited<typeof import('vitest/suite')>\n\nif ((globalThis as any).__vitest_browser__) {\n\timport('@vitest/browser/context').then((m) => {\n\t\tbrowserContext = m\n\t})\n\timport('vitest/suite').then((m) => {\n\t\tvitestSuite = m\n\t})\n}\n\nexport const commands = new Proxy<BrowserCommands>({} as any, {\n\tget(_target, prop) {\n\t\treturn (\n\t\t\t(browserContext?.commands as any)?.[prop] ??\n\t\t\t/* v8 ignore start : used in storybook, not in vitest */\n\t\t\t(() => {\n\t\t\t\tthrow new Error(`Command '${String(prop)}' not found`)\n\t\t\t})()\n\t\t\t/* v8 ignore end */\n\t\t)\n\t},\n})\n\nexport const getCurrentTest = () => vitestSuite?.getCurrentTest()\n","import type { AsyncExpectationResult } from '@vitest/expect'\nimport {\n\ttype ToMatchImageSnapshotOptions,\n\tmatchImageSnapshot,\n\tparseImageSnapshotSubject,\n\tsuccess,\n\ttoTaskId,\n} from 'vitest-plugin-vis/client'\nimport { commands, getCurrentTest } from '../vitest_proxy.ts'\n\nexport interface ImageSnapshotMatcher {\n\ttoMatchImageSnapshot(options?: ToMatchImageSnapshotOptions | undefined): Promise<void>\n}\n\nexport function toMatchImageSnapshot(\n\t/**\n\t * The element or locator to take the snapshot of,\n\t * or the base64 value of the image to compare against.\n\t */\n\tsubject: any,\n\toptions?: ToMatchImageSnapshotOptions | undefined,\n): AsyncExpectationResult {\n\tconst test = getCurrentTest()\n\n\t/* v8 ignore start: stub as success when not in a test (e.g. in a story) */\n\tif (!test) return Promise.resolve(success)\n\t/* v8 ignore end */\n\n\treturn matchImageSnapshot(test, subject, options).then(() => success)\n}\n","import { expect } from '@storybook/test'\nimport './augment.ts'\nimport { toMatchImageSnapshot } from '../expect/to_match_image_snapshot.ts'\n\nexpect.extend({ toMatchImageSnapshot })\n","import type { ProjectAnnotations, Renderer, StoryContext } from 'storybook/internal/types'\nimport { setAutoSnapshotOptions } from 'vitest-plugin-vis'\nimport { getCurrentTest } from '../client/vitest_proxy.ts'\n\nexport const visAnnotations = {\n\tbeforeEach(ctx: StoryContext) {\n\t\tconst tags = ctx.tags\n\t\tconst enable = !tags ? false : tags.lastIndexOf('!snapshot') < tags.lastIndexOf('snapshot')\n\t\tconst test = getCurrentTest()\n\t\tsetAutoSnapshotOptions(test, { enable, ...ctx.parameters?.snapshot })\n\t},\n} satisfies ProjectAnnotations<Renderer>\n","import './client/storybook/expect_extend.ts'\nimport { visAnnotations } from './preview/vis_annotation.ts'\n\nexport default visAnnotations\n"]}
1
+ {"version":3,"sources":["../src/client/vitest_proxy.ts","../src/client/expect/to_match_image_snapshot.ts","../src/client/storybook/expect_extend.ts","../src/client/storybook/param.ts","../src/preview/vis_annotation.ts","../src/preview.ts"],"names":["browserContext","vitestSuite","m","commands","_target","prop","getCurrentTest","toMatchImageSnapshot","subject","options","test","imageSnapshotMatcher","success","expect","isSnapshotEnabled","tags","visAnnotations","ctx","setAutoSnapshotOptions","preview_default"],"mappings":"gKAoBA,IAAIA,CACAC,CAAAA,CAAAA,CAEC,WAAmB,kBACvB,GAAA,OAAO,yBAAyB,CAAA,CAAE,IAAMC,CAAAA,CAAAA,EAAM,CAC7CF,CAAAA,CAAiBE,EAClB,CAAC,CACD,CAAA,OAAO,cAAc,CAAA,CAAE,IAAMA,CAAAA,CAAAA,EAAM,CAClCD,CAAcC,CAAAA,EACf,CAAC,CAAA,CAAA,CAGK,IAAMC,CAAAA,CAAW,IAAI,KAAA,CAAuB,EAAC,CAAU,CAC7D,GAAA,CAAIC,CAASC,CAAAA,CAAAA,CAAM,CAClB,OAAQL,GAAgB,QAAmBK,GAAAA,CAAI,CAChD,CACD,CAAC,CAAA,CAEYC,CAAiB,CAAA,IAAML,GAAa,cAAe,EAAA,CClCzD,SAASM,CAAAA,CAKfC,CACAC,CAAAA,CAAAA,CACyB,CACzB,IAAMC,EAAOJ,CAAe,EAAA,CAG5B,OAAKI,CAAAA,CAGEC,oBAAqBR,CAAAA,CAAQ,CAAEO,CAAAA,CAAAA,CAAMF,CAASC,CAAAA,CAAO,CAAE,CAAA,IAAA,CAAK,IAAMG,OAAO,CAH9D,CAAA,OAAA,CAAQ,QAAQA,OAAO,CAI1C,CChBAC,MAAAA,CAAO,MAAO,CAAA,CAAE,oBAAAN,CAAAA,CAAqB,CAAC,CAAA,CCQ/B,SAASO,CAAAA,CAAkBC,EAAgB,CACjD,OAAOA,CAAK,CAAA,WAAA,CAAY,WAAW,CAAA,CAAIA,CAAK,CAAA,WAAA,CAAY,UAAU,CACnE,CCRO,IAAMC,CAAiB,CAAA,CAC7B,UAAWC,CAAAA,CAAAA,CAAmB,CAC7BC,sBAAuBZ,CAAAA,CAAAA,EAAkB,CAAA,CAAE,MAAQQ,CAAAA,CAAAA,CAAkBG,CAAI,CAAA,IAAI,EAAG,GAAGA,CAAAA,CAAI,UAAY,EAAA,QAAS,CAAC,EAC9G,CACD,CAAA,KCLOE,CAAQH,CAAAA","file":"preview.js","sourcesContent":["import type { BrowserCommands } from '@vitest/browser/context'\nimport type {\n\tHasImageSnapshotCommand,\n\tImageSnapshotNextIndexCommand,\n\tMatchImageSnapshotCommand,\n\tPrepareImageSnapshotComparisonCommand,\n\tSetupVisSuiteCommand,\n\tWriteImageSnapshotCommand,\n} from 'vitest-plugin-vis/commands'\n\ndeclare module '@vitest/browser/context' {\n\tinterface BrowserCommands\n\t\textends MatchImageSnapshotCommand,\n\t\t\tHasImageSnapshotCommand,\n\t\t\tPrepareImageSnapshotComparisonCommand,\n\t\t\tImageSnapshotNextIndexCommand,\n\t\t\tWriteImageSnapshotCommand,\n\t\t\tSetupVisSuiteCommand {}\n}\n\nlet browserContext: Awaited<typeof import('@vitest/browser/context')>\nlet vitestSuite: Awaited<typeof import('vitest/suite')>\n\nif ((globalThis as any).__vitest_browser__) {\n\timport('@vitest/browser/context').then((m) => {\n\t\tbrowserContext = m\n\t})\n\timport('vitest/suite').then((m) => {\n\t\tvitestSuite = m\n\t})\n}\n\nexport const commands = new Proxy<BrowserCommands>({} as any, {\n\tget(_target, prop) {\n\t\treturn (browserContext?.commands as any)?.[prop]\n\t},\n})\n\nexport const getCurrentTest = () => vitestSuite?.getCurrentTest()\n","import type { AsyncExpectationResult } from '@vitest/expect'\nimport { type ToMatchImageSnapshotOptions, imageSnapshotMatcher, success } from 'vitest-plugin-vis/client'\nimport { commands, getCurrentTest } from '../vitest_proxy.ts'\n\nexport function toMatchImageSnapshot(\n\t/**\n\t * The element or locator to take the snapshot of,\n\t * or the base64 value of the image to compare against.\n\t */\n\tsubject: any,\n\toptions?: ToMatchImageSnapshotOptions | undefined,\n): AsyncExpectationResult {\n\tconst test = getCurrentTest()\n\n\t/* v8 ignore start: stub as success when not in a test (e.g. in a story) */\n\tif (!test) return Promise.resolve(success)\n\t/* v8 ignore end */\n\n\treturn imageSnapshotMatcher(commands)(test, subject, options).then(() => success)\n}\n","import { expect } from '@storybook/test'\nimport { toMatchImageSnapshot } from '../expect/to_match_image_snapshot.ts'\n\nexpect.extend({ toMatchImageSnapshot })\n","import type { ToMatchImageSnapshotOptions } from 'vitest-plugin-vis/client'\n\n/**\n * Define snapshot parameter for auto snapshot.\n */\nexport function defineAutoSnapshotParam(snapshot: ToMatchImageSnapshotOptions) {\n\treturn { snapshot }\n}\n\n// This fix the inferred type cannot be named error\nexport type { ToMatchImageSnapshotOptions }\nexport function isSnapshotEnabled(tags: string[]) {\n\treturn tags.lastIndexOf('!snapshot') < tags.lastIndexOf('snapshot')\n}\n","import type { ProjectAnnotations, Renderer, StoryContext } from 'storybook/internal/types'\nimport { setAutoSnapshotOptions } from 'vitest-plugin-vis'\nimport { isSnapshotEnabled } from '../client/storybook/param.ts'\nimport { getCurrentTest } from '../client/vitest_proxy.ts'\n\nexport const visAnnotations = {\n\tbeforeEach(ctx: StoryContext) {\n\t\tsetAutoSnapshotOptions(getCurrentTest(), { enable: isSnapshotEnabled(ctx.tags), ...ctx.parameters?.snapshot })\n\t},\n} satisfies ProjectAnnotations<Renderer>\n","import './client/storybook/expect_extend.ts'\nimport './shared/global_matcher_augment.ts'\nimport { visAnnotations } from './preview/vis_annotation.ts'\n\nexport default visAnnotations\n"]}
@@ -1,4 +1,4 @@
1
- import { commands } from './chunk-GZMKOHHR.js';
1
+ import { commands } from './chunk-Q6DPWZ7S.js';
2
2
  import { createVis } from 'vitest-plugin-vis/setup';
3
3
 
4
4
  var vis = createVis(commands);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "storybook-addon-vis",
3
- "version": "0.16.0",
3
+ "version": "0.16.2",
4
4
  "description": "Storybook Vitest visual testing addon",
5
5
  "keywords": [
6
6
  "image-snapshot",
@@ -29,6 +29,10 @@
29
29
  "import": "./dist/index.js",
30
30
  "require": "./dist/index.cjs"
31
31
  },
32
+ "./matcher": {
33
+ "types": "./dist/matcher.d.ts",
34
+ "import": "./dist/matcher.js"
35
+ },
32
36
  "./preset": "./dist/preset.cjs",
33
37
  "./preview": {
34
38
  "types": "./dist/preview.d.ts",
@@ -53,7 +57,7 @@
53
57
  ],
54
58
  "dependencies": {
55
59
  "type-plus": "^8.0.0-beta.6",
56
- "vitest-plugin-vis": "1.1.0"
60
+ "vitest-plugin-vis": "1.3.0"
57
61
  },
58
62
  "devDependencies": {
59
63
  "@storybook/addon-essentials": "^8.4.2",
@@ -71,7 +75,9 @@
71
75
  "@vitest/browser": "^2.1.8",
72
76
  "@vitest/coverage-istanbul": "^2.1.8",
73
77
  "@vitest/expect": "^2.1.8",
78
+ "boxen": "^8.0.1",
74
79
  "cross-env": "^7.0.3",
80
+ "dedent": "^1.5.1",
75
81
  "npm-run-all2": "^7.0.2",
76
82
  "react": "^18.2.0",
77
83
  "react-dom": "^18.2.0",
@@ -95,6 +101,7 @@
95
101
  "bundler": {
96
102
  "exportEntries": [
97
103
  "src/index.ts",
104
+ "src/matcher.ts",
98
105
  "src/vitest-setup.ts"
99
106
  ],
100
107
  "previewEntries": [
package/readme.md CHANGED
@@ -307,6 +307,24 @@ const config: StorybookConfig = {
307
307
  }
308
308
  ```
309
309
 
310
+ ### TypeScript Configuration
311
+
312
+ The main usage of this addon is to use the `toMatchImageSnapshot` matcher.
313
+
314
+ Since it is exposed under the `expect` object of `vitest` or `@storybook/test`,
315
+ you typically do not need to import `storybook-addon-vis` directly.
316
+
317
+ Because of this, TypeScript may not recognize the matcher.
318
+ To address this, you can add the following to your `tsconfig.json`:
319
+
320
+ ```json
321
+ {
322
+ "compilerOptions": {
323
+ "types": ["storybook-addon-vis/matcher"]
324
+ }
325
+ }
326
+ ```
327
+
310
328
  ## Usage - automatic snapshot
311
329
 
312
330
  With the `auto` preset, [`storybook-addon-vis`][storybook-addon-vis] automatically captures image snapshot for stories with `snapshot` tag.
@@ -0,0 +1,8 @@
1
+ import { getCurrentTest } from './vitest_proxy.ts'
2
+
3
+ export const ctx = {
4
+ getCurrentTest,
5
+ __test__reset() {
6
+ ctx.getCurrentTest = getCurrentTest
7
+ },
8
+ }
@@ -1,17 +1,7 @@
1
1
  import type { AsyncExpectationResult } from '@vitest/expect'
2
- import {
3
- type ToMatchImageSnapshotOptions,
4
- matchImageSnapshot,
5
- parseImageSnapshotSubject,
6
- success,
7
- toTaskId,
8
- } from 'vitest-plugin-vis/client'
2
+ import { type ToMatchImageSnapshotOptions, imageSnapshotMatcher, success } from 'vitest-plugin-vis/client'
9
3
  import { commands, getCurrentTest } from '../vitest_proxy.ts'
10
4
 
11
- export interface ImageSnapshotMatcher {
12
- toMatchImageSnapshot(options?: ToMatchImageSnapshotOptions | undefined): Promise<void>
13
- }
14
-
15
5
  export function toMatchImageSnapshot(
16
6
  /**
17
7
  * The element or locator to take the snapshot of,
@@ -26,5 +16,5 @@ export function toMatchImageSnapshot(
26
16
  if (!test) return Promise.resolve(success)
27
17
  /* v8 ignore end */
28
18
 
29
- return matchImageSnapshot(test, subject, options).then(() => success)
19
+ return imageSnapshotMatcher(commands)(test, subject, options).then(() => success)
30
20
  }
@@ -1,11 +1,12 @@
1
1
  import { type ImageSnapshotIdOptions, toTaskId } from 'vitest-plugin-vis/client'
2
- import { commands, getCurrentTest } from './vitest_proxy.ts'
2
+ import { ctx } from './ctx.ts'
3
+ import { commands } from './vitest_proxy.ts'
3
4
 
4
5
  /**
5
6
  * Check if the snapshot image exists.
6
7
  */
7
8
  export function hasImageSnapshot(options?: ImageSnapshotIdOptions | undefined) {
8
- const test = getCurrentTest()
9
+ const test = ctx.getCurrentTest()
9
10
  if (!test) return false
10
11
 
11
12
  const taskId = toTaskId(test)
@@ -1,5 +1,4 @@
1
1
  import { expect } from '@storybook/test'
2
- import './augment.ts'
3
2
  import { toMatchImageSnapshot } from '../expect/to_match_image_snapshot.ts'
4
3
 
5
4
  expect.extend({ toMatchImageSnapshot })
@@ -9,3 +9,6 @@ export function defineAutoSnapshotParam(snapshot: ToMatchImageSnapshotOptions) {
9
9
 
10
10
  // This fix the inferred type cannot be named error
11
11
  export type { ToMatchImageSnapshotOptions }
12
+ export function isSnapshotEnabled(tags: string[]) {
13
+ return tags.lastIndexOf('!snapshot') < tags.lastIndexOf('snapshot')
14
+ }
@@ -3,14 +3,18 @@ import type {
3
3
  HasImageSnapshotCommand,
4
4
  ImageSnapshotNextIndexCommand,
5
5
  MatchImageSnapshotCommand,
6
+ PrepareImageSnapshotComparisonCommand,
6
7
  SetupVisSuiteCommand,
8
+ WriteImageSnapshotCommand,
7
9
  } from 'vitest-plugin-vis/commands'
8
10
 
9
11
  declare module '@vitest/browser/context' {
10
12
  interface BrowserCommands
11
13
  extends MatchImageSnapshotCommand,
12
14
  HasImageSnapshotCommand,
15
+ PrepareImageSnapshotComparisonCommand,
13
16
  ImageSnapshotNextIndexCommand,
17
+ WriteImageSnapshotCommand,
14
18
  SetupVisSuiteCommand {}
15
19
  }
16
20
 
@@ -28,14 +32,7 @@ if ((globalThis as any).__vitest_browser__) {
28
32
 
29
33
  export const commands = new Proxy<BrowserCommands>({} as any, {
30
34
  get(_target, prop) {
31
- return (
32
- (browserContext?.commands as any)?.[prop] ??
33
- /* v8 ignore start : used in storybook, not in vitest */
34
- (() => {
35
- throw new Error(`Command '${String(prop)}' not found`)
36
- })()
37
- /* v8 ignore end */
38
- )
35
+ return (browserContext?.commands as any)?.[prop]
39
36
  },
40
37
  })
41
38
 
package/src/index.ts CHANGED
@@ -1,4 +1,7 @@
1
1
  // `storybook-addon-vis` provides code that can be used in test files.
2
+ import './client/storybook/expect_extend.ts'
3
+ import './shared/global_matcher_augment.ts'
4
+
2
5
  export * from 'vitest-plugin-vis'
3
6
  export * from './client/storybook/param.ts'
4
7
  export * from './client/has_image_snapshot.ts'
package/src/matcher.ts ADDED
@@ -0,0 +1 @@
1
+ import './shared/global_matcher_augment.ts'
@@ -1,12 +1,10 @@
1
1
  import type { ProjectAnnotations, Renderer, StoryContext } from 'storybook/internal/types'
2
2
  import { setAutoSnapshotOptions } from 'vitest-plugin-vis'
3
+ import { isSnapshotEnabled } from '../client/storybook/param.ts'
3
4
  import { getCurrentTest } from '../client/vitest_proxy.ts'
4
5
 
5
6
  export const visAnnotations = {
6
7
  beforeEach(ctx: StoryContext) {
7
- const tags = ctx.tags
8
- const enable = !tags ? false : tags.lastIndexOf('!snapshot') < tags.lastIndexOf('snapshot')
9
- const test = getCurrentTest()
10
- setAutoSnapshotOptions(test, { enable, ...ctx.parameters?.snapshot })
8
+ setAutoSnapshotOptions(getCurrentTest(), { enable: isSnapshotEnabled(ctx.tags), ...ctx.parameters?.snapshot })
11
9
  },
12
10
  } satisfies ProjectAnnotations<Renderer>
package/src/preview.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import './client/storybook/expect_extend.ts'
2
+ import './shared/global_matcher_augment.ts'
2
3
  import { visAnnotations } from './preview/vis_annotation.ts'
3
4
 
4
5
  export default visAnnotations
@@ -1,6 +1,6 @@
1
1
  /// <reference types="@vitest/browser/context" />
2
2
 
3
- import type { ImageSnapshotMatcher } from '../expect/to_match_image_snapshot.ts'
3
+ import type { ImageSnapshotMatcher } from 'vitest-plugin-vis/client'
4
4
 
5
5
  declare global {
6
6
  namespace jest {
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/client/vitest_proxy.ts"],"names":[],"mappings":";AAgBA,IAAI,cAAA;AACJ,IAAI,WAAA;AAEJ,IAAK,WAAmB,kBAAoB,EAAA;AAC3C,EAAA,OAAO,yBAAyB,CAAA,CAAE,IAAK,CAAA,CAAC,CAAM,KAAA;AAC7C,IAAiB,cAAA,GAAA,CAAA;AAAA,GACjB,CAAA;AACD,EAAA,OAAO,cAAc,CAAA,CAAE,IAAK,CAAA,CAAC,CAAM,KAAA;AAClC,IAAc,WAAA,GAAA,CAAA;AAAA,GACd,CAAA;AACF;AAEO,IAAM,QAAW,GAAA,IAAI,KAAuB,CAAA,EAAW,EAAA;AAAA,EAC7D,GAAA,CAAI,SAAS,IAAM,EAAA;AAClB,IACE,OAAA,cAAA,EAAgB,WAAmB,IAAI,CAAA;AAAA,IAAA,CAEvC,MAAM;AACN,MAAA,MAAM,IAAI,KAAM,CAAA,CAAA,SAAA,EAAY,MAAO,CAAA,IAAI,CAAC,CAAa,WAAA,CAAA,CAAA;AAAA,KACnD,GAAA;AAAA;AAIN,CAAC;AAEY,IAAA,cAAA,GAAiB,MAAM,WAAA,EAAa,cAAe","file":"chunk-GZMKOHHR.js","sourcesContent":["import type { BrowserCommands } from '@vitest/browser/context'\nimport type {\n\tHasImageSnapshotCommand,\n\tImageSnapshotNextIndexCommand,\n\tMatchImageSnapshotCommand,\n\tSetupVisSuiteCommand,\n} from 'vitest-plugin-vis/commands'\n\ndeclare module '@vitest/browser/context' {\n\tinterface BrowserCommands\n\t\textends MatchImageSnapshotCommand,\n\t\t\tHasImageSnapshotCommand,\n\t\t\tImageSnapshotNextIndexCommand,\n\t\t\tSetupVisSuiteCommand {}\n}\n\nlet browserContext: Awaited<typeof import('@vitest/browser/context')>\nlet vitestSuite: Awaited<typeof import('vitest/suite')>\n\nif ((globalThis as any).__vitest_browser__) {\n\timport('@vitest/browser/context').then((m) => {\n\t\tbrowserContext = m\n\t})\n\timport('vitest/suite').then((m) => {\n\t\tvitestSuite = m\n\t})\n}\n\nexport const commands = new Proxy<BrowserCommands>({} as any, {\n\tget(_target, prop) {\n\t\treturn (\n\t\t\t(browserContext?.commands as any)?.[prop] ??\n\t\t\t/* v8 ignore start : used in storybook, not in vitest */\n\t\t\t(() => {\n\t\t\t\tthrow new Error(`Command '${String(prop)}' not found`)\n\t\t\t})()\n\t\t\t/* v8 ignore end */\n\t\t)\n\t},\n})\n\nexport const getCurrentTest = () => vitestSuite?.getCurrentTest()\n"]}
package/dist/preview.d.ts DELETED
@@ -1,26 +0,0 @@
1
- import { ToMatchImageSnapshotOptions } from 'vitest-plugin-vis/client';
2
- import { StoryContext } from 'storybook/internal/types';
3
-
4
- declare global {
5
- namespace jest {
6
- interface Matchers<R, T = {}> {
7
- }
8
- }
9
- }
10
-
11
- interface ImageSnapshotMatcher {
12
- toMatchImageSnapshot(options?: ToMatchImageSnapshotOptions | undefined): Promise<void>;
13
- }
14
-
15
- declare global {
16
- namespace jest {
17
- interface Matchers<R, T> extends ImageSnapshotMatcher {
18
- }
19
- }
20
- }
21
-
22
- declare const visAnnotations: {
23
- beforeEach(ctx: StoryContext): void;
24
- };
25
-
26
- export { visAnnotations as default };
@@ -1,5 +0,0 @@
1
- import { expect } from 'vitest'
2
- import '../client/storybook/augment.ts'
3
- import { toMatchImageSnapshot } from '../client/expect/to_match_image_snapshot.ts'
4
-
5
- expect.extend({ toMatchImageSnapshot })
File without changes
File without changes