-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25958 from storybookjs/kasper/test-codemod
Codemod: Migrate to test package
- Loading branch information
Showing
11 changed files
with
231 additions
and
44 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
64 changes: 64 additions & 0 deletions
64
code/lib/cli/src/automigrate/fixes/remove-jest-testing-library.test.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,64 @@ | ||
import { expect, it } from 'vitest'; | ||
|
||
import type { StorybookConfig } from '@storybook/types'; | ||
import type { JsPackageManager } from '@storybook/core-common'; | ||
import { removeJestTestingLibrary } from './remove-jest-testing-library'; | ||
import ansiRegex from 'ansi-regex'; | ||
|
||
const check = async ({ | ||
packageManager, | ||
main: mainConfig = {}, | ||
storybookVersion = '8.0.0', | ||
}: { | ||
packageManager: Partial<JsPackageManager>; | ||
main?: Partial<StorybookConfig> & Record<string, unknown>; | ||
storybookVersion?: string; | ||
}) => { | ||
return removeJestTestingLibrary.check({ | ||
packageManager: packageManager as any, | ||
configDir: '', | ||
mainConfig: mainConfig as any, | ||
storybookVersion, | ||
}); | ||
}; | ||
|
||
it('should prompt to install the test package and run the codemod', async () => { | ||
const options = await check({ | ||
packageManager: { | ||
getAllDependencies: async () => ({ | ||
'@storybook/jest': '1.0.0', | ||
'@storybook/testing-library': '1.0.0', | ||
}), | ||
}, | ||
main: { addons: ['@storybook/essentials', '@storybook/addon-info'] }, | ||
}); | ||
|
||
await expect(options).toMatchInlineSnapshot(` | ||
{ | ||
"incompatiblePackages": [ | ||
"@storybook/jest", | ||
"@storybook/testing-library", | ||
], | ||
} | ||
`); | ||
|
||
expect.addSnapshotSerializer({ | ||
serialize: (value) => { | ||
const stringVal = typeof value === 'string' ? value : value.toString(); | ||
return stringVal.replace(ansiRegex(), ''); | ||
}, | ||
test: () => true, | ||
}); | ||
|
||
expect(await removeJestTestingLibrary.prompt(options!)).toMatchInlineSnapshot(` | ||
Attention: We've detected that you're using the following packages which are known to be incompatible with Storybook 8: | ||
- @storybook/jest | ||
- @storybook/testing-library | ||
Install the replacement for those packages: @storybook/test | ||
And run the following codemod: | ||
npx storybook migrate migrate-to-test-package --glob="**/*.stories.@(js|jsx|ts|tsx)" | ||
`); | ||
}); |
32 changes: 32 additions & 0 deletions
32
code/lib/cli/src/automigrate/fixes/remove-jest-testing-library.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,32 @@ | ||
import chalk from 'chalk'; | ||
import dedent from 'ts-dedent'; | ||
import type { Fix } from '../types'; | ||
|
||
export const removeJestTestingLibrary: Fix<{ incompatiblePackages: string[] }> = { | ||
id: 'remove-jest-testing-library', | ||
promptOnly: true, | ||
async check({ mainConfig, packageManager }) { | ||
const deps = await packageManager.getAllDependencies(); | ||
|
||
const incompatiblePackages = Object.keys(deps).filter( | ||
(it) => it === '@storybook/jest' || it === '@storybook/testing-library' | ||
); | ||
return incompatiblePackages.length ? { incompatiblePackages } : null; | ||
}, | ||
prompt({ incompatiblePackages }) { | ||
return dedent` | ||
${chalk.bold( | ||
'Attention' | ||
)}: We've detected that you're using the following packages which are known to be incompatible with Storybook 8: | ||
${incompatiblePackages.map((name) => `- ${chalk.cyan(`${name}`)}`).join('\n')} | ||
Install the replacement for those packages: ${chalk.cyan('@storybook/test')} | ||
And run the following codemod: | ||
${chalk.cyan( | ||
'npx storybook migrate migrate-to-test-package --glob="**/*.stories.@(js|jsx|ts|tsx)"' | ||
)} | ||
`; | ||
}, | ||
}; |
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
46 changes: 46 additions & 0 deletions
46
code/lib/codemod/src/transforms/__tests__/migrate-to-test-package.test.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,46 @@ | ||
import { expect, test } from 'vitest'; | ||
import transform from '../migrate-to-test-package'; | ||
import dedent from 'ts-dedent'; | ||
|
||
expect.addSnapshotSerializer({ | ||
serialize: (val: any) => (typeof val === 'string' ? val : val.toString()), | ||
test: () => true, | ||
}); | ||
|
||
const tsTransform = async (source: string) => | ||
(await transform({ source, path: 'Component.stories.tsx' })).trim(); | ||
|
||
test('replace jest and testing-library with the test package', async () => { | ||
const input = dedent` | ||
import { expect } from '@storybook/jest'; | ||
import { within, userEvent } from '@storybook/testing-library'; | ||
`; | ||
|
||
expect(await tsTransform(input)).toMatchInlineSnapshot(` | ||
import { expect } from '@storybook/test'; | ||
import { within, userEvent } from '@storybook/test'; | ||
`); | ||
}); | ||
|
||
test('Make jest imports namespace imports', async () => { | ||
const input = dedent` | ||
import { expect, jest } from '@storybook/jest'; | ||
import { within, userEvent } from '@storybook/testing-library'; | ||
const onFocusMock = jest.fn(); | ||
const onSearchMock = jest.fn(); | ||
jest.spyOn(window, 'Something'); | ||
`; | ||
|
||
expect(await tsTransform(input)).toMatchInlineSnapshot(` | ||
import { expect } from '@storybook/test'; | ||
import * as test from '@storybook/test'; | ||
import { within, userEvent } from '@storybook/test'; | ||
const onFocusMock = test.fn(); | ||
const onSearchMock = test.fn(); | ||
test.spyOn(window, 'Something'); | ||
`); | ||
}); |
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
63 changes: 63 additions & 0 deletions
63
code/lib/codemod/src/transforms/migrate-to-test-package.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,63 @@ | ||
/* eslint-disable no-underscore-dangle */ | ||
import type { FileInfo } from 'jscodeshift'; | ||
import { loadCsf, printCsf } from '@storybook/csf-tools'; | ||
import type { BabelFile } from '@babel/core'; | ||
import * as babel from '@babel/core'; | ||
import * as t from '@babel/types'; | ||
import prettier from 'prettier'; | ||
|
||
export default async function transform(info: FileInfo) { | ||
const csf = loadCsf(info.source, { makeTitle: (title) => title }); | ||
const fileNode = csf._ast; | ||
// @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: info.path }, | ||
{ code: info.source, ast: fileNode } | ||
); | ||
|
||
file.path.traverse({ | ||
ImportDeclaration: (path) => { | ||
if ( | ||
path.node.source.value === '@storybook/jest' || | ||
path.node.source.value === '@storybook/testing-library' | ||
) { | ||
if (path.node.source.value === '@storybook/jest') { | ||
path.get('specifiers').forEach((specifier) => { | ||
if (specifier.isImportSpecifier()) { | ||
const imported = specifier.get('imported'); | ||
if (!imported.isIdentifier()) return; | ||
if (imported.node.name === 'jest') { | ||
specifier.remove(); | ||
path.insertAfter( | ||
t.importDeclaration( | ||
[t.importNamespaceSpecifier(t.identifier('test'))], | ||
t.stringLiteral('@storybook/test') | ||
) | ||
); | ||
} | ||
} | ||
}); | ||
} | ||
path.get('source').replaceWith(t.stringLiteral('@storybook/test')); | ||
} | ||
}, | ||
Identifier: (path) => { | ||
if (path.node.name === 'jest') { | ||
path.replaceWith(t.identifier('test')); | ||
} | ||
}, | ||
}); | ||
|
||
let output = printCsf(csf).code; | ||
try { | ||
output = await prettier.format(output, { | ||
...(await prettier.resolveConfig(info.path)), | ||
filepath: info.path, | ||
}); | ||
} catch (e) { | ||
console.warn(`Failed applying prettier to ${info.path}.`); | ||
} | ||
return output; | ||
} | ||
|
||
export const parser = 'tsx'; |
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