<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: David Würfel</title>
    <description>The latest articles on DEV Community by David Würfel (@mrcube42).</description>
    <link>https://dev.to/mrcube42</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F197074%2Fac89bfb8-24ad-4699-bd1d-2801dec7a8b6.png</url>
      <title>DEV Community: David Würfel</title>
      <link>https://dev.to/mrcube42</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mrcube42"/>
    <language>en</language>
    <item>
      <title>CI ready e2e tests for Angular with Cypress and TypeScript in under 60 minutes</title>
      <dc:creator>David Würfel</dc:creator>
      <pubDate>Mon, 23 Mar 2020 07:05:37 +0000</pubDate>
      <link>https://dev.to/angular/ci-ready-e2e-tests-for-angular-with-cypress-and-typescript-in-under-60-minutes-4f30</link>
      <guid>https://dev.to/angular/ci-ready-e2e-tests-for-angular-with-cypress-and-typescript-in-under-60-minutes-4f30</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This article aims to describe how you can set up &lt;strong&gt;end-to-end testing&lt;/strong&gt; for &lt;strong&gt;Angular&lt;/strong&gt; with &lt;strong&gt;Cypress&lt;/strong&gt; including &lt;strong&gt;TypeScript&lt;/strong&gt;. You will write your very first &lt;strong&gt;e2e tests&lt;/strong&gt; and make them ready to run on a &lt;strong&gt;CircleCI&lt;/strong&gt; as a continuous integration system with every update to your repository.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Before we start: What is an e2e test?&lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;End-to-end &lt;em&gt;(short e2e)&lt;/em&gt; testing  is a type of software testing that validates the software system along with its integration with external interfaces. The purpose of end-to-end test is to exercise a complete production-like scenario.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Source: &lt;a href="https://www.guru99.com/end-to-end-testing.html" rel="noopener noreferrer"&gt;https://www.guru99.com/end-to-end-testing.html&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Overview
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;What is Cypress?&lt;/li&gt;
&lt;li&gt;Prerequisites&lt;/li&gt;
&lt;li&gt;Setting up Cypress&lt;/li&gt;
&lt;li&gt;Writing Some Tests&lt;/li&gt;
&lt;li&gt;Setting up Continuous Integration&lt;/li&gt;
&lt;li&gt;Conclusion and References&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I have a frontend development background in &lt;a href="https://docs.microsoft.com/en-US/dotnet/framework/wpf/" rel="noopener noreferrer"&gt;Microsoft's .NET &amp;amp; WPF&lt;/a&gt; and remember the times where we evaluated costly frameworks to write end-to-end tests for our projects. After a lot of evaluations and weeks, even months of custom glue code and development of test infrastructures on top of existing tools, we finally got some e2e tests running. They were brittle, often failed because of manual adjustments we had to do or problems with flaky runners in the continuous integration pipeline.&lt;/p&gt;

&lt;p&gt;Some years later with &lt;a href="https://angular.io/" rel="noopener noreferrer"&gt;Angular&lt;/a&gt; and &lt;a href="https://www.protractortest.org/#/" rel="noopener noreferrer"&gt;Protractor&lt;/a&gt; as a default for e2e tests, we were still based on &lt;a href="https://www.protractortest.org/#/page-objects" rel="noopener noreferrer"&gt;page objects&lt;/a&gt;, &lt;a href="https://www.selenium.dev/" rel="noopener noreferrer"&gt;Selenium Web Driver&lt;/a&gt; and the tests continued to be rather unreliable. No expensive commercial frameworks and custom infrastructure were needed. But was it fun to write e2e tests? No.&lt;/p&gt;

&lt;p&gt;However we are in 2020 now and time has come for new heroes to arise. 🚀&lt;/p&gt;

&lt;h1&gt;
  
  
  What is Cypress?&lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://cypress.io" rel="noopener noreferrer"&gt;Cypress&lt;/a&gt; promises &lt;em&gt;fast, easy and reliable testing for anything that runs in a browser&lt;/em&gt;. It is not based on Selenium Web Driver which is using network connections to interact with your browser. Instead Cypress is a test runner that runs &lt;em&gt;inside&lt;/em&gt; your browser next to your web application and therefore has has direct control over it.&lt;/p&gt;

