Last active
May 11, 2025 14:07
-
-
Save t3yamoto/761bee001fdcb50465a6f1492f569d3b to your computer and use it in GitHub Desktop.
AWS CDK (TypeScript) で Step Functions を構築する場合にスナップショットの差分が見づらい問題の対処
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module.exports = { | |
| testEnvironment: 'node', | |
| roots: ['<rootDir>/test'], | |
| testMatch: ['**/*.test.ts'], | |
| transform: { | |
| '^.+\\.tsx?$': 'ts-jest', | |
| }, | |
| // この設定を追加 | |
| snapshotSerializers: ['<rootDir>/path/to/sfn-asl-serializer.ts'], | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Plugin } from 'pretty-format'; | |
| const serializer: Plugin = { | |
| test: (val) => { | |
| return ( | |
| val !== null && | |
| typeof val === 'object' && | |
| !Array.isArray(val) && | |
| Object.prototype.hasOwnProperty.call(val, 'DefinitionString') && | |
| (typeof val.DefinitionString === 'string' || | |
| Object.prototype.hasOwnProperty.call(val.DefinitionString, 'Fn::Join')) | |
| ); | |
| }, | |
| serialize: (val, config, indentation, depth, refs, printer) => { | |
| let jsonString = val.DefinitionString; | |
| if (Object.prototype.hasOwnProperty.call(val.DefinitionString, 'Fn::Join')) { | |
| jsonString = convertToParsableJsonString(val.DefinitionString); | |
| } | |
| const newVal = { | |
| ...val, | |
| DefinitionString: JSON.parse(jsonString), | |
| }; | |
| return printer(newVal, config, indentation, depth, refs); | |
| }, | |
| }; | |
| const convertToParsableJsonString = (definitionString: { 'Fn::Join': [string, [string | object]] }) => { | |
| return definitionString['Fn::Join'][1] | |
| .map((v: unknown) => { | |
| if (typeof v === 'string') { | |
| return v; | |
| } else { | |
| try { | |
| return JSON.stringify(v).replace(/"/g, '\\"'); | |
| } catch { | |
| console.warn(`Could not stringify value ${v} in DefinitionString returning 'couldnotstringified' instead.`); | |
| return 'couldnotstringified'; | |
| } | |
| } | |
| }) | |
| .join(''); | |
| }; | |
| module.exports = serializer; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
JSON.parse は元の JSON 文字列のキー順序を保持しないので、スナップショットとして出力した ASL の定義順が元と異なる、という課題はあるかも