Skip to content

Commit

Permalink
allow getSnapshot content by test name pattern (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
connectdotz committed Nov 20, 2022
1 parent 710fd66 commit 12e869a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { CoverageMapData } from 'istanbul-lib-coverage';
import ProjectWorkspace, {ProjectWorkspaceConfig, createProjectWorkspace, LoginShell } from './build/project_workspace';
export {createProjectWorkspace, ProjectWorkspaceConfig, ProjectWorkspace, LoginShell};
import {SourceLocation} from '@babel/types';
import { SnapshotData } from 'jest-snapshot/build/types';
export interface RunArgs {
args: string[];
replace?: boolean; // default is false
Expand Down Expand Up @@ -237,7 +238,7 @@ export class Snapshot {
getMetadata(filepath: string, verbose?: boolean): SnapshotMetadata[];
getMetadataAsync(filePath: string, verbose?: boolean): Promise<Array<SnapshotMetadata>>;
parse(filePath: string, verbose?: boolean): SnapshotBlock[];
getSnapshotContent(filePath: string, testFullName: string): Promise<string | undefined>;
getSnapshotContent(filePath: string, name: string | RegExp): Promise<string | SnapshotData | undefined>;
}

type FormattedTestResults = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jest-editor-support",
"version": "30.3.0",
"version": "30.3.1",
"repository": {
"type": "git",
"url": "https://github.com/jest-community/jest-editor-support"
Expand Down
28 changes: 16 additions & 12 deletions src/Snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

import traverse from '@babel/traverse';
import {buildSnapshotResolver, SnapshotResolver, utils} from 'jest-snapshot';
import {buildSnapshotResolver, SnapshotResolver, utils, SnapshotData} from 'jest-snapshot';
import type {ProjectConfig} from '../types/Config';

import {getASTfor} from './parsers/babel_parser';
Expand Down Expand Up @@ -155,23 +155,27 @@ export default class Snapshot {
/**
* look for snapshot content for the given test.
* @param {*} filePath
* @param {*} testFullName
* @param autoPosition if true (the default), it will append position ("1") to the testFullName,
* otherwise, the testFullName should include the position in it.
* @returns the content of the snapshot, if exist. otherwise undefined.
* @param {*} name can be a literal string or a regex pattern.
* @returns the content of the snapshot, if exist. If name is a string, a string will be returned. If name is a RegExp,
* a SnapshotData object will be returned with all matched snapshots. If nothing matched, null will be returned.
* @throws throws exception if the snapshot version mismatched or any other unexpected error.
*/
async getSnapshotContent(
filePath: string,
testFullName: string,
autoPosition: boolean = true
): Promise<string | null> {
async getSnapshotContent(filePath: string, name: string | RegExp): Promise<string | SnapshotData | null> {
const snapshotResolver = await this._getSnapshotResolver();

const snapshotPath = snapshotResolver.resolveSnapshotPath(filePath);
const snapshots = utils.getSnapshotData(snapshotPath, 'none').data;
const name = autoPosition ? `${testFullName} 1` : testFullName;
return snapshots[name];
if (typeof name === 'string') {
return snapshots[name];
}
const regex = name;
const data: SnapshotData = {};
Object.entries(snapshots).forEach(([key, value]) => {
if (regex.test(key)) {
data[key] = value;
}
});
return Object.entries(data).length > 0 ? data : null;
}

async getMetadataAsync(filePath: string, verbose: boolean = false): Promise<Array<SnapshotMetadata>> {
Expand Down
35 changes: 24 additions & 11 deletions src/__tests__/snapshot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,14 @@ describe('parse', () => {
});
describe('getSnapshotContent', () => {
it.each`
testName | expected
${'regular inline test'} | ${undefined}
${'test.each %s'} | ${undefined}
${'test.each a'} | ${'a'}
${'1 describe with each test.each a'} | ${'1.a'}
${'2 describe with each test.each b'} | ${'2.b'}
${'tests with each case %d test 1-D array each'} | ${undefined}
${'tests with each case 3 test 1-D array each'} | ${'3 1-D'}
testName | expected
${'regular inline test 1'} | ${undefined}
${'test.each %s 1'} | ${undefined}
${'test.each a 1'} | ${'a'}
${'1 describe with each test.each a 1'} | ${'1.a'}
${'2 describe with each test.each b 1'} | ${'2.b'}
${'tests with each case %d test 1-D array each 1'} | ${undefined}
${'tests with each case 3 test 1-D array each 1'} | ${'3 1-D'}
`('', async ({testName, expected}) => {
const filePath = path.join(snapshotFixturePath, 'inline-and-each.example');
const snapshot = new Snapshot(undefined, ['toMatchInlineSnapshot', 'toThrowErrorMatchingInlineSnapshot']);
Expand All @@ -168,9 +168,22 @@ describe('getSnapshotContent', () => {
it('can take literal snapshot name', async () => {
const filePath = path.join(snapshotFixturePath, 'inline-and-each.example');
const snapshot = new Snapshot(undefined, ['toMatchInlineSnapshot', 'toThrowErrorMatchingInlineSnapshot']);
let content = await snapshot.getSnapshotContent(filePath, `literal test 2`);
expect(content).toBeUndefined();
content = await snapshot.getSnapshotContent(filePath, `literal test 2`, false);
const content = await snapshot.getSnapshotContent(filePath, `literal test 2`);
expect(content).toEqual('literal test 2 content');
});
it('can take regex', async () => {
const filePath = path.join(snapshotFixturePath, 'inline-and-each.example');
const snapshot = new Snapshot(undefined, ['toMatchInlineSnapshot', 'toThrowErrorMatchingInlineSnapshot']);
const content = await snapshot.getSnapshotContent(filePath, /literal test/);
expect(content).toEqual({
'literal test 1': 'literal test 1 content',
'literal test 2': 'literal test 2 content',
});
});
it('if nothing matched, returns null', async () => {
const filePath = path.join(snapshotFixturePath, 'inline-and-each.example');
const snapshot = new Snapshot(undefined, ['toMatchInlineSnapshot', 'toThrowErrorMatchingInlineSnapshot']);
const content = await snapshot.getSnapshotContent(filePath, /not existing test/);
expect(content).toEqual(null);
});
});

0 comments on commit 12e869a

Please sign in to comment.