DEV Community

Gaëtan Redin
Gaëtan Redin

Posted on • Originally published at Medium on

3 1

Jest And Angular, Install

A powerful combination, you should install and try it

Hey you know we all desire to work with the best libraries. For months, I only use Jest in my Angular projects. It’s faster, less unexpected behaviors and it offers the snapshot feature which is really a must-have.

Try it yourself, you will see that you will leave Karma/Jasmine for it.

Angular 12.2 / Jest 27.2.0

Step 1: Just install the essential

npm install jest jest-preset-angular --save-dev
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a small setup file

cd src
touch setupJest.ts
Enter fullscreen mode Exit fullscreen mode

Step 3: Add only this as config

// setupJest.ts

import 'jest-preset-angular/setup-jest';
Enter fullscreen mode Exit fullscreen mode

Step 4: Add entry to the package.json

// package.json
{
  ...
  "jest": {     
    "preset": "jest-preset-angular", 
    "setupTestFrameworkScriptFile": "<rootDir>/setupJest.ts"
  }
  ...
}
Enter fullscreen mode Exit fullscreen mode

Step 5: Replace the test(s) script(s)

// package.json
"test": "jest",
"test:watch": "jest --watch",
"test:ci": "jest --runInBand"
Enter fullscreen mode Exit fullscreen mode

Step 6: Uninstall Karma/Jasmine

npm uninstall karma karma-chrome-launcher karma-coverage-istanbul-reporter karma-jasmine karma-jasmine-html-reporter
Enter fullscreen mode Exit fullscreen mode

Step 7: Test it

npm run test

> XXX@0.0.0 test <my-project-path>
> jest

PASS src/app/app.component.spec.ts
  AppComponent
    √ setup (2 ms)

Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 2.069 s
Ran all test suites.
Enter fullscreen mode Exit fullscreen mode

That’s all, I hope this tutorial will help someone.

Thanks for reading. Feel free to comment.

Learn More

Angular for everyone: All about it

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay