snap-shot-core
Advanced tools
Comparing version 8.0.3 to 8.1.0
{ | ||
"name": "snap-shot-core", | ||
"description": "Save / load named snapshots, useful for tests", | ||
"version": "8.0.3", | ||
"version": "8.1.0", | ||
"author": "Gleb Bahmutov <gleb.bahmutov@gmail.com>", | ||
@@ -98,2 +98,3 @@ "bugs": "https://github.com/bahmutov/snap-shot-core/issues", | ||
"disparity": "2.0.0", | ||
"execa": "1.0.0", | ||
"execa-wrap": "1.4.0", | ||
@@ -103,4 +104,6 @@ "git-issues": "1.3.1", | ||
"mocha": "6.1.4", | ||
"mocked-env": "1.2.4", | ||
"pre-git": "3.17.1", | ||
"semantic-release": "15.13.3", | ||
"shelljs": "0.8.3", | ||
"simple-commit-message": "4.0.3", | ||
@@ -107,0 +110,0 @@ "sinon": "7.3.2", |
@@ -32,3 +32,6 @@ [![TODO board](https://imdone.io/api/1.0/projects/5b1adebb4f7fd004e58ef569/badge)](https://imdone.io/app#/board/bahmutov/snap-shot-core) | ||
raiser: raiseErrorFn, // optional | ||
ext: '.test' // default value is '.snapshot.js' | ||
ext: '.test', // default value is '.snapshot.js' | ||
opts: { | ||
// see below | ||
} | ||
}) | ||
@@ -133,3 +136,4 @@ ``` | ||
- `ci` - the tests are running on CI, which should disallow _saving snapshots_ | ||
- `sortSnapshots` - enable sorting snapshots by name when saving (default is true) | ||
- `sortSnapshots` - enable sorting snapshots by name when saving (default is false) | ||
- `useRelativePath` - use relative paths inside `__snapshots__` folder to recreate folder structure to mimic spec file relative path. Default is false. | ||
@@ -143,3 +147,4 @@ ```js | ||
ci: Boolean(process.env.CI), | ||
sortSnapshots: true | ||
sortSnapshots: false, | ||
useRelativePath: false | ||
} | ||
@@ -159,2 +164,25 @@ snapShot.core({ | ||
## useRelativePath | ||
When you pass `useRelativePath: true` option, the folder structure inside the `__snapshots__` folder will recreate the folder paths to the spec. For example if the specs are in subfolders: | ||
```text | ||
specs/ | ||
foo/ | ||
spec.js | ||
bar/ | ||
spec.js | ||
``` | ||
Then output snapshots will be saved as | ||
```text | ||
__snapshots__/ | ||
specs/ | ||
foo/ | ||
spec.js.snapshot.js | ||
bar/ | ||
spec.js.snapshot.js | ||
``` | ||
## Pruning snapshots | ||
@@ -161,0 +189,0 @@ |
@@ -29,3 +29,17 @@ 'use strict' | ||
const fromCurrentFolder = path.relative.bind(null, cwd) | ||
const snapshotsFolder = fromCurrentFolder('__snapshots__') | ||
const snapshotsFolderName = '__snapshots__' | ||
/** | ||
* Given relative path, returns same relative path, but inside | ||
* the snapshots folder. | ||
* @example | ||
* joinSnapshotsFolder('foo/bar') | ||
* // CWD/__snapshots__/foo/bar | ||
*/ | ||
const joinSnapshotsFolder = path.join.bind(null, cwd, snapshotsFolderName) | ||
// TODO: expose the name of the snapshots folder to the outside world id:16 | ||
// - <https://github.com/bahmutov/snap-shot-core/issues/245> | ||
// Gleb Bahmutov | ||
// gleb.bahmutov@gmail.com | ||
const snapshotsFolder = fromCurrentFolder(snapshotsFolderName) | ||
debug('process cwd: %s', cwd) | ||
@@ -44,6 +58,26 @@ debug('snapshots folder: %s', snapshotsFolder) | ||
const isOptions = is.schema({ | ||
const isSaveOptions = is.schema({ | ||
sortSnapshots: is.bool | ||
}) | ||
const isLoadOptions = is.schema({ | ||
useRelativePath: is.bool | ||
}) | ||
function getSnapshotsFolder (specFile, opts = { useRelativePath: false }) { | ||
if (!opts.useRelativePath) { | ||
// all snapshots go into the same folder | ||
return snapshotsFolder | ||
} | ||
const relativeDir = fromCurrentFolder(path.dirname(specFile)) | ||
verbose('relative path to spec file %s is %s', specFile, relativeDir) | ||
// return path.join(resolveToCwd(relativeDir), '__snapshots__') | ||
const folder = joinSnapshotsFolder(relativeDir) | ||
verbose('snapshot folder %s', folder) | ||
return folder | ||
} | ||
function loadSnaps (snapshotPath) { | ||
@@ -73,9 +107,17 @@ const full = require.resolve(snapshotPath) | ||
function fileForSpec (specFile, ext) { | ||
function fileForSpec (specFile, ext, opts = { useRelativePath: false }) { | ||
la(is.maybe.string(ext), 'invalid extension to find', ext) | ||
la(isLoadOptions(opts), 'expected fileForSpec options', opts) | ||
const specName = path.basename(specFile) | ||
verbose('spec file %s has name %s', specName) | ||
const snapshotFolder = getSnapshotsFolder(specFile, opts) | ||
let filename = path.join(snapshotsFolder, specName) | ||
verbose( | ||
'spec file "%s" has name "%s" and snapshot folder %s', | ||
specFile, | ||
specName, | ||
snapshotFolder | ||
) | ||
let filename = path.join(snapshotFolder, specName) | ||
if (ext) { | ||
@@ -106,6 +148,7 @@ if (!filename.endsWith(ext)) { | ||
function loadSnapshots (specFile, ext) { | ||
function loadSnapshots (specFile, ext, opts = { useRelativePath: false }) { | ||
la(is.unemptyString(specFile), 'missing specFile name', specFile) | ||
la(isLoadOptions(opts), 'expected loadSnapshots options', opts) | ||
const filename = fileForSpec(specFile, ext) | ||
const filename = fileForSpec(specFile, ext, opts) | ||
verbose('from spec %s got snap filename %s', specFile, filename) | ||
@@ -116,3 +159,3 @@ return loadSnapshotsFrom(filename) | ||
function prepareFragments (snapshots, opts = { sortSnapshots: true }) { | ||
la(isOptions(opts), 'expected prepare fragments options', opts) | ||
la(isSaveOptions(opts), 'expected prepare fragments options', opts) | ||
@@ -149,8 +192,16 @@ const names = opts.sortSnapshots | ||
ext, | ||
opts = { sortSnapshots: true } | ||
opts = { sortSnapshots: true, useRelativePath: false } | ||
) { | ||
la(isOptions(opts), 'expected save snapshots options', opts) | ||
la( | ||
isSaveOptions(opts) && isLoadOptions(opts), | ||
'expected save snapshots options', | ||
opts | ||
) | ||
const snapshotsFolder = getSnapshotsFolder(specFile, opts) | ||
debug('for spec file %s', specFile) | ||
debug('making folder "%s" for snapshot if does not exist', snapshotsFolder) | ||
mkdirp.sync(snapshotsFolder) | ||
const filename = fileForSpec(specFile, ext) | ||
const filename = fileForSpec(specFile, ext, opts) | ||
const specRelativeName = fromCurrentFolder(specFile) | ||
@@ -217,3 +268,5 @@ debug('saving snapshots into %s for %s', filename, specRelativeName) | ||
exportText, | ||
prepareFragments | ||
prepareFragments, | ||
joinSnapshotsFolder, | ||
snapshotsFolderName | ||
} |
@@ -84,5 +84,14 @@ 'use strict' | ||
debug('loading snapshots from %s ext %s for spec %s', file, ext, relativePath) | ||
const snapshots = fs.loadSnapshots(file, ext) | ||
debug( | ||
'loading snapshots for file %s ext %s from path %s (relative to CWD)', | ||
file, | ||
ext, | ||
relativePath | ||
) | ||
const loadOptions = R.pick(['useRelativePath'], opts) | ||
debug('load options %o', loadOptions) | ||
const snapshots = fs.loadSnapshots(file, ext, loadOptions) | ||
if (!snapshots) { | ||
debug('could not find any snapshots') | ||
return | ||
@@ -137,3 +146,7 @@ } | ||
// as comments above each key? | ||
const snapshots = fs.loadSnapshots(file, ext) | ||
const snapshots = fs.loadSnapshots( | ||
file, | ||
ext, | ||
R.pick(['useRelativePath'], opts) | ||
) | ||
const key = exactSpecName || formKey(specName, index) | ||
@@ -149,3 +162,8 @@ snapshots[key] = value | ||
if (!opts.dryRun) { | ||
fs.saveSnapshots(file, snapshots, ext, R.pick(['sortSnapshots'], opts)) | ||
fs.saveSnapshots( | ||
file, | ||
snapshots, | ||
ext, | ||
R.pick(['sortSnapshots', 'useRelativePath'], opts) | ||
) | ||
debug('saved updated snapshot %d for spec "%s"', index, specName) | ||
@@ -231,5 +249,10 @@ | ||
debug('setting sortSnapshots flags to true') | ||
opts.sortSnapshots = true | ||
opts.sortSnapshots = false | ||
} | ||
if (!('useRelativePath' in opts)) { | ||
debug('setting useRelativePath flag to false') | ||
opts.useRelativePath = false | ||
} | ||
if (ext) { | ||
@@ -236,0 +259,0 @@ la(ext[0] === '.', 'extension should start with .', ext) |
@@ -40,4 +40,4 @@ const R = require('ramda') | ||
const pruneSnapshotsInFile = ({ fs, byFilename, ext }) => file => { | ||
const snapshots = fs.loadSnapshots(file, ext) | ||
const pruneSnapshotsInFile = ({ fs, byFilename, ext }, opts) => file => { | ||
const snapshots = fs.loadSnapshots(file, ext, opts) | ||
if (is.empty(snapshots)) { | ||
@@ -56,3 +56,3 @@ debug('empty snapshots to prune in file', file) | ||
debug('saving pruned snapshot file for', file) | ||
fs.saveSnapshots(file, prunedSnapshots, ext) | ||
fs.saveSnapshots(file, prunedSnapshots, ext, opts) | ||
} | ||
@@ -64,3 +64,3 @@ | ||
// https://github.com/bahmutov/snap-shot-core/issues/88 | ||
const pruneSnapshots = fs => ({ tests, ext = utils.DEFAULT_EXTENSION }) => { | ||
const pruneSnapshots = (fs) => ({ tests, ext = utils.DEFAULT_EXTENSION }, opts) => { | ||
la(is.array(tests), 'missing tests', tests) | ||
@@ -72,3 +72,3 @@ const byFilename = R.groupBy(R.prop('file'), tests) | ||
Object.keys(byFilename).forEach(pruneSnapshotsInFile({ fs, byFilename, ext })) | ||
Object.keys(byFilename).forEach(pruneSnapshotsInFile({ fs, byFilename, ext }, opts)) | ||
} | ||
@@ -75,0 +75,0 @@ |
42586
906
348
19