&lt;p&gt;Without going into all the details, this not only makes Cypress faster and more reliable, it also opens the door for a lot of other interesting features like&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;time travel debugging,&lt;/li&gt;
&lt;li&gt;easy snapshotting and recording,&lt;/li&gt;
&lt;li&gt;automatic waitings.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On top of all the features, Cypress has a developer experience (&lt;em&gt;DX&lt;/em&gt;) that is nearly unrivalled. Have you ever seen a &lt;a href="https://www.cypress.io/blog/2017/07/26/good-error-messages/" rel="noopener noreferrer"&gt;message in the error logs&lt;/a&gt; of your failed build that tells you exactly what you did wrong, points you to the right dependencies to add and also links to an explanatory documentation site describing the problem? This is what Cypress feels like. It is built by developers for developers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fl90rk6dmuijzkv3uvhg6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fl90rk6dmuijzkv3uvhg6.png" alt="Cypress error on the build server telling you how to fix it" width="800" height="378"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hereafter, we will install Cypress for a fresh Angular project created with the CLI. We'll write some e2e tests and conclude with running these by an automated build system. All these steps should not take more than 60 minutes. We try to keep the steps as short as possible, leveraging existing tools like &lt;a href="https://angular.io/guide/schematics" rel="noopener noreferrer"&gt;Angular Schematics&lt;/a&gt;, libraries and common templates.&lt;/p&gt;

&lt;h1&gt;
  
  
  Prerequisites&lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;This guide assumes that you have a standard Angular 9 app project. If not, you may create one like you would normally do with the &lt;a href="https://angular.io/cli/new" rel="noopener noreferrer"&gt;Angular CLI&lt;/a&gt;. If you don't have the CLI installed globally, you can make use of the &lt;a href="https://www.npmjs.com/package/npx" rel="noopener noreferrer"&gt;&lt;code&gt;npx&lt;/code&gt;&lt;/a&gt; command that will install it temporarily on the fly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx @angular/cli new &amp;lt;app-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h1&gt;
  
  
  Setting up Cypress&lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;In order to set up Cypress together with TypeScript as fast as possible, we make use of an existing schematic developed by &lt;a href="https://www.npmjs.com/package/@briebug/cypress-schematic" rel="noopener noreferrer"&gt;BrieBug&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In the root of your Angular project, you can open the terminal and enter the following command:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ng add @briebug/cypress-schematic &lt;span class="nt"&gt;--addCypressTestScripts&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If the CLI isn't installed globally, the &lt;code&gt;ng&lt;/code&gt; command may not be available directly. You can enforce the use of the local &lt;code&gt;ng&lt;/code&gt; from the &lt;code&gt;package.json&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run ng &lt;span class="nt"&gt;--&lt;/span&gt; add @briebug/cypress-schematic &lt;span class="c"&gt;# In case 'ng' could not be found&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We can safely remove Protractor because it will be completely replaced. During the installation some binaries were downloaded because Cypress comes with an &lt;a href="https://www.electronjs.org/" rel="noopener noreferrer"&gt;Electron&lt;/a&gt;-bundled UI as an interactive test runner.&lt;/p&gt;

&lt;p&gt;Using the flag &lt;code&gt;--addCypressTestScripts&lt;/code&gt; two handy npm scripts were added to make the work with Cypress more comfortable. One to run e2e tests headless and the other script running the tests with the Cypress UI runner:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;package.json&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;scripts&lt;/span&gt;&lt;span class="w"&gt;

    &lt;/span&gt;&lt;span class="nl"&gt;"cy:run"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"cypress run"&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"cy:open"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"cypress open"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If you were to run one of these scripts standalone, the test would fail because it tries to route to &lt;em&gt;&lt;a href="http://localhost:4200" rel="noopener noreferrer"&gt;http://localhost:4200&lt;/a&gt;&lt;/em&gt; where nothing is served at the moment. In order to fix this, we need to open a second terminal and serve our Angular application beforehand with &lt;code&gt;npm start&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Luckily the schematic adjusted the &lt;code&gt;e2e&lt;/code&gt; command so that this is done for you automatically by the &lt;a href="https://angular.io/guide/cli-builder" rel="noopener noreferrer"&gt;CLI builder&lt;/a&gt;. You can serve the application and start the e2e test by using the following command:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run e2e
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Cypress will detect that we launched it for the first time. It verifies its installation and adds some initial example files. After the UI has opened up, we can see a test that has already been created for us.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2byc31bumuz3hoozi28h.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2byc31bumuz3hoozi28h.PNG" alt="Cypress UI after cy:open" width="800" height="550"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Selecting the test will run it. Initially the test will fail because we didn't actually test something properly. We will fix this now.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fqi6455u49slch9bq2gor.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fqi6455u49slch9bq2gor.png" alt="Failing test that is not implemented yet" width="800" height="448"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Writing Some Tests&lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;As a very first step, as proposed by the Cypress &lt;a href="https://docs.cypress.io/guides/references/best-practices.html" rel="noopener noreferrer"&gt;best practices&lt;/a&gt;, we set our &lt;a href="https://docs.cypress.io/guides/references/best-practices.html#Setting-a-global-baseUrl" rel="noopener noreferrer"&gt;global baseUrl&lt;/a&gt;, so that we don't have to duplicate this on every test execution. Add the following to the configuration &lt;code&gt;cypress.json&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;cypress.json&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"baseUrl"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"http://localhost:4200"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;After that, we write our very first &lt;a href="https://en.wikipedia.org/wiki/Smoke_testing_(software)" rel="noopener noreferrer"&gt;&lt;em&gt;smoke test&lt;/em&gt;&lt;/a&gt; that only checks whether the default app title is set on the Angular starting page. Therefore, change the content of the &lt;code&gt;spec.ts&lt;/code&gt; to the following content:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// spec.ts&lt;/span&gt;
&lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;smoke test&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cypress-e2e-testing-angular app is running!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The test starts by routing to our &lt;em&gt;baseUrl&lt;/em&gt; and proceeds by querying any element that contains the text &lt;em&gt;cypress-e2e-testing-angular app is running!&lt;/em&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Testing User Flows
&lt;/h2&gt;

