-
Notifications
You must be signed in to change notification settings - Fork 12k
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
Feature request: Support using Jest testing framework #4543
Comments
We recently tried that out in a React project. It seemed pretty cool but we had trouble getting it to work on Windows. Has anyone had better luck? |
@kuncevic, I would suggest seeing if https://github.com/angular/zone.js/blob/master/lib/mocha/mocha.ts I know @hansl has mentioned he would only like to support one testing framework, |
I looked into this before as jest is really awesome, unfortunately it only runs on node so there isn't a way of getting it to run with karma in the browser. There's an open issue for it here: jestjs/jest#848 Due to the way angular abstracts the DOM it might be possible to get it working, but it would be a big difference from the current test setup. |
I have set up a demo project here https://github.com/beeman/angular-cli-jest and the first impression is that it works fine. I did not explore any advanced feature yet though. |
So it's currently possible to use Jest with Angular, and it's pretty forward to add/migrate thanks to jest-preset-angular. I've made a blog post about this very topic, thought you might be interested. What @beeman proposed is half a solution. Jest also needs to run in Zone.js context, otherwise testing components will fail. Although it would be great, if we could bake it into the cli and make it an option while bootstrapping somehow. |
@thymikee your preset looks great, I'm about to test it. I don't think Jest will find it's way into the CLI any time soon. It might be a nice thing to look at when the CLI starts supporting custom blueprints. |
This is a great candidate for a blueprint! |
This blog post has just appeared which apparently goes over using Jest in CLI 1.0.0: https://www.xfive.co/blog/testing-angular-faster-jest/ Just ran some speed tests on ~400 tests: Karma: 0m48.999s Edit: Just tested watch mode and it's brilliant. You can filter by filename or test name via patterns and every time it detects a change, it'll only run the tests from those matched files. Also, it's able to see what files were last changed via git and only run tests against changed files, so you know within seconds when you've broken a test as opposed to running the whole suite on any change. ne file of tests: Time: 1.536s, estimated 2s: |
I eagerly tried ng-cli 1.0 default project with xfive.co and jest-preset-angular changes described, also tried https://github.com/beeman/angular-cli-jest and none was working on osx/node7.8. Did I miss anything? |
@vecernik please describe what went wrong on jest-preset-angular issue tracker, we'll see what can be done :) |
I tried Jest with instructions from @thymikee's post (which uses jest-preset-angular), and it worked just fine. Let me share my findings: Result Summary
Required Changes
Gains
Issues
|
@Meligy thanks for your input, really appreciate it! You should definitely time the execution of whole process (in my case it's much faster overall).
That's right. It's because polyfills are also for browsers, and Jest runs through Node, and hence we need
Working on it! thymikee/jest-preset-angular#17
You're not being ignorant, as for now Jest unfortunately doesn't integrates well with browsers, but it may change in future (but not so soon) |
Aye, when you use I love that it shows my slow tests. Actually, I've noticed that components with large views and a lot of form elements are super slow and was wondering if there's any way to speed those up Example, a registration-form component with about 10 Form Controls and 100 lines of HTML takes about 10 seconds to run through all my assertions.
|
@thymikee Thanks heaps man. I was planning to look into making the RxJS change in the project in a few days. You did it and released in a few hours. Super! @intellix I haven't timed fully yet (after easter, as it's on a different machine), but yes, they feel overall the same total time. Also, I had the same results around which tests take longer time. Container components test seem to be the slowest. I'm not sure yet whether it's compiling all the child components or interacting with the debug DOM that causes most time, but it's quite slow. Some tests go for 15 seconds or more. I suspect it's the DOM, you know, the jsdom emulation layer, is much slower than the real thing (at least Chrome). |
Also, be careful with the fact that protractor is still using jasmine, so when using you favourite IDE, it's possible that you either end up with jest assertions functions being proposed by the IDE in e2e tests, or if you leave This is more a limitation of the IDE than angular, but the problem exists still :) |
FYI, it is easily workaroundable by specifying |
Generated an Angular CLI 1.0.0 project and created a commit to show the required changes to get Jest working in your projects for anyone who hasn't made the leap yet :) intellix/angular-cli-jest@07829f5 |
@intellix you can also remove karma and related dependencies I think. This is how I did it in the end: https://gitlab.com/linagora/petals-cockpit/commit/91955c8d5d2209c2bcd19cb1d3050eef23b68f55 It is quite similar, but also update the |
I had a go myself and there is a big blocker unfortunately... there doesn't seem to be a way to add the the webpack build chain we use to Jest, instead it uses a custom transform to inline templates. This is a problem because a lot of functionality we enable in the CLI depends heavily on webpack processing. For instance, correct scss compilation. Having a different build system for test mean it might work on some cases, maybe it doesn't on others, and it needs double the work for changes. We had this for a while with Karma actually, and it was the source of a lot of insidious bugs. So for now I'm sorry to say Jest is not really a viable option to fully integrate into the CLI. |
@filipesilva thanks for the detailed feedbadk! I'm not clear when it is problematic to use jest, when testing the angular component that uses scss? Is that it? |
It's problematic whenever your code uses functionality that depends on the CLI build system, rather than anything that's just part of TypeScript+html/css inlining (which the Jest preset does in a regex based way). SCSS compilation would be problematic, yes. |
I also ran into issues attempting to get this setup. Some quick notes on roadblocks.
Jest is a really great idea, and the feedback loop for writing tests is awesome. Hopefully with headless Chrome hitting mainstream there will be some advances in Jasmine to get closer to the parity of Jest. I agree with @filipesilva. I don't think its ready for the masses. |
Ey, any update on this? Is it possible to use Jest with angular-cli? |
@davidanaya You can use the jest preset from @thymikee which is described here: https://github.com/thymikee/jest-preset-angular Works like a charm for us. |
@4kochi no doubt it's working great for unit test. But out of curiosity, is it working well with integration tests and compiled templates? Last time I tried (a long ago), it was not doing well especially with the fact that I'm using scss and for some reasons it was just crashing. Can you give a bit more of information on the app you use jest? Is it medium, large? |
@maxime1992 We have a medium size project I would say. But we are only doing unit test with jest. So no angular helper methods from the TestBed class. Just calling the component constructor and testing the ts code. So I can’t say if this is fixed now. But you can make an issue in the jest-preset-angular if it’s still the case. They normally respond really fast. |
@filipesilva Can this be achieved now that Angular moves to Bazel? |
@maxime1992 we are using Jest for both unit and integration tests (via jest-preset-angular) and it's been working just fine for us. In case it's helpful, this repo is using Jest for all library code (~80% unit / 20% integration). |
Even better is the builder! |
@filipesilva maybe this issue can be reopened since the angular builder is there? |
@sinedied You can use the above mentioned builder directly in a project. There are no changes to the CLI code required. |
@cgatian Looks like the angular builder url changed to https://github.com/meltedspark/angular-builders/tree/master/packages/jest |
Just to have people aware before deciding to move on Jest, it seems that it's not possible to use fakeAsync with Jest... Which in my case is a huge issue. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
This is a proposal/future request
Current Angular CLI testing tools based on karma and jasmine that we all get use to it. But would you consider to support Jest as well https://github.com/facebook/jest ?
Jest testing framework commonly used across React and Nodejs projects. It is supports unit tests, assertions, mocks, coverage, live test reload, JS File mocks and has a lots of features build in to it like 'Snapshot Testing' also is provided zero configuration experience and fully compatible with webpack.
Goal
Just thinking if it might help the Angular community to improve out testing story using Jest and it would of be great it it can be available with Angular CLI.
Any thoughts?
The text was updated successfully, but these errors were encountered: