Skip to content

Commit

Permalink
throw an error when hooks are defined twice
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbf committed Nov 17, 2023
1 parent 6db764f commit e6df920
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
4 changes: 2 additions & 2 deletions playwright/jest-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ if (testRunnerConfig) {
testRunnerConfig.setup();
}

const preVisitFn = testRunnerConfig.preRender || testRunnerConfig.preVisit;
const preVisitFn = testRunnerConfig.preVisit || testRunnerConfig.preRender;
if (preVisitFn) {
setPreVisit(preVisitFn);
}

const postVisitFn = testRunnerConfig.postRender || testRunnerConfig.postVisit;
const postVisitFn = testRunnerConfig.postVisit || testRunnerConfig.postRender;
if (postVisitFn) {
setPostVisit(postVisitFn);
}
Expand Down
21 changes: 17 additions & 4 deletions src/test-storybook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ process.on('unhandledRejection', (err) => {
});

const log = (message: string) => console.log(`[test-storybook] ${message}`);
const warn = (message: string) => console.warn('\x1b[33m%s\x1b[0m', `[test-storybook] ${message}`);
const error = (err: { message: any; stack: any }) => {
if (err instanceof Error) {
console.error(`\x1b[31m[test-storybook]\x1b[0m ${err.message} \n\n${err.stack}`);
Expand Down Expand Up @@ -275,12 +276,12 @@ function ejectConfiguration() {
log('Configuration file successfully copied as test-runner-jest.config.js');
}

function warnOnce(msg: string) {
function warnOnce(message: string) {
let warned = false;
return () => {
if (!warned) {
// here we specify the ansi code for yellow as jest is stripping the default color from console.warn
console.warn('\x1b[33m%s\x1b[0m', msg);
warn(message);
warned = true;
}
};
Expand All @@ -298,15 +299,27 @@ const main = async () => {

const testRunnerConfig = getTestRunnerConfig(runnerOptions.configDir) || ({} as TestRunnerConfig);

if (testRunnerConfig.preVisit && testRunnerConfig.preRender) {
throw new Error(
'You cannot use both preVisit and preRender hooks in your test-runner config file. Please use preVisit instead.'
);
}

if (testRunnerConfig.postVisit && testRunnerConfig.postRender) {
throw new Error(
'You cannot use both postVisit and postRender hooks in your test-runner config file. Please use postVisit instead.'
);
}

// TODO: remove preRender and postRender hooks likely in 0.20.0
if (testRunnerConfig.preRender) {
warnOnce(
'[Test-runner] The "preRender" hook is deprecated and will be removed in later versions. Please use "preVisit" instead in your test-runner config file.'
'The "preRender" hook is deprecated and will be removed in later versions. Please use "preVisit" instead in your test-runner config file.'
)();
}
if (testRunnerConfig.postRender) {
warnOnce(
'[Test-runner] The "postRender" hook is deprecated and will be removed in later versions. Please use "postVisit" instead in your test-runner config file.'
'The "postRender" hook is deprecated and will be removed in later versions. Please use "postVisit" instead in your test-runner config file.'
)();
}

Expand Down

0 comments on commit e6df920

Please sign in to comment.