&lt;p&gt;This test should already work, but let's write some more interactive ones. As e2e are inherently slower than unit tests, it is totally fine to have e2e tests that model the entire user flow for a feature.&lt;/p&gt;

&lt;p&gt;For example, we want to check whether some characteristics of the starting page are valid: Our page should contain the title and the &lt;code&gt;ng generate&lt;/code&gt; text in the terminal by default, but when the users clicks the &lt;em&gt;Angular Material&lt;/em&gt; button, we want to ensure that the proper &lt;code&gt;ng add&lt;/code&gt; command is displayed in the terminal view below.&lt;/p&gt;

&lt;p&gt;You may replace the content of your test file with this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// spec.ts&lt;/span&gt;
&lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;When Angular starting page is loaded&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;beforeEach&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;has app title, shows proper command by default and reacts on command changes&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cypress-e2e-testing-angular&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.terminal&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ng generate component xyz&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Angular Material&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.terminal&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ng add @angular/material&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We refactored our test suite by adding a &lt;code&gt;describe&lt;/code&gt; block to capture all tests that run when the starting page is loaded. As we visit the &lt;em&gt;baseUrl&lt;/em&gt; every time, we moved this into the &lt;code&gt;beforeEach&lt;/code&gt; call. Lastly we combined the basic smoke tests with the test for the terminal view on the starting page.&lt;/p&gt;

&lt;p&gt;It is important to know that you should not store Cypress' query results in variables, but instead work with &lt;a href="https://docs.cypress.io/guides/references/best-practices.html#Assigning-Return-Values" rel="noopener noreferrer"&gt;closures&lt;/a&gt;. Moreover, we selected elements by CSS classes and text content, which may be too brittle. It is recommended to use &lt;code&gt;data-&lt;/code&gt; attributes for &lt;a href="https://docs.cypress.io/guides/references/best-practices.html#Selecting-Elements" rel="noopener noreferrer"&gt;selecting elements&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Cypress has a lot of great features and possibilities. We won't cover all of them because our goal is to focus on the very first starting point. The official documentation is really good and covers everything on how to &lt;a href="https://docs.cypress.io/guides/core-concepts/interacting-with-elements.html#Actionability" rel="noopener noreferrer"&gt;interact with elements&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you rerun this test suite, you should see the UI clicking through each scenario and all three tests should pass this time. ✔✔✔&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2hkws56oi8xyusxu6t4r.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2hkws56oi8xyusxu6t4r.gif" alt="Running our first Cypress test suite succefully" width="1200" height="656"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Setting up Continuous Integration&lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;Now that our tests run locally, let's kick of a small CI (&lt;a href="https://en.wikipedia.org/wiki/Continuous_integration" rel="noopener noreferrer"&gt;continuous integration&lt;/a&gt;) pipeline. A good way to prepare for this, is to create npm scripts and combine them so that the build system can use a single script as entry point. By following this method, you can try the CI steps locally before pushing online. Moreover npm scripts are rather independent from any actual build system.&lt;/p&gt;

&lt;p&gt;On CI, we need to start our server in the background and wait for it to bundle our application, which might take a while. Then we need to start the Cypress test runner, go through the tests and shut down the server when the tests finish. Luckily we can do this all with a single utility called &lt;em&gt;start-server-and-test&lt;/em&gt; as described in the &lt;a href="https://docs.cypress.io/guides/guides/continuous-integration.html#Solutions" rel="noopener noreferrer"&gt;Cypress docs&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save-dev&lt;/span&gt; start-server-and-test
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;After this is installed, we use the Angular &lt;em&gt;serve&lt;/em&gt; which is currently behind &lt;code&gt;npm start&lt;/code&gt; and combine it with the headless &lt;code&gt;cy:run&lt;/code&gt; command:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;package.json&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;scripts&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"start"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ng serve"&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"cy:run"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"cypress run"&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"e2e:ci"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"start-server-and-test start http://localhost:4200 cy:run"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;You could surely use a production build or build beforehand and serve the app using any http server. For sake of conciseness, I will leave these improvements up for you.&lt;/p&gt;
&lt;h2&gt;
  
  
  Circle CI
&lt;/h2&gt;

&lt;p&gt;For our example, we choose &lt;a href="https://circleci.com/" rel="noopener noreferrer"&gt;CircleCI&lt;/a&gt; because it integrates very well with GitHub, is commonly used there and has a free plan. You may use any other CI system like &lt;a href="https://jenkins.io/" rel="noopener noreferrer"&gt;Jenkins&lt;/a&gt; or &lt;a href="https://about.gitlab.com/" rel="noopener noreferrer"&gt;GitLab&lt;/a&gt; (which I have the most experience with). After signing into CircleCI and connecting to our GitHub account, you can select the repository and create a new project via their dashboard.&lt;/p&gt;

&lt;p&gt;In order to configure the pipeline, you could write a &lt;code&gt;config.yml&lt;/code&gt; by selecting a template and adjusting it to your needs and eventually running the e2e script. Fortunately Cypress has a &lt;a href="https://github.com/cypress-io/circleci-orb" rel="noopener noreferrer"&gt;ready to use configurations&lt;/a&gt; (called &lt;a href="https://github.com/cypress-io/circleci-orb/blob/master/docs/examples.md" rel="noopener noreferrer"&gt;Orbs&lt;/a&gt;) for CircleCI which already include the installation of dependencies, caching and so on. Before we can use it, we must visit the &lt;em&gt;Organisation Settings&lt;/em&gt; to &lt;a href="https://github.com/cypress-io/circleci-orb#how-to-enable" rel="noopener noreferrer"&gt;enable third party runners&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# circleci/config.yml&lt;/span&gt;

&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2.1&lt;/span&gt;
&lt;span class="na"&gt;orbs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="c1"&gt;# This Orb includes the following:&lt;/span&gt;
  &lt;span class="c1"&gt;# - checkout current repository&lt;/span&gt;
  &lt;span class="c1"&gt;# - npm install with caching&lt;/span&gt;
  &lt;span class="c1"&gt;# - start the server&lt;/span&gt;
  &lt;span class="c1"&gt;# - wait for the server to respond&lt;/span&gt;
  &lt;span class="c1"&gt;# - run Cypress tests&lt;/span&gt;
  &lt;span class="c1"&gt;# - store videos and screenshots as artifacts on CircleCI&lt;/span&gt;
  &lt;span class="na"&gt;cypress&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;cypress-io/cypress@1&lt;/span&gt;
&lt;span class="na"&gt;workflows&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;cypress/run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npm start&lt;/span&gt;
          &lt;span class="na"&gt;wait-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;http://localhost:4200'&lt;/span&gt;
          &lt;span class="na"&gt;store_artifacts&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The pipeline only has one job: Run all e2e tests. It checks out the current branch, installs all dependencies including caching, starts the application server and runs our tests. Additionally, videos (recorded by default) and screenshots (in case tests are failing) are uploaded as CircleCI artifacts for further inspection.*&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F77rim7ftwubsef1pzn32.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F77rim7ftwubsef1pzn32.PNG" alt="CircleCI Dashboard" width="800" height="534"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/w3BVGdNIhFg"&gt;
&lt;/iframe&gt;
&lt;/p&gt;
&lt;h1&gt;
  
  
  Conclusion&lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;The steps in this guide are rather minimal. You may use your existing Angular project, may change the configuration of your Cypress test suites and write a lot of more meaningful tests. Moreover, you may define npm scripts for different scenarios and environments and of course your entire build pipeline may be extended with linting, unit testing, building and even deploying your application. Nevertheless, this should be a first step which shows how quick automated end-to-end tests can be set up in nowadays.&lt;/p&gt;

&lt;p&gt;Wait till you write real Cypress tests for your application. You will have fun!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnvco03xmwknslcrqgytc.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnvco03xmwknslcrqgytc.gif" alt="Mind blown gif" width="263" height="172"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope that you will also find some value in this article. If you have any questions or remarks, just let me know. Your feedback is very welcome!&lt;/p&gt;

&lt;p&gt;You can find the sources for this guide on GitHub:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/MrCube42" rel="noopener noreferrer"&gt;
        MrCube42
      &lt;/a&gt; / &lt;a href="https://github.com/MrCube42/cypress-e2e-testing-angular" rel="noopener noreferrer"&gt;
        cypress-e2e-testing-angular
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Example Angular 9 app using Cypress for end-to-end testing.
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;p&gt;
  &lt;a href="https://circleci.com/gh/MrCube42/cypress-e2e-testing-angular" rel="nofollow noopener noreferrer"&gt;
    &lt;img src="https://camo.githubusercontent.com/85251b9090d0e7612d218c0a4d961b37d8d671a93eb52cb37f2baea9345bc591/68747470733a2f2f636972636c6563692e636f6d2f67682f4d724375626534322f637970726573732d6532652d74657374696e672d616e67756c61722f747265652f6d61737465722e7376673f7374796c653d736869656c64"&gt;
  &lt;/a&gt;
&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;CypressE2eTestingAngular&lt;/h1&gt;

&lt;/div&gt;

&lt;p&gt;This project was generated with &lt;a href="https://github.com/angular/angular-cli" rel="noopener noreferrer"&gt;Angular CLI&lt;/a&gt; version 9.0.6.&lt;/p&gt;

&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Development server&lt;/h2&gt;

&lt;/div&gt;

&lt;p&gt;Run &lt;code&gt;ng serve&lt;/code&gt; for a dev server. Navigate to &lt;code&gt;http://localhost:4200/&lt;/code&gt;. The app will automatically reload if you change any of the source files.&lt;/p&gt;

&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Code scaffolding&lt;/h2&gt;

&lt;/div&gt;

&lt;p&gt;Run &lt;code&gt;ng generate component component-name&lt;/code&gt; to generate a new component. You can also use &lt;code&gt;ng generate directive|pipe|service|class|guard|interface|enum|module&lt;/code&gt;.&lt;/p&gt;

&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Build&lt;/h2&gt;

&lt;/div&gt;

&lt;p&gt;Run &lt;code&gt;ng build&lt;/code&gt; to build the project. The build artifacts will be stored in the &lt;code&gt;dist/&lt;/code&gt; directory. Use the &lt;code&gt;--prod&lt;/code&gt; flag for a production build.&lt;/p&gt;

&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Running unit tests&lt;/h2&gt;

&lt;/div&gt;

&lt;p&gt;Run &lt;code&gt;ng test&lt;/code&gt; to execute the unit tests via &lt;a href="https://karma-runner.github.io" rel="nofollow noopener noreferrer"&gt;Karma&lt;/a&gt;.&lt;/p&gt;

&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Running end-to-end tests&lt;/h2&gt;

&lt;/div&gt;

&lt;p&gt;Run &lt;code&gt;npm run e2e&lt;/code&gt; to execute the end-to-end tests via &lt;a href="http://cypress.io/" rel="nofollow noopener noreferrer"&gt;Cypress&lt;/a&gt;.&lt;/p&gt;

&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Further help&lt;/h2&gt;

&lt;/div&gt;

&lt;p&gt;To get more help on the Angular CLI use &lt;code&gt;ng help&lt;/code&gt; or go check out the &lt;a href="https://github.com/angular/angular-cli/blob/master/README.md" rel="noopener noreferrer"&gt;Angular CLI README&lt;/a&gt;.&lt;/p&gt;

&lt;/div&gt;
&lt;br&gt;
&lt;br&gt;
  &lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/MrCube42/cypress-e2e-testing-angular" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


&lt;h3&gt;
  
  
  References&lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;*For other CI systems we could use our previously defined npm script. However we need to take care of all the additional work by ourselves. If you already have an existing sophisticated pipeline it could be easier to integrate just the script.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://angular.io/docs" rel="noopener noreferrer"&gt;Official Angular documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/briebug/cypress-schematic" rel="noopener noreferrer"&gt;Briebug Cypress schematic&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.cypress.i" rel="noopener noreferrer"&gt;Official Cypress documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://circleci.com/" rel="noopener noreferrer"&gt;CircleCI&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Setup Jest for Angular with Debugging in Visual Studio Code</title>
      <dc:creator>David Würfel</dc:creator>
      <pubDate>Mon, 09 Dec 2019 19:31:41 +0000</pubDate>
      <link>https://dev.to/angular/setup-jest-for-angular-with-debugging-in-visual-studio-code-2d96</link>
      <guid>https://dev.to/angular/setup-jest-for-angular-with-debugging-in-visual-studio-code-2d96</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This article aims to describe a really fast step-by-step way to setup &lt;a href="https://jestjs.io" rel="noopener noreferrer"&gt;Jest&lt;/a&gt; testing for &lt;a href="https://angular.io/" rel="noopener noreferrer"&gt;Angular&lt;/a&gt; projects including &lt;a href="https://code.visualstudio.com/Docs/editor/debugging" rel="noopener noreferrer"&gt;debugging&lt;/a&gt; capabilities of &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;Visual Studio Code&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Overview
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Why Jest?&lt;/li&gt;
&lt;li&gt;Prerequisites&lt;/li&gt;
&lt;li&gt;Removal of Karma (and Jasmine)&lt;/li&gt;
&lt;li&gt;Setting up Jest&lt;/li&gt;
&lt;li&gt;Configure Debugging in VS Code&lt;/li&gt;
&lt;li&gt;Outlook and References&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I always thought that no one will ever read my blog posts. However, I myself will surely read my own as long as I haven't completely automated the following steps. The upcoming information is already present online but varies and is distributed over several sources*. My goal is to describe everything that is needed in one single post.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why Jest?&lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;The default test runner for Angular applications is &lt;a href="https://karma-runner.github.io/latest/index.html" rel="noopener noreferrer"&gt;Karma&lt;/a&gt; in combination with &lt;a href="https://jasmine.github.io/" rel="noopener noreferrer"&gt;Jasmine&lt;/a&gt; as test framework. In recent times I often prefer &lt;a href="https://jestjs.io" rel="noopener noreferrer"&gt;Jest&lt;/a&gt; as testing framework. In my opinion it has some advantages over the default setup: It is headless out-of-the-box, less configuration is needed and it has some handy features like snapshot testing. Since Jest is headless which means that it won't spawn a browser window where you can open Chrome Dev Tools, it can get cumbersome to use console-logs when trying to debug while testing. The debug logs would spam the terminal and you will probably have to scroll a lot to find the outputs you are looking for. At this point you should think about using the great &lt;a href="https://code.visualstudio.com/Docs/editor/debugging" rel="noopener noreferrer"&gt;debugging&lt;/a&gt; features of your editor.&lt;/p&gt;

&lt;h1&gt;
  
  
  Some Prerequisites&lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;This guide assumes that you are using Angular in version 8 (as it uses the new &lt;a href="https://angular.io/guide/cli-builder" rel="noopener noreferrer"&gt;CLI builders&lt;/a&gt;). Moreover, I assume that we won't use &lt;a href="https://www.protractortest.org" rel="noopener noreferrer"&gt;Protractor&lt;/a&gt; as E2E testing framework. We're already living in heaven and may be using &lt;a href="https://cypress.io" rel="noopener noreferrer"&gt;Cypress&lt;/a&gt; later. Therefore, we can safely remove anything related to Jasmine.&lt;/p&gt;

&lt;p&gt;If you don't have an Angular application already, you may create one like you would normally do with the &lt;a href="https://angular.io/cli/new" rel="noopener noreferrer"&gt;Angular CLI&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ng new &amp;lt;app-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h1&gt;
  
  
  The Cleanup&lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;When it comes to source code or configuration, I am a huge friend of &lt;em&gt;less is more&lt;/em&gt;. Any code snippet or configuration that is not needed or commented out can be removed. Our plan is to remove any Karma and Jasmine related dependencies and entities to always have a clean project.&lt;/p&gt;
&lt;h2&gt;
  
  
  Remove Protractor and Jasmine &lt;em&gt;(Optional)&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;If you really want to use Protractor for your end-to-end tests you can skip this part. Otherwise, we will remove everything related to the E2E tests. If we have done this, we can remove everything related to Jasmine, too.&lt;/p&gt;

&lt;p&gt;Remove the following because we won't use it anymore:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;/e2e&lt;/code&gt; folder containing all your end-to-end tests and configuration&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;e2e&lt;/code&gt; section from &lt;code&gt;angular.json&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;e2e&lt;/code&gt; entry in the &lt;code&gt;README.md&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;e2e&lt;/code&gt; command from the &lt;code&gt;scripts&lt;/code&gt; in the &lt;code&gt;package.json&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now we can safely uninstall Protractor and Jasmine dependencies:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm uninstall protractor
npm uninstall jasmine-core jasmine-spec-reporter @types/jasmine @types/jasminewd2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Remove Karma
&lt;/h2&gt;

&lt;p&gt;Since we will use Jest for our entire testing we can now remove everything that is related to Karma.&lt;/p&gt;

&lt;p&gt;Uninstall Karma dependencies:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm uninstall karma karma-chrome-launcher karma-coverage-istanbul-reporter
karma-jasmine karma-jasmine-html-reporter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;As we don't use Karma, we can remove the following files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;karma.conf.js&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;src/test.ts&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  The Setup&lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;We cleaned up our project from any unused resources and will now setup our toolchain to run unit tests with Jest.&lt;/p&gt;
&lt;h2&gt;
  
  
  Add Jest
&lt;/h2&gt;

&lt;p&gt;We can start by installing Jest, it's types and new Angular builder that is used to run the Jest tests:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save-dev&lt;/span&gt; jest @types/jest @angular-builders/jest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Adjust Angular CLI Configuration
&lt;/h2&gt;

&lt;p&gt;Replace the previous Karma &lt;code&gt;builder&lt;/code&gt; in the &lt;code&gt;test&lt;/code&gt; section of the &lt;code&gt;angular.json&lt;/code&gt; with the new Jest builder:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"builder"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"@angular-builders/jest:run"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"options"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; 
    &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;The&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;options&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;and&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;everything&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;in&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;here&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;can&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;be&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;removed.&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Adjust TypeScript Configurations
&lt;/h2&gt;

&lt;p&gt;In your configuration for the tests (&lt;code&gt;tsconfig.spec.json&lt;/code&gt;) change the entry &lt;code&gt;jasmine&lt;/code&gt; in the &lt;code&gt;types&lt;/code&gt; array to &lt;code&gt;jest&lt;/code&gt; and remove the file &lt;code&gt;src/test.ts&lt;/code&gt; from the &lt;code&gt;files&lt;/code&gt; array. Set &lt;code&gt;emitDecoratorMetadata&lt;/code&gt; and &lt;code&gt;esModuleInterop&lt;/code&gt; to &lt;code&gt;true&lt;/code&gt;. The first one is needed for Angular's dependency injection to work this Jest, the latter one is &lt;a href="https://github.com/kulshekhar/ts-jest/wiki/Troubleshooting#commonjs-compatibility" rel="noopener noreferrer"&gt;suggested&lt;/a&gt; by a Jest warning when running your tests.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"types"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="s2"&gt;"jest"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="s2"&gt;"node"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"emitDecoratorMetadata"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"esModuleInterop"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"files"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"src/polyfills.ts"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Lastly in your main base configuration &lt;code&gt;tsconfig.json&lt;/code&gt; also change the &lt;code&gt;types&lt;/code&gt; entry from &lt;code&gt;jasmine&lt;/code&gt; to &lt;code&gt;jest&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Add Minimal Jest Configuration &lt;em&gt;(Optional)&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;When running Jest it complains that it cannot find a configuration file. Since I don't like such warnings we take this opportunity to add a minimal configuration that also setups up the code coverage format. I want the coverage to output a nice HTML site as an overview. You can use one of the &lt;a href="https://github.com/istanbuljs/istanbuljs/tree/master/packages/istanbul-reports/lib" rel="noopener noreferrer"&gt;Istanbul reporters&lt;/a&gt; here.&lt;/p&gt;

&lt;p&gt;You can create the file &lt;code&gt;jest.config.js&lt;/code&gt; in the root of your project and add the following contents:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;coverageReporters&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;text-summary&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Adjust NPM Test Scripts &lt;em&gt;(Optional)&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;I like to define two npm scripts to run my tests. The &lt;code&gt;test&lt;/code&gt; script runs all tests and produces the code coverage. The &lt;code&gt;test:watch&lt;/code&gt; script doesn't produce code coverage but watches for file changes (using the integrated Jest watch mode).&lt;/p&gt;

&lt;p&gt;You can add the following to your &lt;code&gt;package.json&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ng test --coverage"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"test:watch"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ng test --watch"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h1&gt;
  
  
  Configure Debugging in VS Code&lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;As debugging via console-logs can be tedious, especially when your unit tests are running in headless mode, we want to use the great &lt;a href="https://code.visualstudio.com/Docs/editor/debugging" rel="noopener noreferrer"&gt;debugging features of Visual Studio Code&lt;/a&gt;. The goal is to have two launch scripts to run and debug all tests and only tests that belong to the currently opened file.&lt;/p&gt;

&lt;p&gt;If you press &lt;code&gt;F5&lt;/code&gt; or switch to the &lt;em&gt;Debug&lt;/em&gt; view in VS Code you will start one of these launch scripts. They are located in the file &lt;code&gt;.vscode/launch.json&lt;/code&gt;. You may add as many scripts as you want.&lt;/p&gt;

&lt;p&gt;If not already present, create the file &lt;code&gt;.vscode/launch.json&lt;/code&gt; and add the following two launch configurations to this file:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"0.2.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"configurations"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"request"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"launch"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Jest All"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"program"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"${workspaceFolder}/node_modules/@angular/cli/bin/ng"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"cwd"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"${workspaceFolder}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"args"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"--testMatch=&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;**/+(*.)+(spec|test).+(ts|js)?(x)&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"--runInBand"&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"console"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"integratedTerminal"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"internalConsoleOptions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"neverOpen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"disableOptimisticBPs"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"request"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"launch"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Jest Current File"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"program"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"${workspaceFolder}/node_modules/@angular/cli/bin/ng"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"cwd"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"${workspaceFolder}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"args"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"--testMatch=&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;**/+(*.)+(spec|test).+(ts|js)?(x)&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"--testPathPattern=${fileBasenameNoExtension}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"--runInBand"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"console"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"integratedTerminal"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"internalConsoleOptions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"neverOpen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"disableOptimisticBPs"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The first script &lt;code&gt;Jest All&lt;/code&gt; will run all tests. The second one &lt;code&gt;Jest Current File&lt;/code&gt; will run all specs with a path that matches the filename (without extension) of your currently opened file. This means if you have opened either &lt;code&gt;app.component.ts&lt;/code&gt; or &lt;code&gt;app.component.spec.ts&lt;/code&gt; it will run the test &lt;code&gt;app.component.spec.ts&lt;/code&gt; because both &lt;code&gt;app.component&lt;/code&gt; and &lt;code&gt;app.component.spec&lt;/code&gt; match the path of your spec. Smart, isn't it? 😉&lt;/p&gt;

