diff --git a/amplify-migration-apps/README.md b/amplify-migration-apps/README.md index 7c920739c6c..60b3126bc42 100644 --- a/amplify-migration-apps/README.md +++ b/amplify-migration-apps/README.md @@ -283,7 +283,7 @@ are needed, and adding a new app automatically gets correct mocks without extra ### Adding a Snapshot Test | `generate` To add a snapshot test that validates the `gen2-migration generate` command for a new app, -add a new test to [`command-handlers.test.ts`](../packages/amplify-cli/src/__tests__/commands/gen2-migration/generate/codegen-head/command-handlers.test.ts): +add a new test to [`generate.test.ts`](../packages/amplify-cli/src/__tests__/commands/gen2-migration/generate.test.ts): ```typescript // For apps WITH Amplify Hosting: @@ -312,8 +312,8 @@ test(' snapshot', async () => { ```console cd packages/amplify-cli -npx jest --no-coverage src/__tests__/commands/gen2-migration/generate/codegen-head/command-handlers.test.ts -t ' snapshot' -npx jest --no-coverage src/__tests__/commands/gen2-migration/refactor/refactor.test.ts -t ' snapshot' +npx jest --no-coverage src/__tests__/commands/gen2-migration/generate.test.ts -t ' snapshot' +npx jest --no-coverage src/__tests__/commands/gen2-migration/refactor.test.ts -t ' snapshot' ``` The tests should pass with no differences. They could fail for two reasons: @@ -333,14 +333,20 @@ When the migration tool code changes and you need to update expected snapshots: ```console cd packages/amplify-cli -npx jest --no-coverage src/__tests__/commands/gen2-migration/generate/codegen-head/command-handlers.test.ts --updateSnapshot +npx jest --no-coverage src/__tests__/commands/gen2-migration/generate.test.ts --updateSnapshot ``` This updates all snapshots. You can also target a specific app: ```console -npx jest --no-coverage src/__tests__/commands/gen2-migration/generate/codegen-head/command-handlers.test.ts \ - -t ' snapshot' --updateSnapshot +npx jest --no-coverage src/__tests__/commands/gen2-migration/generate.test.ts -t ' snapshot' --updateSnapshot ``` Always review the diff after updating to make sure the changes are intentional. + + +> [!NOTE] +> When updating snapshots, the first run with `--updateSnapshot` will still report a failure +> because it detects the diff before writing the updated files. Run the tests a second time +> (without `--updateSnapshot`) to verify the snapshots are now correct. + diff --git a/packages/amplify-cli/src/__tests__/commands/gen2-migration/_framework/snapshot.ts b/packages/amplify-cli/src/__tests__/commands/gen2-migration/_framework/snapshot.ts index cc2c4065046..c724191bf95 100644 --- a/packages/amplify-cli/src/__tests__/commands/gen2-migration/_framework/snapshot.ts +++ b/packages/amplify-cli/src/__tests__/commands/gen2-migration/_framework/snapshot.ts @@ -81,14 +81,6 @@ export class Snapshot { public async compare(actualDir: string, ignorePatterns?: RegExp[]): Promise { const fullIgnorePatterns = [...(ignorePatterns ?? []), /node_modules/]; const differences = await diff({ expectedDir: this.props.expectedPath, actualDir, ignorePatterns: fullIgnorePatterns }); - - // copy the temporary actual path to repo (ignored) for easy manual comparison - const ignoredActualPath = `${this.props.expectedPath}.actual.${Date.now()}`; - if (fs.existsSync(ignoredActualPath)) { - fs.rmSync(ignoredActualPath, { recursive: true, force: true }); - } - copySync({ src: actualDir, dest: ignoredActualPath, ignorePatterns: fullIgnorePatterns }); - return new Report({ app: this.props.app, expectedPath: this.props.expectedPath, @@ -109,6 +101,7 @@ export class Snapshot { public update(actualDir: string) { console.log(`Updating snapshot: ${this.props.expectedPath}`); fs.rmSync(this.props.expectedPath, { recursive: true }); - copySync({ src: actualDir, dest: this.props.expectedPath }); + copySync({ src: actualDir, dest: this.props.expectedPath, ignorePatterns: [/node_modules/] }); + console.log(`Finished updating snapshot: ${this.props.expectedPath}`); } }