Skip to content

Commit 12e869a

Browse files
authored
allow getSnapshot content by test name pattern (#95)
1 parent 710fd66 commit 12e869a

File tree

4 files changed

+43
-25
lines changed

4 files changed

+43
-25
lines changed

index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { CoverageMapData } from 'istanbul-lib-coverage';
1212
import ProjectWorkspace, {ProjectWorkspaceConfig, createProjectWorkspace, LoginShell } from './build/project_workspace';
1313
export {createProjectWorkspace, ProjectWorkspaceConfig, ProjectWorkspace, LoginShell};
1414
import {SourceLocation} from '@babel/types';
15+
import { SnapshotData } from 'jest-snapshot/build/types';
1516
export interface RunArgs {
1617
args: string[];
1718
replace?: boolean; // default is false
@@ -237,7 +238,7 @@ export class Snapshot {
237238
getMetadata(filepath: string, verbose?: boolean): SnapshotMetadata[];
238239
getMetadataAsync(filePath: string, verbose?: boolean): Promise<Array<SnapshotMetadata>>;
239240
parse(filePath: string, verbose?: boolean): SnapshotBlock[];
240-
getSnapshotContent(filePath: string, testFullName: string): Promise<string | undefined>;
241+
getSnapshotContent(filePath: string, name: string | RegExp): Promise<string | SnapshotData | undefined>;
241242
}
242243

243244
type FormattedTestResults = {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jest-editor-support",
3-
"version": "30.3.0",
3+
"version": "30.3.1",
44
"repository": {
55
"type": "git",
66
"url": "https://github.com/jest-community/jest-editor-support"

src/Snapshot.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

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

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

171166
const snapshotPath = snapshotResolver.resolveSnapshotPath(filePath);
172167
const snapshots = utils.getSnapshotData(snapshotPath, 'none').data;
173-
const name = autoPosition ? `${testFullName} 1` : testFullName;
174-
return snapshots[name];
168+
if (typeof name === 'string') {
169+
return snapshots[name];
170+
}
171+
const regex = name;
172+
const data: SnapshotData = {};
173+
Object.entries(snapshots).forEach(([key, value]) => {
174+
if (regex.test(key)) {
175+
data[key] = value;
176+
}
177+
});
178+
return Object.entries(data).length > 0 ? data : null;
175179
}
176180

177181
async getMetadataAsync(filePath: string, verbose: boolean = false): Promise<Array<SnapshotMetadata>> {

src/__tests__/snapshot.test.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,14 @@ describe('parse', () => {
151151
});
152152
describe('getSnapshotContent', () => {
153153
it.each`
154-
testName | expected
155-
${'regular inline test'} | ${undefined}
156-
${'test.each %s'} | ${undefined}
157-
${'test.each a'} | ${'a'}
158-
${'1 describe with each test.each a'} | ${'1.a'}
159-
${'2 describe with each test.each b'} | ${'2.b'}
160-
${'tests with each case %d test 1-D array each'} | ${undefined}
161-
${'tests with each case 3 test 1-D array each'} | ${'3 1-D'}
154+
testName | expected
155+
${'regular inline test 1'} | ${undefined}
156+
${'test.each %s 1'} | ${undefined}
157+
${'test.each a 1'} | ${'a'}
158+
${'1 describe with each test.each a 1'} | ${'1.a'}
159+
${'2 describe with each test.each b 1'} | ${'2.b'}
160+
${'tests with each case %d test 1-D array each 1'} | ${undefined}
161+
${'tests with each case 3 test 1-D array each 1'} | ${'3 1-D'}
162162
`('', async ({testName, expected}) => {
163163
const filePath = path.join(snapshotFixturePath, 'inline-and-each.example');
164164
const snapshot = new Snapshot(undefined, ['toMatchInlineSnapshot', 'toThrowErrorMatchingInlineSnapshot']);
@@ -168,9 +168,22 @@ describe('getSnapshotContent', () => {
168168
it('can take literal snapshot name', async () => {
169169
const filePath = path.join(snapshotFixturePath, 'inline-and-each.example');
170170
const snapshot = new Snapshot(undefined, ['toMatchInlineSnapshot', 'toThrowErrorMatchingInlineSnapshot']);
171-
let content = await snapshot.getSnapshotContent(filePath, `literal test 2`);
172-
expect(content).toBeUndefined();
173-
content = await snapshot.getSnapshotContent(filePath, `literal test 2`, false);
171+
const content = await snapshot.getSnapshotContent(filePath, `literal test 2`);
174172
expect(content).toEqual('literal test 2 content');
175173
});
174+
it('can take regex', async () => {
175+
const filePath = path.join(snapshotFixturePath, 'inline-and-each.example');
176+
const snapshot = new Snapshot(undefined, ['toMatchInlineSnapshot', 'toThrowErrorMatchingInlineSnapshot']);
177+
const content = await snapshot.getSnapshotContent(filePath, /literal test/);
178+
expect(content).toEqual({
179+
'literal test 1': 'literal test 1 content',
180+
'literal test 2': 'literal test 2 content',
181+
});
182+
});
183+
it('if nothing matched, returns null', async () => {
184+
const filePath = path.join(snapshotFixturePath, 'inline-and-each.example');
185+
const snapshot = new Snapshot(undefined, ['toMatchInlineSnapshot', 'toThrowErrorMatchingInlineSnapshot']);
186+
const content = await snapshot.getSnapshotContent(filePath, /not existing test/);
187+
expect(content).toEqual(null);
188+
});
176189
});

0 commit comments

Comments
 (0)