Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log function info in env mapping error messages #14

Merged
merged 1 commit into from
Apr 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 `[Function: ${val.name}]: ${val.toString()}`;
} else {
return `[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
41 changes: 33 additions & 8 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,35 +170,60 @@ 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 { func: namedFunction }
}
const badMapping2 = [['bad', 'mapping']]
t.throws(
() => sagaTestEngine(f2, badMapping2),
`Env Mapping is missing a value for ${JSON.stringify({ "func": `[Function: namedFunction]: ${namedFunction.toString()}` }, null, 2)}`
)

// Bad mapping for saga that yields object with anonymous function
var anonymousFunction = function() { return 'something'; }
// Skip the anonymous function test if this anonymous functon is named (done in newer node versions)
if (!anonymousFunction.name) {
const f3 = function*() {
yield { func: anonymousFunction }
}
const badMapping3 = [['bad', 'mapping']]
t.throws(
() => sagaTestEngine(f3, badMapping3),
`Env Mapping is missing a value for ${JSON.stringify({ func: `[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