Skip to content

Commit

Permalink
Log function info in env mapping error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
CoryDanielson committed Apr 19, 2017
1 parent 64b3732 commit 1e8f703
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
16 changes: 15 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ function getNextVal(searchVal, mapping) {
}
}

// Used to stringify yielded values. Output includes functions
function stringifyVal(val) {
return JSON.stringify(val, (key, val) => {
if (typeof val === 'function') {
if (val.name) {
return val.name;
} else {
return `anonymous function: ${val.toString()}`;
}
}
return val;
}, 2);
}

function sagaTestEngine(genFunc, envMapping, ...initialArgs) {
assert(
isGeneratorFunction(genFunc),
Expand All @@ -80,7 +94,7 @@ function sagaTestEngine(genFunc, envMapping, ...initialArgs) {
const yieldedEffectIsPut = isPut(val) || isNestedPut(val)
assert(
(isFirstLoop || nextValFound || yieldedUndefined || yieldedEffectIsPut),
`Env Mapping is missing a value for ${JSON.stringify(val, null, 2)}`)
`Env Mapping is missing a value for ${stringifyVal(val)}`)

const genResult = gen.next(nextVal)

Expand Down
38 changes: 30 additions & 8 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,35 +170,57 @@ test('sagaTestEngine throws under bad conditions', t => {
() => sagaTestEngine(f, badMapping),
'Env Mapping is missing a value for "key"')

// Bad mapping for saga that yields obj with anonymous function
function namedFunction() {}
const f2 = function*() {
yield { select: namedFunction }
}
const badMapping2 = [['no', 'functions']]
t.throws(
() => sagaTestEngine(f2, badMapping2),
`Env Mapping is missing a value for ${JSON.stringify({ "select": "namedFunction" }, null, 2)}`
)

// Bad mapping for saga that yields object with anonymous function
var anonymousFunction = function() { return 'something'; }
const f3 = function*() {
yield { select: anonymousFunction }
}
const badMapping3 = [['also no', 'functions']]
t.throws(
() => sagaTestEngine(f3, badMapping3),
`Env Mapping is missing a value for ${JSON.stringify({ select: `anonymous function: ${anonymousFunction.toString()}` }, null, 2)}`
)

// No errors thrown
const goodMapping = [['key', 'value']]
t.notThrows(() => sagaTestEngine(f, goodMapping))

const f2 = function*() {
const f4 = function*() {
yield 'key1'
yield 'key2'
}
const goodMapping2 = [['key1', 'value1'], ['key2', 'value2']]
t.notThrows(() => sagaTestEngine(f2, goodMapping2))
t.notThrows(() => sagaTestEngine(f4, goodMapping2))

const f3 = function*() {
const f5 = function*() {
yield undefined
}
const goodMapping3 = [[undefined, undefined]]
t.notThrows(() => sagaTestEngine(f3, goodMapping3))
t.notThrows(() => sagaTestEngine(f5, goodMapping3))

const f4 = function*() {
const f6 = function*() {
yield [put({a: 1})]
}
t.notThrows(
() => sagaTestEngine(f4, goodMapping3),
() => sagaTestEngine(f6, goodMapping3),
'Correctly handles nested array of puts'
)

const f5 = function*() {
const f7 = function*() {
yield [select(() => 1)]
}
t.throws(() => sagaTestEngine(f5, goodMapping3))
t.throws(() => sagaTestEngine(f7, goodMapping3))
})


Expand Down

0 comments on commit 1e8f703

Please sign in to comment.