cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
47.02k stars 3.18k forks source link

Add support for customizing babel configuration #343

Closed chrisbreiding closed 6 years ago

chrisbreiding commented 7 years ago

Add support for specifying babel presets and plugins, so users can override the default ones.

Implementation notes:

Potentially auto-load a .babelrc in the project root. Keep in mind that a user could have an older version of babel installed for their project that isn't compatible with Cypress's version.

Support having a .babelrc in the cypress directory that is preferred over one in the project root. Also, explore if there's a need/use case for specifying the preset/plugins in cypress.json under a babel namespace.

Write documentation detailing the current defaults and how to override them.

paulfalgout commented 7 years ago

This would be an excellent addition. We have a babel plugin specifically for our unit tests which lets us use a couple of webpack aliases in our unit tests without loading webpack. Once we have .babelrc support we can possibly move at least some of our unit tests into cypress. We're interested in doing this as we're finding we're doing some duplicate work setting up stubs, and it is wildly easier to setup complicated scenarios in cypress than straight mocha/chai/sinon.

rdooh commented 7 years ago

Looking to use something like this: https://www.npmjs.com/package/babel-root-import

in essence, what I want to be able to do is to access some util helpers with something like import { someHelper } from '~/support/utils'; instead of import { someHelper } from '../../../support/utils';, where we might want to have our specs in nested dirs for organization purposes.

Current attempt fails, perhaps because I'm unable to get this plugged in

paulfalgout commented 7 years ago

I do something very similar to what you are saying using babel-plugin-webpack-alias

kentcdodds commented 7 years ago

Chiming in with my use-case. I have some stage features of JS that aren't available in Cypress (like Object Spread). Would love to have the ability to specify a custom .babelrc :)

nshoes commented 7 years ago

@kentcdodds Came here to say the same thing! +1

adamdicarlo commented 7 years ago

Use case on my work project is accessing constants and helper functions (like Redux selectors) in the actual project from the Cypress tests.

This goes beyond configuring babel itself, though, into bundler configuration. Because using ES2015+ code from node_modules is still a messy business, and requires a shouldBabel function.

Here's what my app needs:

    rules: [
      {
        test: /\.jsx?$/,
        include: ['<path to app source>', shouldBabel /* <- custom function */],
        use: ['babel-loader?cacheDirectory']
      },
brian-mann commented 6 years ago

Fixed by https://github.com/cypress-io/cypress/pull/888.

kentcdodds commented 6 years ago

Well this is a big deal :tada: Thanks!

brian-mann commented 6 years ago

Fixed in 1.1.0.

chrisbreiding commented 6 years ago

Check out how to modify the default options of the built-in browserify preprocesor or you may find the webpack preprocessor a better fit if you're already using webpack.