forked from graphql/graphql-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix crash in node when mixing sync/async resolvers (backport of graph…
- Loading branch information
Showing
3 changed files
with
126 additions
and
14 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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { expect } from 'chai'; | ||
|
||
import isObjectLike from '../jsutils/isObjectLike'; | ||
import mapValue from '../jsutils/mapValue'; | ||
|
||
/** | ||
* Deeply transforms an arbitrary value to a JSON-safe value by calling toJSON | ||
* on any nested value which defines it. | ||
*/ | ||
function toJSONDeep(value) { | ||
if (!isObjectLike(value)) { | ||
return value; | ||
} | ||
|
||
if (typeof value.toJSON === 'function') { | ||
return value.toJSON(); | ||
} | ||
|
||
if (Array.isArray(value)) { | ||
return value.map(toJSONDeep); | ||
} | ||
|
||
return mapValue(value, toJSONDeep); | ||
} | ||
|
||
export default function expectJSON(actual) { | ||
const actualJSON = toJSONDeep(actual); | ||
|
||
return { | ||
toDeepEqual(expected) { | ||
const expectedJSON = toJSONDeep(expected); | ||
expect(actualJSON).to.deep.equal(expectedJSON); | ||
}, | ||
toDeepNestedProperty(path, expected) { | ||
const expectedJSON = toJSONDeep(expected); | ||
expect(actualJSON).to.deep.nested.property(path, expectedJSON); | ||
}, | ||
}; | ||
} | ||
|
||
export function expectToThrowJSON(fn) { | ||
function mapException() { | ||
try { | ||
return fn(); | ||
} catch (error) { | ||
throw toJSONDeep(error); | ||
} | ||
} | ||
|
||
return expect(mapException).to.throw(); | ||
} |
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