MCPcopy
hub / github.com/jestjs/jest / getSnapshotData

Function getSnapshotData

packages/jest-snapshot-utils/src/utils.ts:133–165  ·  view source on GitHub ↗
(
  snapshotPath: string,
  update: Config.SnapshotUpdateState,
)

Source from the content-addressed store, hash-verified

131};
132
133export const getSnapshotData = (
134 snapshotPath: string,
135 update: Config.SnapshotUpdateState,
136): {
137 data: SnapshotData;
138 dirty: boolean;
139} => {
140 const data = Object.create(null);
141 let snapshotContents = '';
142 let dirty = false;
143
144 if (fs.existsSync(snapshotPath)) {
145 try {
146 snapshotContents = fs.readFileSync(snapshotPath, 'utf8');
147 // eslint-disable-next-line no-new-func
148 const populate = new Function('exports', snapshotContents);
149 populate(data);
150 } catch {}
151 }
152
153 const validationResult = validateSnapshotHeader(snapshotContents);
154 const isInvalid = snapshotContents && validationResult;
155
156 if (update === 'none' && isInvalid) {
157 throw validationResult;
158 }
159
160 if ((update === 'all' || update === 'new') && isInvalid) {
161 dirty = true;
162 }
163
164 return {data, dirty};
165};
166
167export const escapeBacktickString = (str: string): string =>
168 str.replaceAll(/`|\\|\${/g, '\\$&');

Callers 2

utils.test.tsFile · 0.90
constructorMethod · 0.90

Calls 2

validateSnapshotHeaderFunction · 0.85
createMethod · 0.45

Tested by

no test coverage detected