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

Append named exports to the generated esm bundle #1835

Merged
merged 2 commits into from Jun 10, 2018
Merged

Append named exports to the generated esm bundle #1835

merged 2 commits into from Jun 10, 2018

Conversation

chrisguttandin
Copy link
Contributor

@chrisguttandin chrisguttandin commented Jun 9, 2018

Purpose (TL;DR) - mandatory

This pull request will add named exports to the ESM bundle to allow parts of sinon to be imported like this:

import { spy } from 'sinon';

It fixes issue ##1833.

As a byproduct it will also equalize the following to import statements:

import * as sinon from 'sinon';
import sinon from 'sinon';

It works by appending all named exports to the bundle generated by rollup. With the current API the following code block gets appended:

const _serverPrototype = sinon.serverPrototype;
export { _serverPrototype as serverPrototype };
const _getFakes = sinon.getFakes;
export { _getFakes as getFakes };
const _getRestorers = sinon.getRestorers;
export { _getRestorers as getRestorers };
const _createStubInstance = sinon.createStubInstance;
export { _createStubInstance as createStubInstance };
const _inject = sinon.inject;
export { _inject as inject };
const _mock = sinon.mock;
export { _mock as mock };
const _reset = sinon.reset;
export { _reset as reset };
const _resetBehavior = sinon.resetBehavior;
export { _resetBehavior as resetBehavior };
const _resetHistory = sinon.resetHistory;
export { _resetHistory as resetHistory };
const _restore = sinon.restore;
export { _restore as restore };
const _restoreContext = sinon.restoreContext;
export { _restoreContext as restoreContext };
const _replace = sinon.replace;
export { _replace as replace };
const _replaceGetter = sinon.replaceGetter;
export { _replaceGetter as replaceGetter };
const _replaceSetter = sinon.replaceSetter;
export { _replaceSetter as replaceSetter };
const _spy = sinon.spy;
export { _spy as spy };
const _stub = sinon.stub;
export { _stub as stub };
const _useFakeTimers = sinon.useFakeTimers;
export { _useFakeTimers as useFakeTimers };
const _verify = sinon.verify;
export { _verify as verify };
const _verifyAndRestore = sinon.verifyAndRestore;
export { _verifyAndRestore as verifyAndRestore };
const _useFakeServer = sinon.useFakeServer;
export { _useFakeServer as useFakeServer };
const _useFakeXMLHttpRequest = sinon.useFakeXMLHttpRequest;
export { _useFakeXMLHttpRequest as useFakeXMLHttpRequest };
const _usingPromise = sinon.usingPromise;
export { _usingPromise as usingPromise };
const _sandbox = sinon.sandbox;
export { _sandbox as sandbox };
const _createSandbox = sinon.createSandbox;
export { _createSandbox as createSandbox };
const _assert = sinon.assert;
export { _assert as assert };
const _fake = sinon.fake;
export { _fake as fake };
const _match = sinon.match;
export { _match as match };
const _spyCall = sinon.spyCall;
export { _spyCall as spyCall };
const _expectation = sinon.expectation;
export { _expectation as expectation };
const _defaultConfig = sinon.defaultConfig;
export { _defaultConfig as defaultConfig };
const _setFormatter = sinon.setFormatter;
export { _setFormatter as setFormatter };
const _timers = sinon.timers;
export { _timers as timers };
const _xhr = sinon.xhr;
export { _xhr as xhr };
const _FakeXMLHttpRequest = sinon.FakeXMLHttpRequest;
export { _FakeXMLHttpRequest as FakeXMLHttpRequest };
const _fakeServer = sinon.fakeServer;
export { _fakeServer as fakeServer };
const _fakeServerWithClock = sinon.fakeServerWithClock;
export { _fakeServerWithClock as fakeServerWithClock };
const _createFakeServer = sinon.createFakeServer;
export { _createFakeServer as createFakeServer };
const _createFakeServerWithClock = sinon.createFakeServerWithClock;
export { _createFakeServerWithClock as createFakeServerWithClock };
const _addBehavior = sinon.addBehavior;
export { _addBehavior as addBehavior };

How to verify - mandatory

  1. Check out this branch
  2. npm install
  3. npm run build

Checklist for author

  • npm run lint passes
  • References to standard library functions are cached.

@fatso83
Copy link
Contributor

fatso83 commented Jun 9, 2018

Great stuff! Do you by any chance have any pre-made repos/demos that shows its use (and that it's working)?

#lazymaintainer

@chrisguttandin
Copy link
Contributor Author

Yes I for example import sinon like that in my standardized-audio-context package: https://github.com/chrisguttandin/standardized-audio-context/search?q=spy&unscoped_q=spy

@chrisguttandin
Copy link
Contributor Author

@fatso83 I added a test which verifies the named exports: 9ff9c7d

@fatso83
Copy link
Contributor

fatso83 commented Jun 10, 2018

Tried testing standardized-audio-context, but it wasn't possible to run without some local environment variables, so I'll take your word for it :-)

I'll merge this if you could remove the duplication.

@chrisguttandin
Copy link
Contributor Author

Yes, the tests of standardized-audio-context have other dependencies which make them not a good example. That's why I tried to add a test case here, so you don't just have to believe me. :-) If you want to test it with another package yourself midi-player (https://github.com/chrisguttandin/midi-player) might be a better fit as it has much simpler tests.

I merged the two puppeteer tests and made the test case much simpler.

Copy link
Contributor

@fatso83 fatso83 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants