Skip to content

Commit

Permalink
feat: add sortSnapshots config option
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed May 7, 2019
1 parent bf1b251 commit 7fb952f
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 2 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,28 @@ You can update snapshot values
SNAPSHOT_UPDATE=1 npm test
```

If you want to sort saved snapshots alphabetically, run with
## Sorted snapshots

If you want to sort saved snapshots alphabetically inside each snapshot file, run with

```bash
SNAPSHOT_SORT=1 npm test
```

You can also set the config option in the package.json file

```json
{
"config": {
"snap-shot-it": {
"sortSnapshots": true
}
}
}
```

Hopefully sorting snapshots would help when updating them.

## Named snapshots

Renaming tests might lead to confusion and pruning snapshots. You can name the snapshots
Expand Down
94 changes: 94 additions & 0 deletions src/e2e-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ const checkSnapshots = (rootFolder, snapshots) => {
'as file',
filename
)
// important: remove the loaded module from the cache to
// avoid a test using old data
const loaded = require(filename)
delete require.cache[require.resolve(filename)]

const expected = snapshots[expectedFilename]
// compare the values
la(
R.equals(loaded, expected),
'in snapshot file',
Expand All @@ -56,6 +61,18 @@ const checkSnapshots = (rootFolder, snapshots) => {
'from expected',
expected
)
// compare the order of snapshots
const savedOrder = Object.keys(loaded)
const expectedOrder = Object.keys(expected)
la(
R.equals(savedOrder, expectedOrder),
'in snapshot file',
filename,
'the order of snapshots is',
savedOrder,
'but expected order of snapshots to be',
expectedOrder
)
})
}

Expand Down Expand Up @@ -250,3 +267,80 @@ describe('custom pre-compare function', () => {
})
})
})

describe('sorted snapshots', () => {
// folder with specs to run
const sourceFolder = join(__dirname, '..', 'test-sorting')
// temp folder to copy to before running tests
const tempFolder = join(__dirname, '..', 'temp-sorting')

beforeEach(() => {
copyFolder(sourceFolder, tempFolder)
})

it('does not sort by default', function () {
this.timeout(5000)

execa.shellSync('npm test', {
cwd: tempFolder,
stdio: 'inherit',
// only use the limited environment keys
// without "CI=1" value
env: limitedEnv,
extendEnv: false
})

checkSnapshots(tempFolder, {
'spec.js': {
zz: 3,
bb: 2,
aa: 1
}
})

// run the tests again to check if values are not clashing
// but with CI=1 to avoid writing new files accidentally
execa.shellSync('npm test', {
cwd: tempFolder,
stdio: 'inherit',
env: { CI: '1' }
})
})

it('sorts with config parameter', function () {
this.timeout(5000)

const packageFilename = join(tempFolder, 'package.json')
const pkg = JSON.parse(fs.readFileSync(packageFilename, 'utf8'))
pkg.config['snap-shot-it'] = {
sortSnapshots: true
}
fs.writeFileSync(packageFilename, JSON.stringify(pkg, null, 2), 'utf8')

execa.shellSync('npm test', {
cwd: tempFolder,
stdio: 'inherit',
// only use the limited environment keys
// without "CI=1" value
env: limitedEnv,
extendEnv: false
})

// now the snapshots should be sorted
checkSnapshots(tempFolder, {
'spec.js': {
aa: 1,
bb: 2,
zz: 3
}
})

// run the tests again to check if values are not clashing
// but with CI=1 to avoid writing new files accidentally
execa.shellSync('npm test', {
cwd: tempFolder,
stdio: 'inherit',
env: { CI: '1' }
})
})
})
3 changes: 2 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const defaults = {
useRelativePath: false,
'pre-compare': null,
compare: null,
store: null
store: null,
sortSnapshots: false
}
/**
* Pick only the keys we know about from whatever the user
Expand Down
5 changes: 5 additions & 0 deletions test-sorting/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# test-custom-pre-fn

This repo shows how `snap-shot-it` can load a custom pre-compare function via config in `package.json` file and use it to clean up data before comparing.

See https://github.com/bahmutov/snap-shot-it/issues/350
15 changes: 15 additions & 0 deletions test-sorting/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "test-custom-pre-fn",
"version": "1.0.0",
"description": "shows custom pre-compare function",
"main": "index.js",
"scripts": {
"test": "../node_modules/.bin/mocha 'specs/**/*.js'"
},
"keywords": [],
"author": "",
"license": "ISC",
"config": {
"snap-shot-it": {}
}
}
9 changes: 9 additions & 0 deletions test-sorting/specs/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const snapshot = require('../..')

/* eslint-env mocha */
it('has names in reverse order', () => {
// notice that snapshots are named NOT in sorted order
snapshot('zz', 3)
snapshot('bb', 2)
snapshot('aa', 1)
})

0 comments on commit 7fb952f

Please sign in to comment.