Skip to content

Instantly share code, notes, and snippets.

@kimniche
Last active September 27, 2017 20:56
Show Gist options
  • Save kimniche/f18078b9fe2c29e965ab0371377e3975 to your computer and use it in GitHub Desktop.
Save kimniche/f18078b9fe2c29e965ab0371377e3975 to your computer and use it in GitHub Desktop.

Plugins

Add module exports

https://github.com/59naga/babel-plugin-add-module-exports

avoids need for:

require('./bundle.js') // { default: 'foo' }
require('./bundle.js').default // 'foo'

so our OG code still works fine:

require('./bundle.js') // foo

no more module.exports

Convert to export default

Tests

Rewire

https://www.npmjs.com/package/babel-plugin-rewire

  • No more explicit "rewire()" calls, auto translated via .babelrc
  • Calls to __get__ are now __GetDependency__
  • Must reset dependency if you __set__ it, since we no longer manually rewire in a beforeEach block
  • Assertions like expect(server)to.have.keys('function1', 'function2'...) should now use include instead of have, since the module will have extra rewired methods
Utils.__set__('_findFoo', _findFooSpy)

// ... some expect(...) assertion

Utils.__ResetDependency__('_findFoo')

Proxyquire

  • Removed node module from repo, but all tests using proxyquire are currently skipped (describe.skip)
  • In a followup we should transition these to use the getDependencies approach

TypeError: get_(...) is not a function

If you see this error, make sure you're exporting the function under test as a named export in the file under test

Unexpected test failures

Is there a __set__ call in a preceding test? Make sure you're calling __ResetDependency__ any time you do this, otherwise downstream tests will still respect whatever mock you passed to the module in your __set__ call

"exports a function" failures in actions

With this you'll see:

AssertionError: expected { Object (get, GetDependency, ...) } to be a function

Because of how babel-rewire works, each module under test gets several other methods (__GetDependency, etc). Proposed solution: don't typeof test actions anymore (?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment