-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Warn when argTypesRegex is used together with the visual test addon
- Loading branch information
1 parent
21ed4a3
commit 952dba5
Showing
5 changed files
with
147 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
code/lib/core-server/src/utils/warnWhenUsingArgTypesRegex.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import type { PackageJson, StorybookConfig } from '@storybook/types'; | ||
import { getConfigInfo } from '@storybook/core-common'; | ||
import { readFile } from 'fs-extra'; | ||
import * as babel from '@babel/core'; | ||
import type { BabelFile } from '@babel/core'; | ||
import { babelParse } from '@storybook/csf-tools'; | ||
import dedent from 'ts-dedent'; | ||
import chalk from 'chalk'; | ||
|
||
export async function warnWhenUsingArgTypesRegex( | ||
packageJson: PackageJson, | ||
configDir: string, | ||
config: StorybookConfig | ||
) { | ||
const { previewConfig } = getConfigInfo(packageJson, configDir); | ||
const previewContent = previewConfig ? await readFile(previewConfig, 'utf8') : ''; | ||
|
||
const hasVisualTestAddon = | ||
config?.addons?.some((it) => | ||
typeof it === 'string' | ||
? it === '@chromatic-com/storybook' | ||
: it.name === '@chromatic-com/storybook' | ||
) ?? false; | ||
|
||
if (hasVisualTestAddon && previewConfig && previewContent.includes('argTypesRegex')) { | ||
// @ts-expect-error File is not yet exposed, see https://github.com/babel/babel/issues/11350#issuecomment-644118606 | ||
const file: BabelFile = new babel.File( | ||
{ filename: previewConfig }, | ||
{ code: previewContent, ast: babelParse(previewContent) } | ||
); | ||
|
||
file.path.traverse({ | ||
Identifier: (path) => { | ||
if (path.node.name === 'argTypesRegex') { | ||
const message = dedent` | ||
${chalk.bold('Attention')}: We've detected that you're using ${chalk.cyan( | ||
'actions.argTypesRegex' | ||
)} together with the visual test addon: | ||
${path.buildCodeFrameError(previewConfig).message} | ||
We recommend removing the ${chalk.cyan( | ||
'argTypesRegex' | ||
)} and assigning explicit action with the ${chalk.cyan( | ||
'fn' | ||
)} function from ${chalk.cyan('@storybook/test')} instead. | ||
The build used by the addon for snapshot testing doesn't take the regex into account, which can cause hard to debug problems when a snapshot depends on the presence of action props. | ||
`; | ||
console.warn(message); | ||
} | ||
}, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters