Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Kiran Mantha
Kiran Mantha

Posted on

     

Integrating Jest with latest angular version

As jest is increasingly gaining traction in unit testing angular components lets see how to integrate it with latest angular versions (12+)

  • uninstall jasmine & karma usingnpm uninstall -D @types/jasmine jasmine jasmine-core jasmine-spec-reporter karma karma-chrome-launcher karma-coverage karma-jasmine karma-jasmine-html-reporter ts-node

  • install jest usingnpm i -D @types/jest jest jest-preset-angular @angular-builders/jest

  • create filesetupJest.ts in root and add below content

import{getTestBed}from'@angular/core/testing';import{BrowserDynamicTestingModule,platformBrowserDynamicTesting,}from'@angular/platform-browser-dynamic/testing';import'jest-preset-angular';import'zone.js';import'zone.js/testing';getTestBed().initTestEnvironment(BrowserDynamicTestingModule,platformBrowserDynamicTesting(),{teardown:{destroyAfterEach:true},});Object.defineProperty(window,'CSS',{value:null});Object.defineProperty(window,'getComputedStyle',{value:()=>{return{display:'none',appearance:['-webkit-appearance']};}});Object.defineProperty(document,'doctype',{value:'<!DOCTYPE html>'});Object.defineProperty(document.body.style,'transform',{value:()=>{return{enumerable:true,configurable:true};}});
Enter fullscreen modeExit fullscreen mode
  • If you are migrating from lower angular version usingng upgrade then except above piece of code if you are using any other piece of code insrc/test.ts file then add that new content tosetupJest.ts and deletesrc/test.ts

  • create filejest.config.js in root and add below content

module.exports={preset:'jest-preset-angular',roots:['<rootDir>/src'],testMatch:['**/+(*.)+(spec).+(ts)'],collectCoverage:true,coverageReporters:['html','lcov','json','text'],coverageDirectory:'<rootDir>/coverage',moduleFileExtensions:['ts','html','js','json'],};
Enter fullscreen modeExit fullscreen mode
  • replacetest part ofangular.json with below content
..."test":{"builder":"@angular-builders/jest:run","options":{"main":["setupJest.ts"],"tsConfig":"src/tsconfig.spec.json","no-cache":true}}...
Enter fullscreen modeExit fullscreen mode
  • add"esModuleInterop": true tocompilerOptions intsconfig.json
  • opentsconfig.spec.json and replacejasmine withjest intypes field

That's it jest is now comlpetely integrated with latest angular 👏 💥

Thanks for reading 👍 and post any issue if you faced while doing so in comments below 👇

You can find the example repohere

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

A playful father and developer. Creator of PlumeJS a webcomponent & typescript based framework to build SPAs
  • Location
    Bangalore
  • Work
    Senior Dev at PublicisSapient
  • Joined

More fromKiran Mantha

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp