diff --git a/index.d.ts b/index.d.ts index b33c26d..7031eb9 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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 @@ -237,7 +238,7 @@ export class Snapshot { getMetadata(filepath: string, verbose?: boolean): SnapshotMetadata[]; getMetadataAsync(filePath: string, verbose?: boolean): Promise>; parse(filePath: string, verbose?: boolean): SnapshotBlock[]; - getSnapshotContent(filePath: string, testFullName: string): Promise; + getSnapshotContent(filePath: string, name: string | RegExp): Promise; } type FormattedTestResults = { diff --git a/package.json b/package.json index 27808ea..7c40c74 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/Snapshot.js b/src/Snapshot.js index 0d581b4..19ddbf9 100644 --- a/src/Snapshot.js +++ b/src/Snapshot.js @@ -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'; @@ -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 { + async getSnapshotContent(filePath: string, name: string | RegExp): Promise { 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> { diff --git a/src/__tests__/snapshot.test.js b/src/__tests__/snapshot.test.js index 44c9f68..543b55a 100644 --- a/src/__tests__/snapshot.test.js +++ b/src/__tests__/snapshot.test.js @@ -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']); @@ -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); + }); });