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

Replace default exports with individual exports and namespaced imports #1907

Merged
merged 30 commits into from
Aug 4, 2021

Conversation

becca-bailey
Copy link
Contributor

@becca-bailey becca-bailey commented Jul 20, 2021

Closes #650
Closes #1909

Updates default export objects to individual exports to better support webpack tree-shaking. In order to preserve the namespacing (e.g. import { Helpers } from 'victory-core'), this adds the babel export namespace proposal in order to support syntax like export * as Helpers from 'victory-util/helpers', though it sounds like we might need to upgrade to webpack v5 to support tree-shaking with this syntax.

I am currently running into this issue with any helper methods that are mocked using sinon.stub(...) in the tests. I will continue to investigate this!

This also includes some testing improvements, including a TEST_MODULE env variable to isolate a single module. For example, now you can run:

TEST_MODULE=victory-axis nps test

This also adds test watcher scripts so you can run nps test.watch or nps karma.watch.

@becca-bailey becca-bailey marked this pull request as draft July 20, 2021 21:05
import * as Helpers from "../victory-util/helpers";
import * as LabelHelpers from "../victory-util/label-helpers";
import * as Style from "../victory-util/style";
import * as Log from "../victory-util/log";
import TextSize from "../victory-util/textsize";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm still working on TextSize - this one broke some tests and I'm still determining whether this is an issue with sinon mocking or some other kind of bug. Storybook looks the same, though.

Copy link
Member

Choose a reason for hiding this comment

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

@beccanelson If you're still spinning your wheels on this, feel free to just give me some reproduction instructions and I can take a look too!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I figured it out - the earlier version of sinon we were using for test stubbing doesn't support module exports. Upgrading sinon seems to be doing the trick to fix the tests.

@boygirl
Copy link
Contributor

boygirl commented Jul 20, 2021

@beccanelson This is all looking great to me! Please remove the draft status when you're ready so the chromatic tests aren't skipped

Copy link
Member

@ryan-roemer ryan-roemer left a comment

Choose a reason for hiding this comment

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

LGTM!

@becca-bailey
Copy link
Contributor Author

I just pushed up the sinon update, which should fix some of the test mocking issues I was having. Do we also want to include all the helper-methods.js files in each chart package, or should we save that for another PR?

@becca-bailey becca-bailey marked this pull request as ready for review July 21, 2021 18:22
@becca-bailey
Copy link
Contributor Author

Hmm, maybe there's something else going on here. I still see some of the test failures related to the TextSize module.

@boygirl
Copy link
Contributor

boygirl commented Jul 21, 2021

@beccanelson the tests I'm seeing fail look like they're related to fixLabelOverlap on VictoryAxis

@becca-bailey
Copy link
Contributor Author

becca-bailey commented Jul 21, 2021

@boygirl Yes, from what I can figure out we're trying to stub the result of TextSize.approximateTextSize to always return a consistent value, but that stub isn't working so it's using the default calculated size.

@boygirl
Copy link
Contributor

boygirl commented Jul 21, 2021

ah, gotcha

const stub = sandbox
.stub(pathHelpers, symbol)
.returns(`${symbol} symbol`);
stub = sinon.stub(pathHelpers, symbol).returns(`${symbol} symbol`);
Copy link
Member

@ryan-roemer ryan-roemer Jul 21, 2021

Choose a reason for hiding this comment

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

If there is an error in the test before this line of code your stub.restore() call in line 23 with also fail. Better to switch back to the sandbox approach because that is guaranteed to be instantiated in beforeEach

@@ -240,6 +240,27 @@ const _approximateTextHeightInternal = (text, style) => {
}, 0);
};

