Skip to content

Commit

Permalink
fix(comparadise-utils): enforce rawName argument when test file conta…
Browse files Browse the repository at this point in the history
…ins multiple tests (#355)

Co-authored-by: danadajian <[email protected]>
  • Loading branch information
danadajian and danadajian authored Oct 31, 2023
1 parent 55e7d72 commit c1df75b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
27 changes: 27 additions & 0 deletions comparadise-utils/match-screenshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ function getTestFolderPathFromScripts(rawName?: string) {
);
}

const currentTestNumber = Cypress.mocha.getRunner().currentRunnable?.order;
if (
!rawName &&
typeof currentTestNumber === 'number' &&
currentTestNumber > 1
) {
throw new Error(
'❌ The rawName argument was not provided to matchScreenshot and is required for test files containing multiple tests!'
);
}

const testName = relativeTestPath.substring(
relativeTestPath.lastIndexOf('/') + 1,
relativeTestPath.lastIndexOf(SUFFIX_TEST_IDENTIFIER)
Expand Down Expand Up @@ -118,3 +129,19 @@ Cypress.Commands.add(
{ prevSubject: ['optional', 'element', 'window', 'document'] },
matchScreenshot
);

interface ExtendedCurrentRunnable extends Mocha.Runnable {
currentRunnable?: {
order?: unknown;
};
}

declare global {
namespace Cypress {
interface Cypress {
mocha: {
getRunner: () => ExtendedCurrentRunnable;
};
}
}
}
2 changes: 1 addition & 1 deletion docs/docs/setup/writing-visual-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('MyComponent visual test', () => {

By default, `matchScreenshot` will infer the name of your test from the name of your file and create a folder to save the base, new, and diff images to.

However, if you have multiple visual tests in a single file, you should pass a different `rawName` for each test to vary the paths where screenshots will be saved.
However, if you have multiple visual tests in a single file, you are required to provide a different `rawName` for each test to vary the paths where screenshots will be saved.

#### options - optional (Cypress.ScreenshotOptions)

Expand Down

0 comments on commit c1df75b

Please sign in to comment.