Skip to content

Commit

Permalink
fix: form error message without undefined, close #221
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Mar 20, 2019
1 parent 8b9321f commit 9cf28fa
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 25 deletions.
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -115,6 +115,7 @@
"lazy-ass": "1.6.0",
"mkdirp": "0.5.1",
"pluralize": "7.0.0",
"quote": "0.4.0",
"ramda": "0.26.1"
}
}
2 changes: 1 addition & 1 deletion src/cypress-system.js
Expand Up @@ -72,7 +72,7 @@ function init () {
// }
}

function raiseIfDifferent ({value, expected}) {
function raiseIfDifferent ({ value, expected }) {
cy.then(() => {
expect(value).to.equal(expected)
})
Expand Down
64 changes: 64 additions & 0 deletions src/index-spec.js
@@ -0,0 +1,64 @@
'use strict'

const la = require('lazy-ass')
const is = require('check-more-types')
const { stripIndent } = require('common-tags')

/* eslint-env mocha */
describe('throwCannotSaveOnCI', () => {
const { throwCannotSaveOnCI } = require('./index')

it('is a function', () => {
la(is.fn(throwCannotSaveOnCI))
})

it('throws good message for auto formed', () => {
try {
throwCannotSaveOnCI({
value: 'foo',
fileParameter: 'spec.js',
exactSpecName: null,
specName: 'my spec',
index: 1
})
} catch (e) {
const expected = stripIndent`
Cannot store new snapshot value
in "spec.js"
for snapshot called "my spec"
test key "my spec 1"
when running on CI (opts.ci = 1)
see https://github.com/bahmutov/snap-shot-core/issues/5
`
la(
e.message === expected,
'expected:\n' + expected + '\n\nactual:\n' + e.message
)
}
})

it('throws good message for exact snapshot name', () => {
try {
throwCannotSaveOnCI({
value: 'foo',
fileParameter: 'spec.js',
exactSpecName: 'my snapshot name',
specName: null,
index: null
})
} catch (e) {
const expected = stripIndent`
Cannot store new snapshot value
in "spec.js"
for snapshot called "my snapshot name"
test key "my snapshot name"
when running on CI (opts.ci = 1)
see https://github.com/bahmutov/snap-shot-core/issues/5
`
la(
e.message === expected,
'expected:\n' + expected + '\n\nactual:\n' + e.message
)
}
})
})
68 changes: 51 additions & 17 deletions src/index.js
Expand Up @@ -6,6 +6,7 @@ const la = require('lazy-ass')
const is = require('check-more-types')
const utils = require('./utils')
const isCI = require('is-ci')
const quote = require('quote')

const snapshotIndex = utils.snapshotIndex
const strip = utils.strip
Expand Down Expand Up @@ -40,7 +41,7 @@ var snapshotsPerTest = {}
* Forms unique long name for a snapshot
* @param {string} specName
* @param {number} oneIndex
*/
*/
const formKey = (specName, oneIndex) => `${specName} ${oneIndex}`

function restore (options) {
Expand Down Expand Up @@ -156,6 +157,30 @@ function storeValue (options) {

const isPromise = x => is.object(x) && is.fn(x.then)

function throwCannotSaveOnCI ({
value,
fileParameter,
exactSpecName,
specName,
index
}) {
const key = exactSpecName || formKey(specName, index)
throw new Error(
'Cannot store new snapshot value\n' +
'in ' +
quote(fileParameter) +
'\n' +
'for snapshot called ' +
quote(exactSpecName || specName) +
'\n' +
'test key ' +
quote(key) +
'\n' +
'when running on CI (opts.ci = 1)\n' +
'see https://github.com/bahmutov/snap-shot-core/issues/5'
)
}

function core (options) {
const what = options.what // value to store
la(
Expand Down Expand Up @@ -234,21 +259,29 @@ function core (options) {
if (opts.ci) {
console.log('current directory', process.cwd())
console.log('new value to save: %j', value)
const key = formKey(specName, index)
throw new Error(
'Cannot store new snapshot value\n' +
'in ' +
fileParameter +
'\n' +
'for spec called "' +
specName +
'"\n' +
'test key "' +
key +
'"\n' +
'when running on CI (opts.ci = 1)\n' +
'see https://github.com/bahmutov/snap-shot-core/issues/5'
)
return throwCannotSaveOnCI({
value,
fileParameter,
exactSpecName,
specName,
index
})

// const key = exactSpecName || formKey(specName, index)
// throw new Error(
// 'Cannot store new snapshot value\n' +
// 'in ' +
// quote(fileParameter) +
// '\n' +
// 'for snapshot called ' +
// quote(exactSpecName || specName) +
// '\n' +
// 'test key ' +
// quote(key) +
// '\n' +
// 'when running on CI (opts.ci = 1)\n' +
// 'see https://github.com/bahmutov/snap-shot-core/issues/5'
// )
}

const storedValue = store(value)
Expand Down Expand Up @@ -294,5 +327,6 @@ const prune = require('./prune')(fs).pruneSnapshots
module.exports = {
core,
restore,
prune
prune,
throwCannotSaveOnCI
}
10 changes: 5 additions & 5 deletions src/utils-spec.js
Expand Up @@ -34,28 +34,28 @@ describe('compare', () => {
it('returns Result', () => {
const expected = 'foo'
const value = 'foo'
const r = compare({expected, value})
const r = compare({ expected, value })
la(Result.hasInstance(r))
})

it('passes identical values', () => {
const expected = 'foo'
const value = 'foo'
const r = compare({expected, value})
const r = compare({ expected, value })
la(r.value === undefined)
})

it('has error text', () => {
const expected = 'foo'
const value = 'bar'
const r = compare({expected, value})
const r = compare({ expected, value })
la(r.value === '"foo" !== "bar"')
})

it('has error (snapshot)', () => {
const expected = 'foo'
const value = 'bar'
snapshot(compare({expected, value}))
snapshot(compare({ expected, value }))
})

const raise = () => {
Expand All @@ -64,7 +64,7 @@ describe('compare', () => {
it('snapshots error value', () => {
const expected = 'foo'
const value = 'bar'
compare({expected, value})
compare({ expected, value })
.map(raise)
.orElse(snapshot)
})
Expand Down

0 comments on commit 9cf28fa

Please sign in to comment.