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

fix: don't run test/*.spec.js files automatically in Browsers #202

Closed
wants to merge 1 commit into from

Conversation

vmx
Copy link
Member

@vmx vmx commented Mar 1, 2018

For Browsers we use webpack. Karma will create a bundle for every file
that is specified as entry point. This may take lots of resources (see
ipfs-inactive/js-ipfs-http-client#683 for more information).

The solution is to have only a single entry point, test/browser.js,
which will then be responsible to create the bundle with all the tests.

BREAKING CHANGE: Without bundling all tests in test/browser.js the
tests might not run. The bundling can be done like this:

// This is webpack specific. It will create a single bundle
// out of all files ending in ".spec.js" within the "test"
// directory and all its subdirectories.
'use strict'
const testsContext = require.context('.', true, /\.spec\.js/)
testsContext.keys().forEach(testsContext)

For Browsers we use webpack. Karma will create a bundle for every file
that is specified as entry point. This may take lots of resources (see
ipfs-inactive/js-ipfs-http-client#683 for more information).

The solution is to have only a single entry point, `test/browser.js`,
which will then be responsible to create the bundle with all the tests.

BREAKING CHANGE: Without bundling all tests in `test/browser.js` the
tests might not run. The bundling can be done like this:

    // This is webpack specific. It will create a single bundle
    // out of all files ending in ".spec.js" within the "test"
    // directory and all its subdirectories.
    'use strict'
    const testsContext = require.context('.', true, /\.spec\.js/)
    testsContext.keys().forEach(testsContext)
@codecov
Copy link

codecov bot commented Mar 1, 2018

Codecov Report

Merging #202 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #202   +/-   ##
=======================================
  Coverage   78.16%   78.16%           
=======================================
  Files           6        6           
  Lines         142      142           
=======================================
  Hits          111      111           
  Misses         31       31

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 57c9554...0fe34b5. Read the comment docs.

@daviddias
Copy link
Member

@vmx this is such a great find!!!!! ❤️ I say let's remove .spec.js all together and use node.js and browser.js as the entry points

@vmx
Copy link
Member Author

vmx commented Mar 1, 2018

@diasdavid Well, my change makes browser.js the only entry point, though I don't think it's ideal. For webpack it will be just a file that globs all .spec.js files together. So it's the same as before (which I think makes sense), but sadly with another file. I wished it was possible differently, but I didn't find a way in Karma.

@daviddias
Copy link
Member

@dignifiedquire ^^

@vmx
Copy link
Member Author

vmx commented Mar 2, 2018

I've uploaded an alternative idea: f7457f4 This illustrates what I was after, but it is really hacky and doesn't even work in all cases. I guess it could be done with a custom Karma preprocessor, but I'm not sure if it's worth it.

@dignifiedquire
Copy link
Member

Really this should be fixed in karma-webpack, e.g. there should be a config option for karma-webpack to either bundle all files or not. These hacks will work for some time and then break :(

I think the alternative is the better approach and you can probably use some node builtins to search for the correct location of when executing the test, e.g injecting the correct files in src/test/browser-config.js. Also that file should probably bundle browser.js as well, as you would otherwise end up with two bundles again. And another note, it should respect the files option, so you need to dynamically inject all files that were requested on there

@vmx
Copy link
Member Author

vmx commented Mar 2, 2018

@dignifiedquire If it's technically possible with Karma, that would be cool. Does it mean that a preprocessor can merge/bundle several files and Karma will see them as a single one?

@@ -14,8 +14,7 @@ function getPatterns (ctx) {
}

return [
'test/browser.js',
'test/**/*.spec.js'
'test/browser.js'
Copy link
Member

Choose a reason for hiding this comment

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

This change as it is will break the assumption on tests everywhere.

To make this happen, we need to make it consistent (either use .spec for node and browser or for none) and we need updated docs.

@vmx
Copy link
Member Author

vmx commented Mar 3, 2018

After spending a lot of time of thinking and trying I think I found a viable, backwards compatible solution to the problem: f694aa6 (it's not 100% working yet, but I'm really close).

@JonKrone
Copy link

JonKrone commented Mar 3, 2018

f694aa6

If a project does define a browser.js file, should we then use just that in deference to whatever custom code the project has there? i.e. treat browser.js and *.spec.js as mutually exclusive

@vmx
Copy link
Member Author

vmx commented Mar 4, 2018

I'd like to close this one in favour of #203.

@daviddias daviddias closed this Mar 5, 2018
@daviddias daviddias deleted the karma-webpack-issue branch March 5, 2018 08:49
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.

4 participants