Skip to content

Commit

Permalink
Merge pull request #2 from hildjj/update-with-environment-var
Browse files Browse the repository at this point in the history
Allow updating snaps with 'UPDATE_SNAPSHOT' environment variable
  • Loading branch information
i-like-robots authored Apr 16, 2024
2 parents 4428fab + 4d968a1 commit a27a59d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ The created snapshots should be committed with your other code changes, and revi

### Updating snapshots

You can update your snapshots by running your test command with a `--updateSnapshot` or `-u` flag, by deleting the snapshot file, or using the `update` option.
You can update your snapshots by running your test command with a `--updateSnapshot` or `-u` flag, by deleting the snapshot file, specifying the "UPDATE_SNAPSHOT" environment variable, or using the `update` option.

### Expiring snapshots

Expand Down
4 changes: 4 additions & 0 deletions lib/snappy-snaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ import { escapeBacktickString } from './utils.js'
const SNAPSHOT_BANNER = '// Data Snap v1'
const SNAPSHOT_EXT = 'snap'
const SNAPSHOT_DIR = '__snapshots__'
const SNAPSHOT_ENV = 'UPDATE_SNAPSHOT'

const hasUpdateFlag = () => {
const argv = process.argv.slice(2)
if (process.env[SNAPSHOT_ENV] !== undefined) {
return true
}
return ['--updateSnapshot', '-u'].some((d) => argv.includes(d))
}

Expand Down
20 changes: 20 additions & 0 deletions test/snappy-snaps.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ describe('lib/snappy-snaps', () => {
process.argv.pop()

assert.equal(data.name, 'Gino')

process.argv.push('--updateSnapshot')

const data2 = await subject('dog', { name: 'Gino' })

process.argv.pop()

assert.equal(data2.name, 'Gino')
})

it('updates snapshots when run with the update environment variable', async () => {
await subject('dog', { name: 'Ness' })

process.env.UPDATE_SNAPSHOT = ''

const data = await subject('dog', { name: 'Gino' })

delete process.env.UPDATE_SNAPSHOT

assert.equal(data.name, 'Gino')
})

it('warns when snapshots are past their expiry date', async () => {
Expand Down

0 comments on commit a27a59d

Please sign in to comment.