Skip to content

Commit

Permalink
Added console-error-checking
Browse files Browse the repository at this point in the history
  • Loading branch information
work933k committed Jun 21, 2023
1 parent 2aa338d commit d2b4090
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ Usage: test-storybook [options]
| `--junit` | Indicates that test information should be reported in a junit file. <br/>`test-storybook --**junit**` |
| `--ci` | Instead of the regular behavior of storing a new snapshot automatically, it will fail the test and require Jest to be run with `--updateSnapshot`. <br/>`test-storybook --ci` |
| `--shard [shardIndex/shardCount]` | Splits your test suite across different machines to run in CI. <br/>`test-storybook --shard=1/3` |
| `--checkConsole` | Checks the browser console output for errors<br/>`test-storybook --checkConsole` |

## Ejecting configuration

Expand Down
12 changes: 11 additions & 1 deletion src/setup-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const sanitizeURL = (url: string) => {

export const setupPage = async (page: Page, browserContext: BrowserContext) => {
const targetURL = process.env.TARGET_URL;
const checkConsole = process.env.TEST_CHECK_CONSOLE;

const viewMode = process.env.VIEW_MODE || 'story';
const renderedEvent = viewMode === 'docs' ? 'docsRendered' : 'storyRendered';
Expand Down Expand Up @@ -306,10 +307,14 @@ export const setupPage = async (page: Page, browserContext: BrowserContext) => {
// collect logs to show upon test error
let logs = [];
let hasErrors = false;
const spyOnConsole = (method, name) => {
const originalFn = console[method];
return function () {
if (\`${checkConsole}\`==='true' && method==='error') {
hasErrors = true;
}
const message = [...arguments].map(composeMessage).join(', ');
const prefix = \`\${bold(name)}: \`;
logs.push(prefix + message);
Expand All @@ -332,7 +337,12 @@ export const setupPage = async (page: Page, browserContext: BrowserContext) => {
})
return new Promise((resolve, reject) => {
channel.on('${renderedEvent}', () => resolve(document.getElementById('root')));
channel.on('${renderedEvent}', () => {
if (hasErrors) {
return reject(new StorybookTestRunnerError(storyId, 'Browser console errors', logs));
}
return resolve(document.getElementById('root'));
});
channel.on('storyUnchanged', () => resolve(document.getElementById('root')));
channel.on('storyErrored', ({ description }) => reject(
new StorybookTestRunnerError(storyId, description, logs))
Expand Down
4 changes: 4 additions & 0 deletions src/test-storybook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ const main = async () => {
const { storiesPaths, lazyCompilation } = getStorybookMetadata();
process.env.STORYBOOK_STORIES_PATTERN = storiesPaths;

if (runnerOptions.checkConsole) {
process.env.TEST_CHECK_CONSOLE = 'true';
}

if (lazyCompilation && isLocalStorybookIp) {
log(
`You're running Storybook with lazy compilation enabled, and will likely cause issues with the test runner locally. Consider disabling 'lazyCompilation' in ${runnerOptions.configDir}/main.js when running 'test-storybook' locally.`
Expand Down
9 changes: 9 additions & 0 deletions src/util/getCliOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,13 @@ describe('getCliOptions', () => {
const opts = getCliOptions();
expect(opts.runnerOptions).toMatchObject(customConfig);
});

it('returns checkConsole option if passed', () => {
const customConfig = { checkConsole: true };
jest
.spyOn(cliHelper, 'getParsedCliOptions')
.mockReturnValue({ options: customConfig, extraArgs: [] });
const opts = getCliOptions();
expect(opts.runnerOptions).toMatchObject(customConfig);
});
});
2 changes: 2 additions & 0 deletions src/util/getCliOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type CliOptions = {
coverage?: boolean;
junit?: boolean;
browsers?: BrowserType | BrowserType[];
checkConsole?: boolean;
};
jestOptions: JestOptions;
};
Expand All @@ -28,6 +29,7 @@ const STORYBOOK_RUNNER_COMMANDS: StorybookRunnerCommand[] = [
'url',
'coverage',
'junit',
'checkConsole',
];

function copyOption<ObjType extends object, KeyType extends keyof ObjType>(
Expand Down
3 changes: 2 additions & 1 deletion src/util/getParsedCliOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ export const getParsedCliOptions = (): ParsedCliOptions => {
.option(
'--shard <shardIndex/shardCount>',
'Splits your test suite across different machines to run in CI.'
);
)
.option('--checkConsole', 'Checks the browser console output for errors');

program.exitOverride();

Expand Down

0 comments on commit d2b4090

Please sign in to comment.