{"id":2160,"date":"2024-09-10T18:00:37","date_gmt":"2024-09-11T01:00:37","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=2160"},"modified":"2024-09-13T10:31:52","modified_gmt":"2024-09-13T17:31:52","slug":"how-to-use-jest-for-angular-unit-tests","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/how-to-use-jest-for-angular-unit-tests\/","title":{"rendered":"How to use Jest for Angular Unit Tests?"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/github.com\/karma-runner\/karma?tab=readme-ov-file#karma-is-deprecated-and-is-not-accepting-new-features-or-general-bug-fixes\" target=\"_blank\" rel=\"noopener\" title=\"\">Karma is deprecated<\/a>, and the Angular team is working on official support for <a href=\"https:\/\/jestjs.io\/\" title=\"\">Jest<\/a> and <a href=\"https:\/\/modern-web.dev\/docs\/test-runner\/overview\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Web Test Runner<\/a>. Today, I want to highlight how to set up your Angular project for Jest since it&#8217;s prevalent in the Javascript ecosystem (React, etc.). It&#8217;s super fast and 100% browserless, making running tests on a continuous integration server easy. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, the syntax is essentially the same as Jasmine\/Karma, so you won&#8217;t have to change your tests much unless you do extensive mocking in Jasmine.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here are the steps I&#8217;ve used on over 28 repositories so far, with great success:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Uninstall Jasmine, Karma, and all associated types<\/strong><br><code>npm uninstall karma karma-chrome-launcher karma-coverage karma-jasmine karma-jasmine-html-reporter @types\/jasmine jasmine-core<\/code><br><br><\/li>\n\n\n\n<li><strong>Install Jest, its types, and presets for Angular<\/strong><br>n<code>pm i --save-dev jest @types\/jest jest-preset-angular<\/code><br><br><\/li>\n\n\n\n<li><strong>Create a <code>setup-jest.ts<\/code> file in your project&#8217;s root folder<\/strong><br>And add that single line of code in it:<br><code>import 'jest-preset-angular\/setup-jest';<\/code><br><br><\/li>\n\n\n\n<li><strong>Create a <code>jest.config.ts<\/code> file in your project&#8217;s root folder<\/strong> <strong>with the following command<\/strong><br><code>npx jest \u2014 init<\/code><br><br>Then ensure you have the following config options set in that file:<br><code>preset: 'jest-preset-angular',<br>setupFilesAfterEnv: ['.\/setup-jest.ts']<\/code><br><br><\/li>\n\n\n\n<li><strong>Update your <code>tsconfig.spec.json<\/code> file<\/strong><br><br>Your <code>compilerOptions<\/code> should look like this:<br><code>\"compilerOptions\": {    <br>       \"outDir\": \".\/out-tsc\/spec\",    <br>       \"types\": [      \"jest\"    ],    <br>       \"esModuleInterop\": true, <br>       \"emitDecoratorMetadata\": true<br> },<\/code><br><br><\/li>\n\n\n\n<li><strong>Last step: Update <code>package.json<\/code><\/strong><br>In the scripts section, replace the <code>test<\/code> entry with:<br><code>\"test\": \"jest\",<\/code><br><\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">And&#8230; that&#8217;s it! You can now launch <code>npm run test<\/code> to run your tests with Jest. Sure, a schematic would be better, but those steps take only 2-3 minutes to implement for now.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Karma is deprecated, and the Angular team is working on official support for Jest and Web Test Runner. Today, I want to highlight how to set up your Angular project for Jest since it&#8217;s prevalent in the Javascript ecosystem (React, etc.). It&#8217;s super fast and 100% browserless, making running tests on a continuous integration server [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,19,17],"tags":[],"class_list":["post-2160","post","type-post","status-publish","format-standard","hentry","category-angular","category-testing","category-tools"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/2160","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=2160"}],"version-history":[{"count":8,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/2160\/revisions"}],"predecessor-version":[{"id":2169,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/2160\/revisions\/2169"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=2160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=2160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=2160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}