Skip to content

Commit

Permalink
Auto-update snapshot tests if changing strings
Browse files Browse the repository at this point in the history
The special incantation to update snapshots is tough to remember and non-obvious to many folks who are just updating text copy in the app. This makes the CI workflow auto-update snapshot tests on pull requests (not other pushes) where there were changes to the english strings. It's possible this could make tests pass when they shouldn't (if other things that should not have changed were changed), so it also posts a comment to the PR if it had to update the snapshots that reminds a reviewer to check the snapshot changes.
  • Loading branch information
Mr0grog committed Feb 22, 2025
1 parent f3c4eee commit 542dc2e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
57 changes: 54 additions & 3 deletions .github/workflows/build-client.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ jobs:
uses: actions/setup-node@v4
with:
node-version: ${{ env.node-version }}
cache: npm
cache-dependency-path: client/package-lock.json

- name: Install
run: npm ci
Expand All @@ -55,22 +57,65 @@ jobs:

test:
runs-on: ubuntu-latest
needs:
- translations
defaults:
run:
working-directory: client
permissions:
contents: write # For commiting changes back to the current PR.
pull-requests: write # For commenting on the PR.
steps:
- uses: actions/checkout@v4

- name: Use Node.js ${{ env.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ env.node-version }}
cache: npm
cache-dependency-path: client/package-lock.json

- name: Install
run: npm ci

# We want to know if this branch has changed the english strings, not
# just if the `translations` job had to update it, so we need this check.
- uses: dorny/paths-filter@v3
id: changes
with:
filters: |
english:
- client/src/i18n/en.json
- name: Unit tests
run: npm test
env:
update_snapshots: ${{ github.event_name == 'pull_request' && steps.changes.outputs.english == 'true' }}
run: |
if [ "${update_snapshots}" == 'true' ]; then
npm run test:update
else
npm test
fi
- name: Commit changes to Snapshots
uses: stefanzweifel/git-auto-commit-action@v5
id: commit
if: github.event_name == 'pull_request'
with:
commit_message: 'Auto-update snapshot tests'
file_pattern: '*.snap'

- name: Comment on the PR about changes
uses: thollander/actions-comment-pull-request@v3
if: steps.commit.outputs.changes_detected == 'true'
env:
commit_url: 'https://github.com/${{ github.repository }}/commit/${{ steps.commit.outputs.commit_hash }}'
with:
message: >
🚨 The "snapshot" tests in your PR have been automatically updated
because you changed some text in the app! **Make sure [the changes
to `.snap` files](${{ env.commit_url }}) are correct** and don’t
include other things that should *not* have changed.
lint:
runs-on: ubuntu-latest
Expand All @@ -84,6 +129,8 @@ jobs:
uses: actions/setup-node@v4
with:
node-version: ${{ env.node-version }}
cache: npm
cache-dependency-path: client/package-lock.json

- name: Install
run: npm ci
Expand Down Expand Up @@ -116,6 +163,8 @@ jobs:
uses: actions/setup-node@v4
with:
node-version: ${{ env.node-version }}
cache: npm
cache-dependency-path: client/package-lock.json

- name: Install
run: npm ci
Expand All @@ -124,14 +173,16 @@ jobs:
if: github.event_name == 'pull_request'
run: npm run intl:extract

- uses: stefanzweifel/git-auto-commit-action@v5
- name: Commit changes to English strings
uses: stefanzweifel/git-auto-commit-action@v5
id: commit-strings
if: github.event_name == 'pull_request'
with:
commit_message: 'Auto-update en.json from component source code'
file_pattern: 'client/src/intl/en.json'

- uses: thollander/actions-comment-pull-request@v3
- name: Comment on the PR about changes
uses: thollander/actions-comment-pull-request@v3
if: steps.commit-strings.outputs.changes_detected == 'true'
with:
message: >
Expand Down
2 changes: 1 addition & 1 deletion client/src/data/copy/common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const UPDATE_BANNER_CONTENT_LARGE = <FormattedMessage

export const UPDATE_BANNER_CONTENT_SMALL = <FormattedMessage
id={'common.pages.alerts.banner.update.content.small'}
defaultMessage={`<bold>This is an unofficial copy of the CEJST Tool.</bold> We are working on an overhaul, but in the meantime some links and text may incorrectly suggest that this site is affiliated with the US Government.`}
defaultMessage={`<bold>This is an unofficial copy of the CEJST Tool!!!!</bold> We are working on an overhaul, but in the meantime some links and text may incorrectly suggest that this site is affiliated with the US Government.`}
description={`Alert body that appears at the top of pages.`}
values={{
bold: boldFn,
Expand Down

0 comments on commit 542dc2e

Please sign in to comment.