Skip to content

allow getSnapshot content by test name pattern #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});