// Stubbable implementation.
export const _approximateTextSizeInternal = {
Copy link
Contributor Author

@becca-bailey becca-bailey Jul 28, 2021

Choose a reason for hiding this comment

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

@boygirl Do you have thoughts about this stubbable implementation that @ryan-roemer added? Normally I'm not a huge fan of doing hacky stuff like this for the sake of testing, but I also don't want to lose the benefit of the existing tests or spend a ton of time trying to figure out why this particular module isn't stubbable.

The other option we have (which probably isn't much better) is to do some old-fashioned dependency injection and accept the text size as an optional prop.

Copy link
Contributor

Choose a reason for hiding this comment

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

@beccanelson I agree that the stubbable implementation isn't great, but I think it's the best option for the moment. Let's go with that so you don't have to keep spinning your wheels on this.

Copy link
Member

Choose a reason for hiding this comment

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

I totally agree with y’all. 😛 If it was more than just one, we’d probably want to look for more first class solutions.

@becca-bailey becca-bailey force-pushed the improvement/replace-defualt-export branch from 449e0e6 to 27c9c77 Compare July 28, 2021 16:07
@becca-bailey
Copy link
Contributor Author

I rebased on main, and got some test errors that can be traced back to f873926. It seems like the latest sinon version and its dependencies are not playing well with webpack 5. 😭

@ryan-roemer
Copy link
Member

@beccanelson I don't have time for this today, but I can dig into this tomorrow if you'd like?

@becca-bailey
Copy link
Contributor Author

@ryan-roemer No rush on this, but I would appreciate some help whenever you're free!

@ryan-roemer
Copy link
Member

I'm up to date on this branch and getting the same error as CI, namely:

export 'LabelHelpers'.'getTextPolarAnchor' (imported as 'LabelHelpers') was not found in 'victory-core' (possible exports: getDegrees, getPolarAngle, getPolarTextAnchor, getPolarVerticalAnchor, getProps, getText)

I think this may indicate potential issues with our upstream consumers for the exports change, so I'll dig in more tomorrow about implications. (tl;dr, we want our es/**/*.js output to be straight up buildable with webpack / other bundlers without needing further babel transformations, and the namespace export might not actually be...)

@becca-bailey becca-bailey force-pushed the improvement/replace-defualt-export branch from fe42e79 to 3742284 Compare August 2, 2021 17:14
@ryan-roemer
Copy link
Member

Here's what is currently failing:

/*!*************************************************************!*\
  !*** ../../node_modules/parse5/lib/parser/parser_stream.js ***!
  \*************************************************************/
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {

"use strict";


var WritableStream = __webpack_require__(/*! stream */ "?2cec").Writable,
    inherits = __webpack_require__(/*! util */ "../../node_modules/util/util.js").inherits,
    Parser = __webpack_require__(/*! ./index */ "../../node_modules/parse5/lib/parser/index.js");

var ParserStream = module.exports = function (options) {
    WritableStream.call(this);

    this.parser = new Parser(options);

    this.lastChunkWritten = false;
    this.writeCallback = null;
    this.pausedByScript = false;

    this.document = this.parser.treeAdapter.createDocument();

    this.pendingHtmlInsertions = [];

    this._resume = this._resume.bind(this);
    this._documentWrite = this._documentWrite.bind(this);
    this._scriptHandler = this._scriptHandler.bind(this);

    this.parser._bootstrap(this.document, null);
};

// ==> THIS IS FAILING <===
inherits(ParserStream, WritableStream);

//WritableStream implementation
ParserStream.prototype._write = function (chunk, encoding, callback) {
    this.writeCallback = callback;
    this.parser.tokenizer.write(chunk.toString('utf8'), this.lastChunkWritten);
    this._runParsingLoop();
};

ParserStream.prototype.end = function (chunk, encoding, callback) {
    this.lastChunkWritten = true;
    WritableStream.prototype.end.call(this, chunk || '', encoding, callback);
};

//Scriptable parser implementation
ParserStream.prototype._runParsingLoop = function () {
    this.parser.runParsingLoopForCurrentChunk(this.writeCallback, this._scriptHandler);
};

Becca Bailey added 2 commits August 4, 2021 11:13
This must have been an existing bug - there was a misnamed function here
that was throwing an error with the new export syntax
@becca-bailey
Copy link
Contributor Author

@ryan-roemer @boygirl I added a node polyfill to the test config that seems to fix the issue in parser.js, and fixed a bug (I think this was pre-existing) where the wrong function name was being called from LabelHelpers. Once everything is passing I should be able to get this merged in today!

@becca-bailey becca-bailey merged commit a314d83 into main Aug 4, 2021
@becca-bailey becca-bailey deleted the improvement/replace-defualt-export branch August 4, 2021 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants