Back to Storybook

Automock Register Spy

docs/_snippets/automock-register-spy.md

10.3.64.8 KB
Original Source
ts
// Replace your-framework with the framework you are using (e.g., react-vite, nextjs, vue3-vite, sveltekit)
import type { Preview } from '@storybook/your-framework';
import { sb } from 'storybook/test';

// ๐Ÿ‘‡ Automatically spies on all exports from the `lib/session` local module
sb.mock(import('../lib/session.ts'), { spy: true });
// ๐Ÿ‘‡ Automatically spies on all exports from the `uuid` package in `node_modules`
sb.mock(import('uuid'), { spy: true });

const preview: Preview = {
  // ...
};

export default preview;
js
import { sb } from 'storybook/test';

// ๐Ÿ‘‡ Automatically spies on all exports from the `lib/session` local module
sb.mock('../lib/session.js', { spy: true });
// ๐Ÿ‘‡ Automatically spies on all exports from the `uuid` package in `node_modules`
sb.mock('uuid', { spy: true });

export default {
  // ...
};
ts
// Replace your-framework with the framework you are using (e.g., react-vite, nextjs, nextjs-vite)
import { definePreview } from '@storybook/your-framework';

import { sb } from 'storybook/test';

// ๐Ÿ‘‡ Automatically spies on all exports from the `lib/session` local module
sb.mock(import('../lib/session.ts'), { spy: true });
// ๐Ÿ‘‡ Automatically spies on all exports from the `uuid` package in `node_modules`
sb.mock(import('uuid'), { spy: true });

export default definePreview({
  // ...
});
<!-- JS snippets still needed while providing both CSF 3 & Next -->
js
// Replace your-framework with the framework you are using (e.g., react-vite, nextjs, nextjs-vite)
import { definePreview } from '@storybook/your-framework';

import { sb } from 'storybook/test';

// ๐Ÿ‘‡ Automatically spies on all exports from the `lib/session` local module
sb.mock('../lib/session.js', { spy: true });
// ๐Ÿ‘‡ Automatically spies on all exports from the `uuid` package in `node_modules`
sb.mock('uuid', { spy: true });

export default definePreview({
  // ...
});
ts
import { definePreview } from '@storybook/vue3-vite';

import { sb } from 'storybook/test';

// ๐Ÿ‘‡ Automatically spies on all exports from the `lib/session` local module
sb.mock(import('../lib/session.ts'), { spy: true });
// ๐Ÿ‘‡ Automatically spies on all exports from the `uuid` package in `node_modules`
sb.mock(import('uuid'), { spy: true });

export default definePreview({
  // ...
});
<!-- JS snippets still needed while providing both CSF 3 & Next -->
js
import { definePreview } from '@storybook/vue3-vite';

import { sb } from 'storybook/test';

// ๐Ÿ‘‡ Automatically spies on all exports from the `lib/session` local module
sb.mock('../lib/session.js', { spy: true });
// ๐Ÿ‘‡ Automatically spies on all exports from the `uuid` package in `node_modules`
sb.mock('uuid', { spy: true });

export default definePreview({
  // ...
});
ts
import { definePreview } from '@storybook/angular';

import { sb } from 'storybook/test';

// ๐Ÿ‘‡ Automatically spies on all exports from the `lib/session` local module
sb.mock(import('../lib/session.ts'), { spy: true });
// ๐Ÿ‘‡ Automatically spies on all exports from the `uuid` package in `node_modules`
sb.mock(import('uuid'), { spy: true });

export default definePreview({
  // ...
});
ts
import { definePreview } from '@storybook/web-components-vite';

import { sb } from 'storybook/test';

// ๐Ÿ‘‡ Automatically spies on all exports from the `lib/session` local module
sb.mock(import('../lib/session.ts'), { spy: true });
// ๐Ÿ‘‡ Automatically spies on all exports from the `uuid` package in `node_modules`
sb.mock(import('uuid'), { spy: true });

export default definePreview({
  // ...
});
<!-- JS snippets still needed while providing both CSF 3 & Next -->
js
import { definePreview } from '@storybook/web-components-vite';

import { sb } from 'storybook/test';

// ๐Ÿ‘‡ Automatically spies on all exports from the `lib/session` local module
sb.mock('../lib/session.js', { spy: true });
// ๐Ÿ‘‡ Automatically spies on all exports from the `uuid` package in `node_modules`
sb.mock('uuid', { spy: true });

export default definePreview({
  // ...
});