&lt;p&gt;You can now set breakpoints in your specs or components that will be hit upon running these scripts. It gives you the ability to use the entire toolchain of Visual Studio Code's debugging features. You don't need to console-log all your variables, you can inspect them right inside your editor while stepping through the program execution. 🤯&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhv5onzjmxqazugxycgkm.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhv5onzjmxqazugxycgkm.gif" alt="Animated GIF showing debugging features of Visual Studio Code" width="600" height="337"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  (Un)known Issues
&lt;/h2&gt;

&lt;p&gt;It really had some trouble with the launch configurations. Sometimes it worked. Most times it causes Jest not to find any tests. After about half a day of searching, debugging and trying out different parameters, I found out that this is caused by the default &lt;code&gt;testMatch&lt;/code&gt; parameter. It appears that VS Code uses the &lt;code&gt;workspaceFolder&lt;/code&gt; and injects it into the parameter. However, it appears that the underlying Regex is not evaluated.&lt;/p&gt;

&lt;p&gt;The solution was to explicitly set the current working directory (&lt;code&gt;"cwd": "${workspaceFolder}"&lt;/code&gt;) and overwriting the default &lt;code&gt;testMatch&lt;/code&gt; parameter while removing the absolute path that VS Code injects itself (&lt;code&gt;"--testMatch=\"**/+(*.)+(spec|test).+(ts|js)?(x)\""&lt;/code&gt;).&lt;/p&gt;
&lt;h1&gt;
  
  
  Bright Future&lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;As mentioned in the beginning, I will probably look up my own blog post several times. At some point in future and if I have some spare time, I want to create my own &lt;a href="https://angular.io/guide/schematics" rel="noopener noreferrer"&gt;Angular Schematic&lt;/a&gt; to &lt;code&gt;ng new&lt;/code&gt; an Angular project and automate these initial steps. The schematic may then basically do the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;ng new&lt;/code&gt; with parameter &lt;code&gt;--minimal&lt;/code&gt; to prevent installing of any Karma or Jasmine related dependencies and files&lt;/li&gt;
&lt;li&gt;Remove E2E mentions from the &lt;code&gt;README&lt;/code&gt; and npm scripts for now (because they will still be added)&lt;/li&gt;
&lt;li&gt;Add Jest dependencies&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;tsconfig.spec.json&lt;/code&gt; and adjust the &lt;code&gt;tsconfig.json&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;test&lt;/code&gt; section to &lt;code&gt;angular.json&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;jest.config.js&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;test&lt;/code&gt; and &lt;code&gt;test:watch&lt;/code&gt; npm scripts&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;launch.json&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I hope that you will also find some value in this article. If you have any questions or remarks, just let me know. Your feedback is very welcome!&lt;/p&gt;

&lt;p&gt;You can find the sources for this guide on GitHub:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/MrCube42" rel="noopener noreferrer"&gt;
        MrCube42
      &lt;/a&gt; / &lt;a href="https://github.com/MrCube42/angular-jest-vscode-debugging" rel="noopener noreferrer"&gt;
        angular-jest-vscode-debugging
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Setup Jest for Angular with debugging in Visual Studio Code.
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;



&lt;h3&gt;
  
  
  References&lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;*There are schematics like the one from &lt;a href="https://github.com/briebug/jest-schematic" rel="noopener noreferrer"&gt;Briebug&lt;/a&gt; that add Jest with a single command. However, it does some things a bit different, I wanted to describe the steps to take without a schematic first and wanted to reduce the pain of setup debugging in VS Code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://angular.io/guide/testing" rel="noopener noreferrer"&gt;Official Angular Documentation on Testing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://jestjs.io/" rel="noopener noreferrer"&gt;Official Jest Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.angularindepth.com/angular-cli-ng-test-with-jest-in-3-minutes-v2-1060ddd7908d" rel="noopener noreferrer"&gt;Angular in Depth - Angular CLI: “ng test” with Jest in 3 minutes (v2)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/microsoft/vscode-recipes/tree/master/debugging-jest-tests" rel="noopener noreferrer"&gt;VSCode Recipies - Debugging Jest with VS Code&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.nrwl.io/debugging-nx-in-vs-code-cb1dbe9eeeba" rel="noopener noreferrer"&gt;Nrwl Blog - Debugging Nx in VS Code&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/@afdiaz_/no-tests-found-exiting-with-code-debug-jest-test-vscode-para-windows-73eded3f7cb7" rel="noopener noreferrer"&gt;alejandro@Medium - “No tests found, exiting with code“ debug Jest test, VSCode paraWindows.&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
  </channel>
</rss>
