Skip to content
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

Migrate @storybook/addon-storyshots to strict TS #22487

Merged
merged 3 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions code/addons/storyshots-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"*.d.ts"
],
"scripts": {
"check": "../../../scripts/node_modules/.bin/tsc --noEmit",
"prep": "../../../scripts/prepare/tsc.ts"
},
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('getSnapshotFileName', () => {
const context = { fileName: 'foo.js', kind: 'kind' };

const result = target.getSnapshotFileName(context);
const platformAgnosticResult = result.replace(/\\|\//g, '/');
const platformAgnosticResult = result?.replace(/\\|\//g, '/');

// This is an absolute path, so we need to use `toContain()`
expect(platformAgnosticResult).toContain('__snapshots__/foo.storyshot');
Expand All @@ -17,7 +17,7 @@ describe('getSnapshotFileName', () => {
const context = { fileName: 'foo.web.stories.js', kind: 'kind' };

const result = target.getSnapshotFileName(context);
const platformAgnosticResult = result.replace(/\\|\//g, '/');
const platformAgnosticResult = result?.replace(/\\|\//g, '/');

// This is an absolute path, so we need to use `toContain()`
expect(platformAgnosticResult).toContain('__snapshots__/foo.web.stories.storyshot');
Expand All @@ -27,7 +27,7 @@ describe('getSnapshotFileName', () => {
const context = { fileName: 'test/foo.js', kind: 'kind' };

const result = target.getSnapshotFileName(context);
const platformAgnosticResult = result.replace(/\\|\//g, '/');
const platformAgnosticResult = result?.replace(/\\|\//g, '/');

// This is an absolute path, so we need to use `toContain()`
expect(platformAgnosticResult).toContain('test/__snapshots__/foo.storyshot');
Expand Down
2 changes: 1 addition & 1 deletion code/addons/storyshots-core/src/Stories2SnapsConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class Stories2SnapsConverter {
}
`
);
return null;
return undefined;
}

return this.getStoryshotFile(fileName);
Expand Down
2 changes: 1 addition & 1 deletion code/addons/storyshots-core/src/api/StoryshotsOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface TestMethodOptions {
renderTree: RenderTree;
renderShallowTree: RenderTree;
stories2snapsConverter: Stories2SnapsConverter;
snapshotFileName: string;
snapshotFileName?: string;
options: any;
done?: () => void;
}
Expand Down
4 changes: 2 additions & 2 deletions code/addons/storyshots-core/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function testStorySnapshots(options: StoryshotsOptions = {}) {
// subsequent calls to `it()` etc will all happen within this tick, which is required
// by Jest (cannot add tests asynchronously)
globalWindow.__STORYBOOK_STORY_STORE__.initializationPromise.then(() => {
const data = storybook.raw().reduce(
const data = storybook.raw()?.reduce(
(acc, item) => {
if (storyNameRegex && !item.name.match(storyNameRegex)) {
return acc;
Expand Down Expand Up @@ -86,7 +86,7 @@ function testStorySnapshots(options: StoryshotsOptions = {}) {
}[]
);

if (data.length) {
if (data && data.length) {
callTestMethodGlobals(testMethod);

snapshotsTests({
Expand Down
2 changes: 1 addition & 1 deletion code/addons/storyshots-core/src/frameworks/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function getConfigPathParts(input: string): Output {

return specifier;
});
output.requireContexts = output.stories.map((specifier) => {
output.requireContexts = output.stories?.map((specifier) => {
const { path: basePath, recursive, match } = toRequireContext(specifier);

// eslint-disable-next-line no-underscore-dangle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function getLoaders(): Loader[] {
return null;
})
.filter(Boolean)
.map((loader) => require(loader).default);
.map((loader) => loader && require(loader).default);
}

function loadFramework(options: StoryshotsOptions) {
Expand Down
2 changes: 1 addition & 1 deletion code/addons/storyshots-core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"jsx": "preserve",
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"strict": false
"strict": true
},
"include": ["src/**/*.ts"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const getApplication = ({
storyFnAngular: StoryFnAngularReturnType;
component?: any;
targetSelector: string;
analyzedMetadata: PropertyExtractor;
analyzedMetadata?: PropertyExtractor;
}) => {
const { props, styles, moduleMetadata = {} } = storyFnAngular;
let { template } = storyFnAngular;
Expand Down