Skip to content

Commit

Permalink
Merge pull request #22487 from 1234tgk/ts-migrate/addon-storyshots
Browse files Browse the repository at this point in the history
Migrate @storybook/addon-storyshots to strict TS
  • Loading branch information
kasperpeulen authored May 31, 2023
2 parents d17c035 + f6795ef commit 294fe71
Show file tree
Hide file tree
Showing 9 changed files with 12 additions and 11 deletions.
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

0 comments on commit 294fe71

Please sign in to comment.