Skip to content

Commit

Permalink
Support to customize Markdown output snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
acuarica committed Jan 16, 2024
1 parent 299c238 commit 524f8cd
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions test/utils/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ export const maskTitle = (title: string) => title
.replace(/ /g, '-')
.replace(/[:^'()|]/g, '_');

Assertion.addMethod('matchSnapshot', function (ext: string, ctx: Mocha.Context) {
Assertion.addMethod('matchSnapshot', function (ext: string, ctx: Mocha.Context, titlePath?: string[]) {
const actual = this._obj;
if (typeof actual !== 'string') throw new TypeError('Actual value should be a string');
if (ctx.test === undefined) throw new TypeError('Mocha context is not defined');

const [root, ...titles] = ctx.test.titlePath();

const write = (line: string) => (output += line + '\n');
const writeSnapshot = () => {
write(tag);
Expand All @@ -26,6 +24,7 @@ Assertion.addMethod('matchSnapshot', function (ext: string, ctx: Mocha.Context)
ctx.test!.title += ` 📸 `;
};

const [root, ...titles] = titlePath ?? ctx.test.titlePath();
const name = maskTitle(titles.map(t => t
.replace(/^should /, '')
.replace(/ #[0-9a-f]{6}/, '')
Expand All @@ -35,6 +34,9 @@ Assertion.addMethod('matchSnapshot', function (ext: string, ctx: Mocha.Context)
const tag = '```' + `${ext} ${name}`;

const snapshotFile = `./test/__snapshots__/${maskTitle(root)}.snap.md`;
const dir = dirname(snapshotFile);
mkdirSync(dir, { recursive: true });

const content = existsSync(snapshotFile) ? readFileSync(snapshotFile, 'utf8') : `# ${root}\n`;
let marker: 'NOT_SEEN' | 'OPEN' | 'CLOSED' = 'NOT_SEEN';
let output = '';
Expand Down Expand Up @@ -88,7 +90,7 @@ declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Chai {
interface Assertion {
matchSnapshot(ext: string, ctx: Mocha.Context): Assertion;
matchSnapshot(ext: string, ctx: Mocha.Context, titlePath?: string[]): Assertion;
matchFile(path: string, ctx: Mocha.Context): Assertion;
}
}
Expand Down

0 comments on commit 524f8cd

Please sign in to comment.