-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CLI: Add "missing-storybook-dependencies" automigration #28579
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b8209a0
add "missing-storybook-dependencies" automigration
yannbf 780f1b9
second pass, use shallow depth in checks
yannbf 24215f0
update error message
yannbf 23ddfff
fix vitest imports
yannbf 08603e8
add specific version ranges
yannbf 9e923f0
Merge branch 'next' into yann/missing-storybook-deps-automigration
ndelangen c9ea814
fix ts error
ndelangen c269e84
fix tests
ndelangen 8467592
create consistent reporting (seems there sort ordering race condition)
ndelangen 939eedc
Merge branch 'next' into yann/missing-storybook-deps-automigration
ndelangen 68d17f1
chalk not putting in colors in CI causing snapshot differences
ndelangen e0a8106
Merge branch 'next' into yann/missing-storybook-deps-automigration
ndelangen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
123 changes: 123 additions & 0 deletions
123
code/lib/cli/src/automigrate/fixes/missing-storybook-dependencies.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,123 @@ | ||
import { describe, expect, vi, it, beforeEach } from 'vitest'; | ||
import type { JsPackageManager } from '@storybook/core/common'; | ||
import stripAnsi from 'strip-ansi'; | ||
|
||
import { missingStorybookDependencies } from './missing-storybook-dependencies'; | ||
|
||
vi.mock('globby', () => ({ | ||
__esModule: true, | ||
globby: vi.fn().mockResolvedValue(['.storybook/manager.ts', 'path/to/file.stories.tsx']), | ||
})); | ||
|
||
vi.mock('node:fs/promises', () => ({ | ||
__esModule: true, | ||
readFile: vi.fn().mockResolvedValue(` | ||
// these are NOT installed, will be reported | ||
import { someFunction } from '@storybook/preview-api'; | ||
import { anotherFunction } from '@storybook/manager-api'; | ||
import { SomeError } from '@storybook/core-events/server-errors'; | ||
// this IS installed, will not be reported | ||
import { yetAnotherFunction } from '@storybook/theming'; | ||
`), | ||
})); | ||
|
||
vi.mock('../../helpers', () => ({ | ||
getStorybookVersionSpecifier: vi.fn().mockReturnValue('^8.1.10'), | ||
})); | ||
|
||
const check = async ({ | ||
packageManager, | ||
storybookVersion = '8.1.10', | ||
}: { | ||
packageManager: JsPackageManager; | ||
storybookVersion?: string; | ||
}) => { | ||
return missingStorybookDependencies.check({ | ||
packageManager, | ||
mainConfig: {} as any, | ||
storybookVersion, | ||
}); | ||
}; | ||
|
||
describe('missingStorybookDependencies', () => { | ||
const mockPackageManager = { | ||
findInstallations: vi.fn().mockResolvedValue({ | ||
dependencies: { | ||
'@storybook/react': '8.1.0', | ||
'@storybook/theming': '8.1.0', | ||
}, | ||
}), | ||
retrievePackageJson: vi.fn().mockResolvedValue({ | ||
dependencies: { | ||
'@storybook/core': '8.1.0', | ||
}, | ||
}), | ||
addDependencies: vi.fn().mockResolvedValue(undefined), | ||
} as Partial<JsPackageManager>; | ||
|
||
describe('check function', () => { | ||
it('should identify missing dependencies', async () => { | ||
const result = await check({ | ||
packageManager: mockPackageManager as JsPackageManager, | ||
}); | ||
|
||
expect(Object.keys(result!.packageUsage)).not.includes('@storybook/theming'); | ||
yannbf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expect(result).toEqual({ | ||
packageUsage: { | ||
'@storybook/preview-api': ['.storybook/manager.ts', 'path/to/file.stories.tsx'], | ||
'@storybook/manager-api': ['.storybook/manager.ts', 'path/to/file.stories.tsx'], | ||
'@storybook/core-events': ['.storybook/manager.ts', 'path/to/file.stories.tsx'], | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('prompt function', () => { | ||
it('should provide a proper message with the missing dependencies', () => { | ||
const packageUsage = { | ||
'@storybook/preview-api': ['.storybook/manager.ts'], | ||
'@storybook/manager-api': ['path/to/file.stories.tsx'], | ||
}; | ||
|
||
const message = missingStorybookDependencies.prompt({ packageUsage }); | ||
|
||
expect(stripAnsi(message)).toMatchInlineSnapshot(` | ||
"Found the following Storybook packages used in your project, but they are missing from your project dependencies: | ||
- @storybook/manager-api: (1 file) | ||
- @storybook/preview-api: (1 file) | ||
|
||
Referencing missing packages can cause your project to crash. We can automatically add them to your dependencies. | ||
|
||
More info: https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#failed-to-resolve-import-storybookx-error" | ||
`); | ||
}); | ||
}); | ||
|
||
describe('run function', () => { | ||
it('should add missing dependencies', async () => { | ||
const dryRun = false; | ||
const packageUsage = { | ||
'@storybook/preview-api': ['.storybook/manager.ts'], | ||
'@storybook/manager-api': ['path/to/file.stories.tsx'], | ||
}; | ||
|
||
await missingStorybookDependencies.run!({ | ||
result: { packageUsage }, | ||
dryRun, | ||
packageManager: mockPackageManager as JsPackageManager, | ||
mainConfigPath: 'path/to/main-config.js', | ||
}); | ||
|
||
expect(mockPackageManager.addDependencies).toHaveBeenNthCalledWith( | ||
1, | ||
{ installAsDevDependencies: true }, | ||
['@storybook/preview-api@8.1.10', '@storybook/manager-api@8.1.10'] | ||
); | ||
expect(mockPackageManager.addDependencies).toHaveBeenNthCalledWith( | ||
2, | ||
{ installAsDevDependencies: true, skipInstall: true, packageJson: expect.anything() }, | ||
['@storybook/preview-api@^8.1.10', '@storybook/manager-api@^8.1.10'] | ||
); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea of adding a depth option is because we want to try and get information over dependencies that are coming directly from package.json, but NOT as dependencies of dependencies. This needs further testing on all package managers