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 babel-jest instead of ts-jest when babel is used for compilation @nrwl/react #1720

Closed
Hotell opened this issue Aug 14, 2019 · 18 comments · Fixed by #3154
Closed

use babel-jest instead of ts-jest when babel is used for compilation @nrwl/react #1720

Hotell opened this issue Aug 14, 2019 · 18 comments · Fixed by #3154
Assignees
Labels
outdated scope: react Issues related to React support for Nx type: enhancement

Comments

@Hotell
Copy link
Contributor

Hotell commented Aug 14, 2019

Expected Behavior

yarn nx g @nrwl/react:app with-babel --babel=true --linter=eslint

should generate app that uses babel for compilation. eslint for linting and babel-jest for running transforms within jest instead of ts-jest which is deprecated and quite slow

Current Behavior

ts-jest is always used for libraries and apps

Context

Please provide any relevant information about your setup:

  • version of Nx used 8.4.8
  • 3rd-party libraries and their versions
  • and most importantly - a use-case that fails
@Hotell Hotell changed the title use babel-jest instead of ts-jest when babel is used for compilation @nrwl:react use babel-jest instead of ts-jest when babel is used for compilation @nrwl/react Aug 14, 2019
@jaysoo jaysoo self-assigned this Aug 15, 2019
@jaysoo
Copy link
Member

jaysoo commented Aug 15, 2019

@Hotell , great suggestion. I'll add this to our features list.

@Hotell
Copy link
Contributor Author

Hotell commented Aug 15, 2019

I highly recommend to add this "feature" to angular generators as well.

We recently migrated to pure babel-jest and the results are astonishing

image

@jaysoo jaysoo added this to the next milestone Aug 15, 2019
@vsavkin vsavkin added the scope: react Issues related to React support for Nx label Dec 12, 2019
@vsavkin vsavkin removed this from the next milestone Dec 12, 2019
@jbojcic1
Copy link
Contributor

jbojcic1 commented Dec 30, 2019

We are also having issue with this. Our tests stoped working once we introduced linguijs i18n library which uses babel macros.

@Hotell @jaysoo is there any way to extend or configure jest builder to use babel-jest with the same babel config that web build and dev-server builders are using or I have to write completely new builder?

@jaysoo
Copy link
Member

jaysoo commented Jan 2, 2020

@jbojcic1 You can swap out ts-jest with babel-jest in the jest.config.js file at your workspace root.

  transform: {
    '^.+\\.(ts|js|html)$': 'babel-jest'
  },

@wuichen
Copy link

wuichen commented Jan 2, 2020

@jaysoo are there other things i have to tweak? changing to babel-jest solely in the root folder breaks the test and gives me this error

● Validation Error:

Module babel-jest in the transform option was not found.
is: /Users/ichenwu/code/mercy/libs/board

@jaysoo
Copy link
Member

jaysoo commented Jan 2, 2020

@wuichen Make sure you also install babel-jest.

yarn add --dev babel-jest
# Or
npm install --save-dev babel-jest

Once we update our schematics to use babel-jest this would be done automatically for you.

@jaysoo jaysoo assigned Cammisuli and jaysoo and unassigned jaysoo Jan 2, 2020
@jaysoo
Copy link
Member

jaysoo commented Jan 2, 2020

Bringing in @Cammisuli as a second set of eyes on this. 👀

I haven't given this enough thought yet, but I definitely see a need to use babel-jest for React projects. We have two options:

  1. Add support for React apps/libs only (so Angular and other projects won't be affected / benefit from this).
  2. Add an option directly to @nrwl/jest schematics to use babel-jest, which can be used for non-React apps.

#1 would be the easiest option since the changes required are minimal.

For #2 to work, we'll have to pull in more dev dependencies, such as @babel/core and @babel/preset-typescript (which isn't a big deal). Also there are some TS features that won't work (See https://kulshekhar.github.io/ts-jest/user/babel7-or-ts), however these features are seldomly used so may not matter.

@Hotell
Copy link
Contributor Author

Hotell commented Jan 2, 2020

I'm in favour of option 2.

Why?

  • in our organization we don't use typescript for transpilation (because of speed and ecosystem mostly / frontend is react only - for now ), but we have also libs/apps in node...
  • also AFAIR there were various issues with transpiling vanilla web components in the past with typescript (so babel needed to be used)
  • while having babel-jest option for every ecocystem, addtions of other technologies might be easier in the future (vue, svelte)

@jbojcic1
Copy link
Contributor

jbojcic1 commented Jan 2, 2020

@jaysoo it's not enough to just swap ts-jest with babel-jest because currently nx is setting all babel plugins and presets in the webpack config as babel-loader options. When running tests with babel-jest webpack is not used.

I ended up creating .babelrc.js in my app root and specified babel presets and plugins there. There is one caveat though. If you have some plugins that you need when running the app but not when running the tests, those plugins have to be added to webpack babel-loader options by extending webpack config and injecting it there.

Regarding the potential options, I agree with @Hotell

@jaysoo
Copy link
Member

jaysoo commented Jan 3, 2020

@jbojcic1 Yes, you will unfortunately have to duplicate babel settings for now.

It sounds like supporting --babel option for @nrwl/jest would be the best option. Angular apps can continue using ts-jest, and other projects can switch to babel-jest for better compatibility with their ecosystem.

@jbojcic1
Copy link
Contributor

@jaysoo what is the status of this? perhaps I could give it a try if it hasn't been started yet?

@stramel
Copy link
Contributor

stramel commented Feb 5, 2020

When I used babel-jest it seemed to break the resolution of typescript paths

@jaysoo
Copy link
Member

jaysoo commented Mar 12, 2020

@jbojcic1 If you want try it out please feel free

We will need to investigate further, and unfortunately we currently have other issues taking priority.


Contribution notes

Once you've made your changes you can test out with:

yarn create-playground

This will take a while, but eventually you can cd into tmp/nx/proj and try adding some apps and libs.

yarn nx g @nrwl/react:app my-app
yarn nx g @nrwl/react:lib my-lib

To update the playground when you make changes to Nx, run:

yarn update-playground

@github-actions github-actions bot added the stale label May 29, 2020
@stramel
Copy link
Contributor

stramel commented May 29, 2020

This is still an issue AFAICT

@nrwl nrwl deleted a comment from github-actions bot May 29, 2020
@FrozenPandaz
Copy link
Collaborator

Hi, sorry about this.

This was mislabeled as stale. We are testing ways to mark not reproducible issues as stale so that we can focus on actionable items but our initial experiment was too broad and unintentionally labeled this issue as stale.

@jaysoo jaysoo removed their assignment Jun 8, 2020
@jaysoo jaysoo assigned BigAB and unassigned Cammisuli Jun 8, 2020
@Hotell
Copy link
Contributor Author

Hotell commented Jun 8, 2020

This was already implemented so I should probably close this #2722
WDYT @FrozenPandaz ?

@BigAB
Copy link
Contributor

BigAB commented Jun 11, 2020

I've added a PR to make this the default behaviour when generating a project (app or lib) with the @nrwl/react schematic.
#3154

@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: react Issues related to React support for Nx type: enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants