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

export-all conflicts with istanbul #5

Closed
IanSavchenko opened this issue Apr 7, 2018 · 6 comments
Closed

export-all conflicts with istanbul #5

IanSavchenko opened this issue Apr 7, 2018 · 6 comments
Assignees

Comments

@IanSavchenko
Copy link
Contributor

Jest mocks do not allow accessing vars from the outer scope, which exports is. Also, other weird errors occur, which I don't really want to dig into. Already wasted too much time on this...

One of the most simple and clean solutions IMO is to use different envs for dev and test in .babelrc. I managed to get it working pretty easy, since jest sets NODE_ENV=test and this one is used by Babel to pick config. We have this variable in our makefile, but it's not exported, so gets redefined by jest. Another level of control is to use BABEL_ENV variable, which has higher priority.

Let's discuss in the office how we want to tweak these vars to make it work properly.

@IanSavchenko IanSavchenko self-assigned this Apr 7, 2018
@andreineculau
Copy link
Contributor

Jest mocks do not allow accessing vars from the outer scope, which exports is.

please expand on the problem. give an example

@andreineculau
Copy link
Contributor

I understand only partially what you're experiencing

Footnotes:

  1. The problem is not in the transpilation of the src folder but the test folder, so adding a .babelrc inside the test disabling export-all should work

  2. I don't even know how come you trigger the export-all plugin inside a jest test module (which should have no export statements)

@IanSavchenko
Copy link
Contributor Author

Okay, so it went a bit deeper than I thought...
Will reply to your quotes first.

trigger https://github.com/facebook/jest/tree/6ee2a14b83393c9e3e3408beb5c4848489f04cf6/packages/babel-plugin-jest-hoist

and you presumably mock an entire module e.g. aws-sdk while referencing a shared top-level function, which babel rewrites as exports.

which can be fixed by the documented doMock function jestjs/jest#2567 (comment)

This can help sometimes when you don't need the hoisting mock behavior, but sometimes you need hoisting (you import tested module and it imports a mocked dependency). Cool thing is that jest allows to have vars prefixed with mock to be referenced in mocks. This kind of implies that you know what you are doing. So on our side in export-all we could add an option for this like ignorePrefix.

The problem is not in the transpilation of the src folder but the test folder, so adding a .babelrc inside the test disabling export-all should work

This works, but only for files in the test folder. Files in src folder will be transpiled with root .babelrc. With suggested workaround above, we could even keep one plugin config (root) across src and test. (Spoiler: further, you will see why it's not enough...)

I don't even know how come you trigger the export-all plugin inside a jest test module (which should have no export statements)

This exception we have only for linter rules, remember? For babel plugin, I don't think we should do this, because you never know if the state of AST tree and ExportDeclaration nodes in it are coming from the source code or were generated by another plugin. Probably, it is possible to know by analyzing original source code as string (you have this info in plugin), but I think it's brittle.

BUT... I was wrong when stated the problem in the title. The original problem (95% of all errors) is with coverage plugin Istanbul and that it ruins the AST somehow. I checked the code and they don't appear to be doing anything wrong, but somehow when the execution comes to our plugin, the tree is not correct and this causes same error as here istanbuljs/babel-plugin-istanbul#116. Instead of fixing a bug here, people actually suggest disabling faulting plugin in test env. Sad.

I really-really don't want to spend any more time on this problem, even though it's so fun to dig in jest/babel/istanbul 😂 Let's sit in the office when we are both in good health after sickness and decide something.

@andreineculau
Copy link
Contributor

🖖 i definitely don't want anyone to spend time like this

We settle it tmrw, even if we have a remote session

@andreineculau
Copy link
Contributor

I have now read your comment in peace. Let's just say bye-bye to this coverage crap, I say, but we talk tmrw.

@andreineculau
Copy link
Contributor

closing as it's a istanbul coverage issue

@andreineculau andreineculau changed the title export-all conflicts with jest mocks export-all conflicts with istanbul Jul 12, 2019
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

No branches or pull requests

2 participants