Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions amplify-migration-apps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -312,8 +312,8 @@ test('<app-name> snapshot', async () => {

```console
cd packages/amplify-cli
npx jest --no-coverage src/__tests__/commands/gen2-migration/generate/codegen-head/command-handlers.test.ts -t '<app-name> snapshot'
npx jest --no-coverage src/__tests__/commands/gen2-migration/refactor/refactor.test.ts -t '<app-name> snapshot'
npx jest --no-coverage src/__tests__/commands/gen2-migration/generate.test.ts -t '<app-name> snapshot'
npx jest --no-coverage src/__tests__/commands/gen2-migration/refactor.test.ts -t '<app-name> snapshot'
```

The tests should pass with no differences. They could fail for two reasons:
Expand All @@ -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 '<app-name> snapshot' --updateSnapshot
npx jest --no-coverage src/__tests__/commands/gen2-migration/generate.test.ts -t '<app-name> 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.

Comment thread
iliapolo marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,6 @@ export class Snapshot {
public async compare(actualDir: string, ignorePatterns?: RegExp[]): Promise<Report> {
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,
Expand All @@ -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}`);
}
}
Loading