Skip to content

Commit

Permalink
Merge pull request #27 from storybookjs/feat/support-webpack-swc
Browse files Browse the repository at this point in the history
[Breaking Change]: Replace babel-istanbul-plugin and replace it by a webpack loader
  • Loading branch information
valentinpalkovic authored Nov 14, 2023
2 parents 3d68eeb + 12a4851 commit cced79d
Show file tree
Hide file tree
Showing 40 changed files with 1,163 additions and 151 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:
- name: Prepare repository
run: git fetch --unshallow --tags

- name: Use Node.js 14.x
- name: Use Node.js 18.x
uses: actions/setup-node@v1
with:
node-version: 14.x
node-version: 18.x

- name: Install dependencies
uses: bahmutov/npm-install@v1
Expand Down
54 changes: 29 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Storybook Addon Coverage

Tools to support code coverage in Storybook and the [Storybook test runner](https://github.com/storybookjs/test-runner). It supports Storybook projects that use **Babel** or **Vite**.
Tools to support code coverage in Storybook and the [Storybook test runner](https://github.com/storybookjs/test-runner). It supports Storybook projects that use **Webpack5** or **Vite**.

### Installation

Expand All @@ -13,17 +13,17 @@ yarn add -D @storybook/addon-coverage
And by registering it in your `.storybook/main.js`:

```js
module.exports = {
export default {
addons: ["@storybook/addon-coverage"],
};
```

### Configuring the addon

This addon instruments your code by using [babel-plugin-istanbul](https://github.com/istanbuljs/babel-plugin-istanbul) if your project uses Babel or [vite-plugin-istanbul](https://github.com/iFaxity/vite-plugin-istanbul) if your project uses Vite. It provides some default configuration, but if you want to add yours, you can do so by setting the options in your `.storybook/main.js`:
This addon instruments your code by using a custom wrapper around [istanbul-lib-instrument](https://www.npmjs.com/package/istanbul-lib-instrument) if your project uses Webpack5 or [vite-plugin-istanbul](https://github.com/iFaxity/vite-plugin-istanbul) if your project uses Vite. It provides some default configuration, but if you want to add yours, you can do so by setting the options in your `.storybook/main.js`:

```js
module.exports = {
export default {
addons: [
{
name: "@storybook/addon-coverage",
Expand All @@ -37,26 +37,29 @@ module.exports = {
};
```

**The available options if your project uses Babel are as follows:**

| Option name | Description | Type | Default |
| --------------------- | ---------------------------------------------------------------------------------------- | ----------------------------------------------------------- | -------------------------------------------------------------------------------- |
| `cwd` | Set the working directory | `String` | `process.cwd()` |
| `include` | See [here](https://github.com/istanbuljs/nyc#selecting-files-for-coverage) for more info | `Array<String>` | `['**']` |
| `exclude` | See [here](https://github.com/istanbuljs/nyc#selecting-files-for-coverage) for more info | `Array<String>` | [list](https://github.com/storybookjs/addon-coverage/blob/main/src/constants.ts) |
| `extension` | List of extensions that nyc should attempt to handle in addition to `.js` | `Array<String>` | `['.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx', '.vue', '.svelte]` |
| `excludeNodeModules` | Whether or not to exclude all node_module folders (i.e. **/node_modules/**) by default | `boolean` | `true` |
| `ignoreClassMethods` | Class method names to ignore for coverage` | `Array<String>` | `[]` |
| `useInlineSourceMaps` | Variable to pass sourcemap explicitly | `object` | `-` |
| `inputSourceMap` | Scope to store the coverage variable | `string` | `-` |
| `nycrcPath` | Path to nyc config file | `string` | `-` |
| `onCover` | Hook used to track coverage for all files | `(fileName: string, fileCoverage: FileCoverage) => unknown` | `-` |
| `fileName` | File name to use in onCover hook | `string` | `-` |

> **Note:**
**The available options if your project uses Webpack5 are as follows:**

| Option name | Description | Type | Default |
| ---------------------- | -------------------------------------------------------------------------------------------------------------- | --------------- | -------------------------------------------------------------------------------------------- |
| `cwd` | Set the working directory | `String` | `process.cwd()` |
| `nycrcPath` | Path to specific nyc config to use instead of automatically searching for a nycconfig. | `string` | - |
| `include` | Glob pattern to include files. It has precedence over the include definition from your nyc config | `Array<String>` | - |
| `exclude` | Glob pattern to exclude files. It has precedence over the exclude definition from your nyc config | `Array<String>` | `defaultExclude` in https://github.com/storybookjs/addon-coverage/blob/main/src/constants.ts |
| `extension` | List of supported extensions. It has precedence over the extension definition from your nyc config | `Array<String>` | `['.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx', '.vue', '.svelte]` |
| `coverageVariable` | The global variable name that Istanbul will use to store coverage results. | `string` | - |
| `preserveComments` | Indicates whether comments in the code should be preserved during the instrumentation process. | `boolean` | `true` |
| `compact` | Controls whether the output of instrumented code is compacted. Useful for debugging when set to `false`. | `boolean` | `false` |
| `esModules` | Determines whether the code to be instrumented uses ES Module syntax. | `boolean` | `true` |
| `autoWrap` | When set to `true`, wraps program code in a function to enable top-level return statements. | `boolean` | `true` |
| `produceSourceMap` | If `true`, instructs Istanbul to produce a source map for the instrumented code. | `boolean` | `true` |
| `sourceMapUrlCallback` | A callback function that gets invoked with the filename and the source map URL when a source map is generated. | `function` | - |
| `debug` | Enables the debug mode, providing additional logging information during the instrumentation process. | `boolean` | - |

> **Note:**
> If you're using typescript, you can import the type for the options like so:
>
> ```ts
> import type { AddonOptionsBabel } from '@storybook/addon-coverage'
> import type { AddonOptionsWebpack } from "@storybook/addon-coverage";
> ```
**The available options if your project uses Vite are as follows:**
Expand All @@ -67,16 +70,17 @@ module.exports = {
| `include` | See [here](https://github.com/istanbuljs/nyc#selecting-files-for-coverage) for more info | `Array<String>` or `string` | `['**']` |
| `exclude` | See [here](https://github.com/istanbuljs/nyc#selecting-files-for-coverage) for more info | `Array<String>` or `string` | [list](https://github.com/storybookjs/addon-coverage/blob/main/src/constants.ts) |
| `extension` | List of extensions that nyc should attempt to handle in addition to `.js` | `Array<String>` or `string` | `['.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx', '.vue', '.svelte]` |
| `requireEnv ` | Optional boolean to require the environment variable (defaults to VITE_COVERAGE) to equal true in order to instrument the code. Otherwise it will instrument even if env variable is not set. However if requireEnv is not set the instrumentation will stop if the environment variable is equal to false. | `boolean` | `-` |
| `requireEnv ` | Optional boolean to require the environment variable (defaults to VITE_COVERAGE) to equal true in order to instrument the code. Otherwise it will instrument even if env variable is not set. However if requireEnv is not set the instrumentation will stop if the environment variable is equal to false. | `boolean` | `-` |
| `cypress ` | Optional boolean to change the environment variable to CYPRESS_COVERAGE instead of VITE_COVERAGE. For ease of use with `@cypress/code-coverage` coverage | `boolean` | `-` |
| `checkProd ` | Optional boolean to enforce the plugin to skip instrumentation for production environments. Looks at Vite's isProduction key from the ResolvedConfig. | `boolean` | `-` |
| `forceBuildInstrument ` | Optional boolean to enforce the plugin to add instrumentation in build mode. | `boolean` | `false` |
| `nycrcPath ` | Path to specific nyc config to use instead of automatically searching for a nycconfig. This parameter is just passed down to @istanbuljs/load-nyc-config. | `string` | `-` |
> **Note:**
> **Note:**
> If you're using typescript, you can import the type for the options like so:
>
> ```ts
> import type { AddonOptionsVite } from '@storybook/addon-coverage'
> import type { AddonOptionsVite } from "@storybook/addon-coverage";
> ```
### Development scripts
Expand Down
11 changes: 0 additions & 11 deletions examples/babel/.storybook/main.js

This file was deleted.

3 changes: 3 additions & 0 deletions examples/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"storybook": "npx storybook dev -p 6006",
"build-storybook": "npx storybook build"
},
"resolutions": {
"jackspeak": "2.1.1"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions examples/webpack5/.nycrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"include": ["**/Button.tsx", "**/Page.tsx"]
}
15 changes: 15 additions & 0 deletions examples/webpack5/.storybook/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
stories: ["../stories/**/*.mdx", "../stories/**/*.stories.@(js|jsx|ts|tsx)"],
addons: [
"@storybook/addon-essentials",
"@storybook/addon-interactions",
"@storybook/addon-coverage",
],
framework: {
name: "@storybook/react-webpack5",
options: {},
},
docs: {
autodocs: true,
},
};
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"storybook": "npx storybook dev -p 6006",
"build-storybook": "npx storybook build"
},
"resolutions": {
"jackspeak": "2.1.1"
},
"dependencies": {
"@babel/core": "latest",
"@babel/preset-typescript": "latest",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit cced79d

Please sign in to comment.