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

Transpile dependencies of the test file #111

Closed
madbence opened this issue Oct 27, 2015 · 58 comments
Closed

Transpile dependencies of the test file #111

madbence opened this issue Oct 27, 2015 · 58 comments

Comments

@madbence
Copy link

ava@0.3 works smoothly on a clean project, but fails to run on mine (tests broke when i've upgraded to from ava@0.2):

(function (exports, require, module, __filename, __dirname) { import {
                                                              ^^^^^^
SyntaxError: Unexpected reserved word

I'm sure it's caused by some weird babel dependency, so it might be unrelated to ava. Last working state is 7d31571, if i try to upgrade to ava@0.3, tests are broken.
I will investigate it tomorrow...

@Qix-
Copy link
Contributor

Qix- commented Oct 27, 2015

Did you try rm -rf node_modules and npm i?

What version of Node are you using? Something tells me Babel was removed from compiling tests.

@madbence
Copy link
Author

yup, still the same, btw it might be the same as #108 ?

@Qix-
Copy link
Contributor

Qix- commented Oct 27, 2015

Yep, that's my guess.

@madbence
Copy link
Author

So right now there is no way to test modules directly? I mean, i have to transpile them first, and test the transpiled files?

@sindresorhus
Copy link
Member

Implicitly transpiling dependencies in 0.2.0 was a bug that was later fixed 0.3.0. Transpiling dependencies by default is both surprising and slow. I'm open to considering an option for enabling this, but it will not be by default.

@sindresorhus sindresorhus changed the title ES2015 broken (?) Transpile dependencies of the test file Oct 28, 2015
@sindresorhus sindresorhus added the enhancement new functionality label Oct 28, 2015
@madbence
Copy link
Author

what about allowing something like $ babel-node node_modules/.bin/ava ? i'm not really familiar with ava internals, so that's just an idea.

@sindresorhus
Copy link
Member

That won't work as we spawn the test files. Just use AVA 0.2.0 until we've figured out how to deal with this. Alternatively use a fork where you've removed this line.

@axross
Copy link

axross commented Oct 30, 2015

I'm open to considering an option for enabling this, but it will not be by default.

I agree. I want to run both tests and target codes for tests with Babel. I think that is proper and as it should be.

@sindresorhus
Copy link
Member

What should the option be named?

@sindresorhus
Copy link
Member

@floatdrop Any thoughts as to how we should solve this now that #103 has landed?

@floatdrop
Copy link
Contributor

@madbence you can require file with following code in tests as workaround:

// app.es5.js
require('babel/register');
require('./app');

@sindresorhus besides this workaround, not really.

@sindresorhus
Copy link
Member

We could set the execPath, when we fork a test file, to the babel-node executable instead of vanilla node.

hawkrives added a commit to hawkrives/gobbldygook that referenced this issue Nov 5, 2015
full conversion pending on ava transpiling depended files
(see avajs/ava#111)
@imevro
Copy link

imevro commented Nov 8, 2015

Hm, any progress on that? Still can't use external files.

@sindresorhus
Copy link
Member

@theaqua This issue is still open, so no. @floatdrop has already provided a workaround if you can't wait.

To move this issue forward, come up with a good name for a CLI flag to enable it.

@imevro
Copy link

imevro commented Nov 9, 2015

pm2 uses execInterpreter option. I think it's ok because most users will install custom interpreter (coffeescript or babel) as dependency and ava can search in local node_modules automagically.

@bookercodes
Copy link
Contributor

@theaqua If I understand correctly, that is what Mocha does too.

@madbence
Copy link
Author

madbence commented Nov 9, 2015

@sindresorhus

  • --transpile-all|-a
  • --compile-all|-a
  • or to be more flexible, do something like --compilers js:babel/register (like in mocha, istanbul, etc)

@bookercodes
Copy link
Contributor

@madbence I like --transpile-all.

I think --transpile-transitive is more descriptive/accurate, though.

Then again, "transpile" a general term. As AVA specifically transpiles using Babel, maybe "babelify" would be a better word.

@Qix-
Copy link
Contributor

Qix- commented Nov 9, 2015

--transpile-transitive would be the longest command line flag I've seen in a while.

@bookercodes
Copy link
Contributor

@Qix- -tt for short

@Qix-
Copy link
Contributor

Qix- commented Nov 9, 2015

Per GNU specs on arguments, -tt would be supplying the short-form argument -t twice. Doesn't make sense. --transpile-all would make the most sense whilst still being correct. --compile-all isn't technically correct but would still probably be easier for people to type.

@madbence
Copy link
Author

madbence commented Nov 9, 2015

btw what's the rationale behind this behavior (ava transpiles tests only)? it's just slow?

@bookercodes
Copy link
Contributor

@madbence #50

@jamestalmage
Copy link
Contributor

It is more than just "ava transpiles tests only".

It is that ava allows you to write tests in ES2015 with zero impact on your production environment.
By zero impact, I mean no transpilation an no polyfills. (IMO polyfills are the sneakier harder to detect bug).

For example, we make sure Promise is available inside your tests, without impacting it's availability in production code.

// Always works in tests. Only works in production in Node >= 0.12
new Promise(...)

Trickier still is a prototype polyfill. Something like String.prototype.includes(). If Babel actually modifies the prototype, your test suite no longer protects you against mistakenly using str.includes() in production. Users will report errors in Node 0.12 that you can't write a failing test case for (because it has been polyfilled even in your production environment).

AVA uses the runtime plugin to provide polyfill aliasing. An example of how that works is here. This means that:

".... some string ....".includes()

Will work in your tests on any platform. But will throw in production code on platforms that do not support str.includes()

@billyjanitsch
Copy link
Contributor

+1 for a --compilers (or perhaps --preprocessors) flag that lets you use custom register hooks, like mocha does (as @alexbooker alluded to) with a pretty clean syntax. Other test runners (Karma, babel-tape-runner) do similar things.

Including import babel-core/register at the top of every test file is clunky enough, and gets much worse if you want to write a custom compiler (e.g. like this, which is necessary to test JS that uses CSS modules). You should be able to specify this when invoking AVA.

@sparty02
Copy link
Contributor

import babel-core/register is a really good, simple workaround until this feature lands. @sindresorhus what do you think about putting this in the README until it's officially addressed? (I wouldn't mind submitting a PR for it)

@sindresorhus
Copy link
Member

@sparty02 Sure, PR welcome.

@franleplant
Copy link

Hi!
import 'babel-core/register' and import 'babel-register' are not working for me.

@jamestalmage
Copy link
Contributor

You need to install as dependencies of your own project to use outside of tests

@franleplant
Copy link

Already done that @jamestalmage

@franleplant
Copy link

It was purely a problem with babel presets/plugins/config settings, nvm, thanks a lot

@ariporad
Copy link
Contributor

EDIT: Whoops... Not the issue I thought it was. Sorry!

@ariporad ariporad reopened this Dec 29, 2015
@franleplant
Copy link

:)

@Hurtak
Copy link
Contributor

Hurtak commented Dec 29, 2015

for anybody trying to make import 'babel-core/register'; work in AVA, try installing babel-core 5.x.x, that did the trick for me, 6 doesn't work for some reason (you probably need have properly configured .babelrc in your directory or something)

@ariporad
Copy link
Contributor

@Hurtak: Yes. Babel 6 doesn't do anything by default.

@kentcdodds
Copy link
Contributor

This issue is solved for me by adding --require 'babel-register' (Babel 6) with a .babelrc file:

{
  "presets": ["es2015", "stage-2"]
}

@kentcdodds
Copy link
Contributor

If you're interested, here's a migration from Mocha to AVA in a small library: https://github.com/kentcdodds/starwars-names/pull/12/files?w=1

edit the reason I bring it up is because it supports ES6 with Babel 6 in my source without having to import babel-register. Thought you'd all be interested.

@jamestalmage
Copy link
Contributor

Cool!

Your use of the --require flag is spot on for what we intended. #407 should make the experience even better.

We would value any feedback on pain points you encountered during the transition.

@kentcdodds
Copy link
Contributor

Cool, I added a comment about that. I prefer to use config to avoid polluting the package.json keyspace :-) (It's also technically the recommended way to do what is being done).

Not really any pain points. The only thing that I would say is the assertions are pretty limiting. I prefer the declarative syntax of something like Chai. But I expect that the small surface area of the API is intentional. I ended up bringing in another utility library to make testing a little more declarative though.

@azhang
Copy link

azhang commented Jan 14, 2016

This is what I use in my package.json file:

  "scripts": {
    "test": "ava --require babel-register --require babel-polyfill '**/*.test.js'"
  },
  "dependencies": {
    "babel-core": "^6.4.0",
    "babel-polyfill": "^6.3.14",
    "babel-preset-es2015": "^6.3.13",
    "babel-preset-stage-0": "^6.3.13",
    "babel-register": "^6.3.13"
  },
  "devDependencies": {
    "ava": "^0.9.2"
  },
  "babel": {
    "presets": [
      "es2015",
      "stage-0"
    ],
  },

@ruyadorno
Copy link

Had to dig around this issue this morning to get it working with babel@6.5.2 in order to run on travis with node@0.12.10 and npm@2.14.9

I had to use the ignore babel config property that somehow went unmentioned in this whole thread, here is the actual snippet of code that I dropped at the top of my test file:

require('babel-core/register')({
    ignore: false
});

TIP: ignore can also be a function that receives a filename and returns false, this way you can filter only the files that need the transpiling, I ended up using it and test times were a lot better

@novemberborn
Copy link
Member

@sindresorhus @vdemedes @jamestalmage is there anything we want to improve here beyond the existing --require support? If not I'll close and remove the link to this issue from the README.

@sindresorhus
Copy link
Member

Not that I'm aware of.

@vadimdemedes
Copy link
Contributor

Sure, we can close it for now.

@z-vr
Copy link

z-vr commented Dec 26, 2016

@Whoaa512 dude you rule

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

No branches or pull requests