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

Use rollup for browser builds #3532

Merged
merged 6 commits into from
Jun 28, 2017
Merged

Conversation

skovhus
Copy link
Contributor

@skovhus skovhus commented May 9, 2017

Summary

In order to provide browser versions of relevant Jest packages (#3360) we've tried:

Here we actually do a proper rollup build including all dependencies and handle packages expecting node globals like process. Similar to what prettier does (https://github.com/prettier/prettier/blob/master/docs/rollup.config.js).

screenshot 2017-05-09 22 18 15

Test plan

I've added an automated test that runs these packages in a browser.

After merging, I would like Jest beta build, so I can test this on the repos I'm trying to convert to Jest + Expect using jest-codemods. skovhus/jest-codemods#39

@skovhus skovhus changed the title Use rollup for browser build to include all dependencies Use rollup for browser builds May 9, 2017
scripts/build.js Outdated
if (browser.indexOf(BUILD_ES5_DIR) !== 0) {
throw new Error(
`browser field for ${pkgJsonPath} should start with "${BUILD_ES5_DIR}"`
async function buildBrowserPackages(packages) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using async/await and for of loops here in order for getting a nice stdout output similar to the node builds.

Copy link
Contributor Author

@skovhus skovhus May 9, 2017

Choose a reason for hiding this comment

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

The current appveyor with node 6 doesn't like this... So should we upgrade the nodejs_version to 7 or should I just get rid of async/await?

async function buildBrowserPackages(packages) {
      ^^^^^^^^
SyntaxError: Unexpected token function

Copy link
Contributor Author

@skovhus skovhus May 9, 2017

Choose a reason for hiding this comment

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

I just removed it...

@skovhus skovhus force-pushed the browser-support-v3 branch 2 times, most recently from 3c45bcc to d3ed28b Compare May 9, 2017 20:43
@codecov-io
Copy link

codecov-io commented May 9, 2017

Codecov Report

Merging #3532 into master will decrease coverage by 0.02%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3532      +/-   ##
==========================================
- Coverage   58.09%   58.07%   -0.03%     
==========================================
  Files         195      195              
  Lines        6751     6747       -4     
  Branches        6        6              
==========================================
- Hits         3922     3918       -4     
  Misses       2826     2826              
  Partials        3        3
Impacted Files Coverage Δ
packages/jest-matchers/src/to_throw_matchers.js 77.55% <ø> (-0.45%) ⬇️
packages/jest-matchers/src/spy_matchers.js 95.08% <ø> (-0.08%) ⬇️
packages/jest-matchers/src/matchers.js 99.37% <ø> (-0.01%) ⬇️
packages/jest-matchers/src/jasmine_utils.js 79.62% <ø> (-0.19%) ⬇️
...matchers/src/extract_expected_assertions_errors.js 7.14% <ø> (-6.2%) ⬇️
packages/jest-matchers/src/jest_matchers_object.js 77.77% <100%> (-2.23%) ⬇️
packages/jest-matchers/src/utils.js 96% <100%> (-0.08%) ⬇️
packages/jest-matchers/src/asymmetric_matchers.js 90.54% <100%> (+0.39%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c8a5135...37b01bf. Read the comment docs.

@SimenB
Copy link
Member

SimenB commented May 10, 2017

Nice! I don't know how much work adding a few browser tests (using mocha, along with jest matchers and jest mocks?) running with karma or something would be? I think that needs to be included in the Jest repo to avoid unknowingly breaking it in the future.

@skovhus
Copy link
Contributor Author

skovhus commented May 10, 2017

Setting up the tests is rather easy, but we need infrastructure to run the tests on a CI.

@skovhus
Copy link
Contributor Author

skovhus commented May 10, 2017

For example sauce labs or browser stack. A docker image with a headless browser is also a possibility. Not sure if Jests CI supports that. : )

@quantizor
Copy link
Contributor

@skovhus Travis has some recommendations for headless testing that can be done without another third party service: https://docs.travis-ci.com/user/gui-and-headless-browsers/#Using-xvfb-to-Run-Tests-That-Require-a-GUI

@skovhus
Copy link
Contributor Author

skovhus commented Jun 2, 2017

@probablyup @SimenB I added some Karma based tests... Works on CI! 👍

Any thoughts/wishes on where to place these tests? And do you want a separate package.json to contain all the karma related things?

Another things: I removed some of the browser/es5 builds that I added earlier. I think it might be enough for now to support jest-mock and jest-matchers.

@skovhus
Copy link
Contributor Author

skovhus commented Jun 12, 2017

@SimenB @cpojer let me know if anything is missing here...

@skovhus
Copy link
Contributor Author

skovhus commented Jun 23, 2017

@aaronabramov, a few open questions:

  • anything missing before this can be merged?
  • any thoughts/wishes on where to place the browser tests? And do you want a separate package.json to contain all the karma related things?

@SimenB
Copy link
Member

SimenB commented Jun 23, 2017

No idea how I lost track of this. Awesome that you were able to add a browser test! Really looking forward to replacing chai and sinon in the browser tests we've got at work.


it('runs mocks', function() {
var someMockFunction = mock.fn();
expect(someMockFunction.mock.calls.length).toBe(0);
Copy link
Member

@SimenB SimenB Jun 23, 2017

Choose a reason for hiding this comment

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

expect(someMockFunction).not.toHaveBeenCalled();?

@cpojer
Copy link
Member

cpojer commented Jun 27, 2017

Do you mind rebasing this one?

@skovhus skovhus force-pushed the browser-support-v3 branch 3 times, most recently from 503a03d to 49ddf7a Compare June 27, 2017 12:13
@skovhus
Copy link
Contributor Author

skovhus commented Jun 27, 2017

@cpojer sure... I gave it a quick try, but #3778 gives me some issues do to some bable configuration.

Error: 'getMatchers' is not exported by packages/jest-matchers/src/jest_matchers_object.js

Will look more into this at some point. But ideas are more than welcome @SimenB

Also let me know your thoughts on the two questions above (#3532 (comment)).

@SimenB
Copy link
Member

SimenB commented Jun 27, 2017

Hmm, we might have to convert to export as well. Breaking change for programmatic usage though...

Started on this, but it doesn't really scale:

diff --git i/scripts/browserBuild.js w/scripts/browserBuild.js
index e0c6a5ee..570d5630 100644
--- i/scripts/browserBuild.js
+++ w/scripts/browserBuild.js
@@ -47,7 +47,31 @@ function browserBuild(pkgName, entryPath, destination) {
     plugins: [
       rollupFlow(),
       rollupJson(),
-      rollupCommonjs(),
+      rollupCommonjs({
+        namedExports: {
+          'packages/jest-matchers/src/jasmine_utils.js': [
+            'equals',
+            'fnNameFor',
+            'hasProperty',
+            'isA',
+            'isUndefined',
+          ],
+          'packages/jest-matchers/src/jest_matchers_object.js': [
+            'getMatchers',
+            'getState',
+            'setMatchers',
+            'setState',
+          ],
+        },
+      }),
       rollupBabel(babelEs5Options),
       rollupGlobals(),
       rollupBuiltins(),

scripts/build.js Outdated
p.split('/').pop(),
path.resolve(srcDir, 'index.js'),
path.resolve(p, browser)
).then(() => {
Copy link
Member

Choose a reason for hiding this comment

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

can you add a catch here?

(node:21659) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: 'getMatchers' is not exported by packages/jest-matchers/src/jest_matchers_object.js vs

{ Error: 'getMatchers' is not exported by packages/jest-matchers/src/jest_matchers_object.js
    at error (/Users/simbekkh/repos/jest/node_modules/rollup/dist/rollup.js:170:12)
    at Module.error$1 [as error] (/Users/simbekkh/repos/jest/node_modules/rollup/dist/rollup.js:8007:2)
    at Module.trace (/Users/simbekkh/repos/jest/node_modules/rollup/dist/rollup.js:8106:9)
    at ModuleScope.findDeclaration (/Users/simbekkh/repos/jest/node_modules/rollup/dist/rollup.js:7691:22)
    at Scope.findDeclaration (/Users/simbekkh/repos/jest/node_modules/rollup/dist/rollup.js:5497:39)
    at CallExpression.bind (/Users/simbekkh/repos/jest/node_modules/rollup/dist/rollup.js:6022:28)
    at /Users/simbekkh/repos/jest/node_modules/rollup/dist/rollup.js:5291:50
    at VariableDeclarator.eachChild (/Users/simbekkh/repos/jest/node_modules/rollup/dist/rollup.js:5308:5)
    at VariableDeclarator.bind (/Users/simbekkh/repos/jest/node_modules/rollup/dist/rollup.js:5291:7)
    at /Users/simbekkh/repos/jest/node_modules/rollup/dist/rollup.js:5291:50
  code: 'MISSING_EXPORT',
  url: 'https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module',
  pos: 1321,
  loc:
   { file: '/Users/simbekkh/repos/jest/packages/jest-matchers/src/index.js',
     line: 31,
     column: 29 },
  frame: '29: import { equals } from \'./jasmine_utils\';\n30: import { any, anything, arrayContaining, objectContaining, stringContaining, stringMatching } from \'./asymmetric_matchers\';\n31: import { getState, setState, getMatchers, se
tMatchers } from \'./jest_matchers_object\';\n                                 ^\n32: import extractExpectedAssertionsErrors from \'./extract_expected_assertions_errors\';' }
diff --git i/scripts/build.js w/scripts/build.js
index 5f73065c..88d084ce 100644
--- i/scripts/build.js
+++ w/scripts/build.js
@@ -94,7 +94,11 @@ function buildBrowserPackage(p) {
     ).then(() => {
       process.stdout.write(adjustToTerminalWidth(`${path.basename(p)}\n`));
       process.stdout.write(`${OK}\n`);
-    });
+    })
+      .catch(e => {
+        console.error(e);
+        process.exit(1);
+      });
   }
 }

var someMockFunction = mock.fn();
expect(someMockFunction.mock.calls.length).toBe(0);
someMockFunction();
expect(someMockFunction.mock.calls.length).toBe(1);
Copy link
Member

Choose a reason for hiding this comment

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

expect(someMockFunction).toHaveBeenCalled(); or expect(someMockFunction).toHaveBeenCalledTimes(1);

@cpojer
Copy link
Member

cpojer commented Jun 27, 2017

I prefer not to use exports for now as this would be another large codemod. Could we use it only for jest-matcher-utils for now and its dependencies? I'd prefer it if there was a way to make it work with current ES module imports that don't use babel as well – is there such a plugin for babel except the default one?

@SimenB
Copy link
Member

SimenB commented Jun 27, 2017

Could we use it only for jest-matcher-utils for now and its dependencies?

"It" as in export or continue with the whitelisting in the rollup plugins config?

@cpojer
Copy link
Member

cpojer commented Jun 27, 2017

export.

@@ -69,4 +69,4 @@ const extractExpectedAssertionsErrors = () => {
return result;
};

module.exports = extractExpectedAssertionsErrors;
export default extractExpectedAssertionsErrors;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cpojer what this what you proposed? : )

@skovhus
Copy link
Contributor Author

skovhus commented Jun 27, 2017

@cpojer @SimenB I've added named exports for jest-matchers and addressed the review comments.

@cpojer cpojer merged commit ac25993 into jestjs:master Jun 28, 2017
@cpojer
Copy link
Member

cpojer commented Jun 28, 2017

Awesome.

@cpojer
Copy link
Member

cpojer commented Jun 28, 2017

Published jest-matchers@20.1.0-alpha.1.

@skovhus skovhus deleted the browser-support-v3 branch June 28, 2017 12:27
@skovhus
Copy link
Contributor Author

skovhus commented Jun 28, 2017

Thanks for merging! Exciting 🌟

Let us try it in the wild!

@skovhus
Copy link
Contributor Author

skovhus commented Jun 28, 2017

@cpojer I guess once this is tested and we are happy, then the documentation should be updated.

@kentcdodds
Copy link
Contributor

documentation should be updated.

Yes, I'd like to try this out, but I'm not sure where to start 😅

@skovhus
Copy link
Contributor Author

skovhus commented Jun 28, 2017

😉

@bestander
Copy link
Contributor

@skorvus, I lack some context here, could you help me out?
I rebased workspace PR #3906 on master and now it uses Yarn Workspaces to install node_modules instead of Lerna.

The result is that rollup browser builds don't transpile ES6 constructs correctly

/home/ubuntu/jest/packages/jest-matchers/build-es5/index.js
  4593:1  error  Parsing error: The keyword 'const' is reserved

Any idea why running node ./scripts/build.js would not get buildBrowserPackage to produce the same bundle?

Technically the only difference is that symlinks to other workspaces are not in jest-matchers/node_modules but are in ../../node_modules

@skovhus
Copy link
Contributor Author

skovhus commented Jul 4, 2017

Let us discuss this in your PR.

tushardhole pushed a commit to tushardhole/jest that referenced this pull request Aug 21, 2017
* Remove browser builds for sub dependencies

* Add karma browser tests for es5 builds

* Use rollup for browser builds

* Review comment + prettier

* Fix rollup build by using export for jest-matchers

* Run prettier on unrelated file
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants