This repository has been archived by the owner on Jun 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Add tests for utils #27
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,12 @@ | ||
{ | ||
"env": { | ||
"test": { | ||
"plugins": [ | ||
"transform-class-properties", | ||
"transform-es2015-modules-commonjs", | ||
"transform-object-rest-spread", | ||
["transform-react-jsx", { "pragma": "createElement" }] | ||
] | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { extract, fixedObserver, initObserver } from '../utils'; | ||
|
||
// mock a simple observer-type generator | ||
function* arrayObserver() { | ||
let array = []; | ||
|
||
array.push(yield); | ||
array.push(yield); | ||
|
||
return array; | ||
} | ||
|
||
// mock dynamic imports | ||
async function dynamicImport() { | ||
return { foo: 'foo', default: 'bar' }; | ||
} | ||
|
||
test('`extract` retrieves a named export', async () => { | ||
const foo = await extract(dynamicImport(), 'foo'); | ||
|
||
expect(foo).toBe('foo'); | ||
}); | ||
|
||
test('`extract` retrieves the default export', async () => { | ||
const bar = await extract(dynamicImport()); | ||
|
||
expect(bar).toBe('bar'); | ||
}); | ||
|
||
test('`extract` throws if the module fails to resolve', async () => { | ||
const error = new Error('Invalid namespace object provided.'); | ||
|
||
expect(extract(null)).rejects.toEqual(error); | ||
}); | ||
|
||
test('`extract` throws if the binding is not present', async () => { | ||
const error = new Error('Binding baz not found.'); | ||
|
||
expect(extract(dynamicImport(), 'baz')).rejects.toEqual(error); | ||
}); | ||
|
||
test('`fixedObserver` yields undefined', () => { | ||
const gen = fixedObserver(2); | ||
|
||
expect(gen.next()).toEqual({ value: void 0, done: false }); | ||
expect(gen.next()).toEqual({ value: void 0, done: false }); | ||
expect(gen.next()).toEqual({ value: void 0, done: true }); | ||
}); | ||
|
||
test('`fixedObserver` terminates if `length` is 0', () => { | ||
const gen = fixedObserver(0); | ||
|
||
expect(gen.next()).toEqual({ value: void 0, done: true }); | ||
expect(gen.next()).toEqual({ value: void 0, done: true }); | ||
}); | ||
|
||
test('`initObserver` starts an observer', () => { | ||
const gen = initObserver(arrayObserver)(); | ||
|
||
expect(gen.next(0)).toEqual({ value: void 0, done: false }); | ||
expect(gen.next(1)).toEqual({ value: [0, 1], done: true }); | ||
}); |
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,15 +1,22 @@ | ||
/** | ||
* Extract a single export from a module. | ||
* Retrieve a single exported binding from a module. | ||
* | ||
* @param {object} obj - A module's namespace object | ||
* @param {string} name - The key to look up | ||
* @param {string} name - The binding to retrieve | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
* @returns {Promise<*>} | ||
*/ | ||
export const extract = (obj, name = 'default') => | ||
Promise.resolve(obj) | ||
.then(mod => mod[name]) | ||
.catch(() => { | ||
throw new Error(`Object is not a valid module.`); | ||
}); | ||
Promise.resolve(obj).then(mod => { | ||
if (!mod || typeof mod !== 'object') { | ||
throw new Error('Invalid namespace object provided.'); | ||
} | ||
|
||
if (!mod.hasOwnProperty(name)) { | ||
throw new Error(`Binding ${name} not found.`); | ||
} | ||
|
||
return mod[name]; | ||
}); | ||
|
||
/** | ||
* Create an observer-type generator that yields a fixed number of values. | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a fan of having 2 separate Babel configuration files. We should choose 1 and confine all babel-related config to it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want to keep the JS version, jestjs/jest#1468 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'm also not a fan of it. I considered moving everything to
.babelrc
, but it's pretty verbose that way. Could still be better than hacking jest, though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whatever route you choose is just temporary until Babel 7, and then we have first-class support for
.babelrc.js
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep 2 files instead of using a hack. More maintenance, but the maintenance burden is obvious.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's my instinct here as well, @zetlen. And @DrewML we should consolidate both files into
.babelrc.js
as soon as we're on7.0
then.