( snapshots: Array<InlineSnapshot>, ast: File | Program, snapshotMatcherNames: Array<string>, )
| 373 | export const groupSnapshotsByFile = groupSnapshotsBy(({frame: {file}}) => file); |
| 374 | |
| 375 | const traverseAst = ( |
| 376 | snapshots: Array<InlineSnapshot>, |
| 377 | ast: File | Program, |
| 378 | snapshotMatcherNames: Array<string>, |
| 379 | ) => { |
| 380 | const groupedSnapshots = groupSnapshotsByFrame(snapshots); |
| 381 | const remainingSnapshots = new Set(snapshots.map(({snapshot}) => snapshot)); |
| 382 | |
| 383 | traverseFast(ast, (node: Node) => { |
| 384 | if (node.type !== 'CallExpression') return; |
| 385 | |
| 386 | const {arguments: args, callee} = node; |
| 387 | if ( |
| 388 | callee.type !== 'MemberExpression' || |
| 389 | callee.property.type !== 'Identifier' || |
| 390 | callee.property.loc == null |
| 391 | ) { |
| 392 | return; |
| 393 | } |
| 394 | const {line, column} = callee.property.loc.start; |
| 395 | const snapshotsForFrame = groupedSnapshots[`${line}:${column}`]; |
| 396 | if (!snapshotsForFrame) { |
| 397 | return; |
| 398 | } |
| 399 | if (snapshotsForFrame.length > 1) { |
| 400 | throw new Error( |
| 401 | 'Jest: Multiple inline snapshots for the same call are not supported.', |
| 402 | ); |
| 403 | } |
| 404 | const inlineSnapshot = snapshotsForFrame[0]; |
| 405 | inlineSnapshot.node = node; |
| 406 | |
| 407 | snapshotMatcherNames.push(callee.property.name); |
| 408 | |
| 409 | const snapshotIndex = args.findIndex( |
| 410 | ({type}) => type === 'TemplateLiteral' || type === 'StringLiteral', |
| 411 | ); |
| 412 | |
| 413 | const {snapshot} = inlineSnapshot; |
| 414 | remainingSnapshots.delete(snapshot); |
| 415 | const replacementNode = templateLiteral( |
| 416 | [templateElement({raw: escapeBacktickString(snapshot)})], |
| 417 | [], |
| 418 | ); |
| 419 | |
| 420 | if (snapshotIndex === -1) { |
| 421 | args.push(replacementNode); |
| 422 | } else { |
| 423 | args[snapshotIndex] = replacementNode; |
| 424 | } |
| 425 | }); |
| 426 | |
| 427 | if (remainingSnapshots.size > 0) { |
| 428 | throw new Error("Jest: Couldn't locate all inline snapshots."); |
| 429 | } |
| 430 | }; |
no test coverage detected