-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: load custom pre-compare, compare and store functions 350 (#351)
* feat: adding compare fn module option * chore: passing tests with custom functions * feat: add pre-compare function
- Loading branch information
Showing
16 changed files
with
246 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
node_modules/ | ||
.DS_Store | ||
npm-debug.log | ||
temp-test-* | ||
temp-* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# test-custom-compare-fn | ||
|
||
This repo shows how `snap-shot-it` can load a custom compare function via config in `package.json` file. | ||
|
||
See /~https://github.com/bahmutov/snap-shot-it/issues/350 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const Result = require('folktale/result') | ||
const debug = require('debug')('snap-shot-it') | ||
|
||
const compare = ({ expected, value }) => { | ||
debug('in compaaaaare.js') | ||
|
||
// convert "expected" to string of same length, but with "a" characters | ||
const aaas = value.replace(/./g, 'a') | ||
debug('original value: %s', value) | ||
debug('aaas: %s', aaas) | ||
debug('expected value: %s', expected) | ||
|
||
if (aaas === expected) { | ||
return Result.Ok() | ||
} else { | ||
return Result.Error({ | ||
message: 'Hmm, not the same "aaaa..."', | ||
expected, | ||
value: aaas | ||
}) | ||
} | ||
} | ||
|
||
module.exports = compare |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "test-custom-compare-fn", | ||
"version": "1.0.0", | ||
"description": "shows custom compare function", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "../node_modules/.bin/mocha 'specs/**/*.js'" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"config": { | ||
"snap-shot-it": { | ||
"compare": "./compaaaaaare", | ||
"store": "./store" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const snapshot = require('../..') | ||
|
||
/* eslint-env mocha */ | ||
it('random string as 10 As', () => { | ||
// our custom compare function replaces strings with | ||
// same length of "aaaa..." :) | ||
const s = Math.random() | ||
.toString() | ||
.substr(0, 10) | ||
snapshot(s) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const debug = require('debug')('snap-shot-it') | ||
|
||
const store = value => { | ||
// converts value before storing it on disk | ||
const transformed = value.replace(/./g, 'a') | ||
debug('original value: %s', value) | ||
debug('transformed: %s', transformed) | ||
return transformed | ||
} | ||
|
||
module.exports = store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"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": { | ||
"pre-compare": "./pre-compare" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const preCompare = value => { | ||
// assuming the value is a string | ||
return value.length | ||
} | ||
|
||
module.exports = preCompare |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const snapshot = require('../..') | ||
|
||
/* eslint-env mocha */ | ||
it('stores string as number', () => { | ||
// should be saved as the length of the string | ||
snapshot('aaaaa') | ||
snapshot('aaa') | ||
}) |