Skip to content

Commit 104bed9

Browse files
fix: storybook flakiness and minor typescript issues (#5530)
* chore: moved fixture * chore: updated storybook test-runner * chore: fixed minor typescript errors * chore: package-lock was mismatching * chore: updated stories * Update .storybook/test-runner.ts Signed-off-by: Brian Muenzenmeyer <[email protected]> --------- Signed-off-by: Brian Muenzenmeyer <[email protected]> Co-authored-by: Brian Muenzenmeyer <[email protected]>
1 parent 61f729a commit 104bed9

File tree

10 files changed

+35
-23
lines changed

10 files changed

+35
-23
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ coverage
2323

2424
# Storybook
2525
storybook-static
26+
.nyc_output
2627

2728
# Vercel Files
2829
.vercel

.storybook/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { StorybookConfig } from '@storybook/nextjs';
21
import { resolve } from 'node:path';
2+
import type { StorybookConfig } from '@storybook/nextjs';
33

44
const config: StorybookConfig = {
55
stories: ['../components/**/*.stories.tsx'],
@@ -9,7 +9,7 @@ const config: StorybookConfig = {
99
docs: { autodocs: 'tag' },
1010
staticDirs: ['../public'],
1111
core: { disableTelemetry: true },
12-
webpackFinal: async (config, { configType }) => {
12+
webpackFinal: async config => {
1313
config.resolve!.modules = [resolve(__dirname, '..'), 'node_modules'];
1414

1515
config.resolve!.alias = {

.storybook/test-runner.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
import type { TestRunnerConfig } from '@storybook/test-runner';
22

3+
const STORYBOOK_ELEMENT_ID = '[data-test-id="story-root"]';
4+
35
const config: TestRunnerConfig = {
4-
postRender: async (page, context) => {
5-
// Gather the page HTML inner content for a DOM HTML Snapshot
6-
const rootElementId = '[data-test-id="story-root"]';
7-
const rootElement = await page.locator(rootElementId);
6+
postRender: async page => {
7+
// We wait for the page to load for at least one second
8+
// as there's no reliable way globally to ensure everything loaded correctly
9+
await page.waitForTimeout(1000);
10+
11+
// We attempt to get the Storybook root Element
12+
const rootElement = await page.locator(STORYBOOK_ELEMENT_ID);
13+
14+
// Then we rewrite the inner HTML content
815
const content = await rootElement.innerHTML();
916

1017
expect(content).toBeDefined();
18+
19+
// We strip the `class` tags from the HTML content as we do not want
20+
// to pollute the snapshots with generated class names
1121
expect(content.replace(/class="(.*?)"/gm, '')).toMatchSnapshot();
1222
},
1323
};

components/Article/AuthorList/Author/__snapshots__/index.stories.tsx.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ exports[`Article/AuthorList/Author Default smoke-test 1`] = `
1414
data-nimg="1"
1515
srcset="https://github.com/nodejs.png?size=60?w=64&amp;q=75 1x, https://github.com/nodejs.png?size=60?w=128&amp;q=75 2x"
1616
src="https://github.com/nodejs.png?size=60?w=128&amp;q=75"
17-
style="color: transparent; background-size: cover; background-position: 50% 50%; background-repeat: no-repeat; background-image: url(&quot;/static/images/placeholder-img.png&quot;);"
17+
style="color: transparent;"
1818
>
1919
</a>
2020
`;
@@ -31,9 +31,9 @@ exports[`Article/AuthorList/Author WithourUsername smoke-test 1`] = `
3131
height="0"
3232
decoding="async"
3333
data-nimg="1"
34-
srcset="https://github.com/.png?size=0?w=16&amp;q=75 1x"
35-
src="https://github.com/.png?size=0?w=16&amp;q=75"
36-
style="color: transparent; background-size: cover; background-position: 50% 50%; background-repeat: no-repeat; background-image: url(&quot;/static/images/placeholder-img.png&quot;);"
34+
srcset="/static/images/placeholder-img.png?w=16&amp;q=75 1x"
35+
src="http://127.0.0.1:6006/static/images/placeholder-img.png?w=16&amp;q=75"
36+
style
3737
>
3838
</a>
3939
`;

components/Article/AuthorList/__snapshots__/index.stories.tsx.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ exports[`Article/AuthorList Default smoke-test 1`] = `
1818
data-nimg="1"
1919
srcset="https://github.com/flaviocopes.png?size=60?w=64&amp;q=75 1x, https://github.com/flaviocopes.png?size=60?w=128&amp;q=75 2x"
2020
src="https://github.com/flaviocopes.png?size=60?w=128&amp;q=75"
21-
style="color: transparent; background-size: cover; background-position: 50% 50%; background-repeat: no-repeat; background-image: url(&quot;/static/images/placeholder-img.png&quot;);"
21+
style="color: transparent;"
2222
>
2323
</a>
2424
</li>
@@ -36,7 +36,7 @@ exports[`Article/AuthorList Default smoke-test 1`] = `
3636
data-nimg="1"
3737
srcset="https://github.com/MarkPieszak.png?size=60?w=64&amp;q=75 1x, https://github.com/MarkPieszak.png?size=60?w=128&amp;q=75 2x"
3838
src="https://github.com/MarkPieszak.png?size=60?w=128&amp;q=75"
39-
style="color: transparent; background-size: cover; background-position: 50% 50%; background-repeat: no-repeat; background-image: url(&quot;/static/images/placeholder-img.png&quot;);"
39+
style="color: transparent;"
4040
>
4141
</a>
4242
</li>
@@ -54,7 +54,7 @@ exports[`Article/AuthorList Default smoke-test 1`] = `
5454
data-nimg="1"
5555
srcset="https://github.com/mcollina.png?size=60?w=64&amp;q=75 1x, https://github.com/mcollina.png?size=60?w=128&amp;q=75 2x"
5656
src="https://github.com/mcollina.png?size=60?w=128&amp;q=75"
57-
style="color: transparent; background-size: cover; background-position: 50% 50%; background-repeat: no-repeat; background-image: url(&quot;/static/images/placeholder-img.png&quot;);"
57+
style="color: transparent;"
5858
>
5959
</a>
6060
</li>
@@ -70,9 +70,9 @@ exports[`Article/AuthorList Default smoke-test 1`] = `
7070
height="60"
7171
decoding="async"
7272
data-nimg="1"
73-
srcset="https://github.com/unavailable-author.png?size=60?w=64&amp;q=75 1x, https://github.com/unavailable-author.png?size=60?w=128&amp;q=75 2x"
74-
src="https://github.com/unavailable-author.png?size=60?w=128&amp;q=75"
75-
style="color: transparent; background-size: cover; background-position: 50% 50%; background-repeat: no-repeat; background-image: url(&quot;/static/images/placeholder-img.png&quot;);"
73+
srcset="/static/images/placeholder-img.png?w=64&amp;q=75 1x, /static/images/placeholder-img.png?w=128&amp;q=75 2x"
74+
src="http://127.0.0.1:6006/static/images/placeholder-img.png?w=128&amp;q=75"
75+
style
7676
>
7777
</a>
7878
</li>

components/Common/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

hooks/__tests__/useMediaQuery.test.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ describe('useMediaQuery', () => {
3939
});
4040

4141
it('should subscribe for media changes', () => {
42-
const listenerMock = jest.fn().mockImplementation((_, handler) => {
43-
handler();
44-
});
42+
const listenerMock = jest
43+
.fn()
44+
.mockImplementation((_: any, handler: Function) => {
45+
handler();
46+
});
4547

4648
Object.defineProperty(window, 'matchMedia', {
4749
writable: true,
@@ -58,7 +60,7 @@ describe('useMediaQuery', () => {
5860
});
5961

6062
it("should support MediaQueryList's old event listeners", () => {
61-
const listenerMock = jest.fn().mockImplementation(handler => {
63+
const listenerMock = jest.fn().mockImplementation((handler: Function) => {
6264
handler();
6365
});
6466

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"format": "npm run lint:fix && npm run prettier:fix",
3030
"storybook": "cross-env NODE_NO_WARNINGS=1 storybook dev -p 6006 --quiet",
3131
"storybook:build": "cross-env NODE_NO_WARNINGS=1 storybook build --quiet --loglevel warn",
32-
"test:unit": "cross-env NODE_NO_WARNINGS=1 jest --passWithNoTests",
32+
"test:unit": "cross-env NODE_NO_WARNINGS=1 jest --passWithNoTests --coverage",
3333
"test:unit:watch": "npm run test:unit -- --watch",
3434
"test:storybook": "concurrently -P -k -s first -n \"storybook,test-storybook\" -c \"magenta,blue\" \"npm:storybook -- --ci\" \"wait-on http://localhost:6006 && test-storybook {@}\"",
3535
"test:storybook:snapshot": "npm run test:storybook -- -- --updateSnapshot",

0 commit comments

Comments
 (0)