<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dirk Luijk on Medium]]></title>
        <description><![CDATA[Stories by Dirk Luijk on Medium]]></description>
        <link>https://medium.com/@dirkluijk?source=rss-8db9b1101a09------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*6XQcC1BN20IXIy2UqbqLTw.jpeg</url>
            <title>Stories by Dirk Luijk on Medium</title>
            <link>https://medium.com/@dirkluijk?source=rss-8db9b1101a09------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 08:15:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dirkluijk/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[ Spectator V4: A Powerful Tool to Simplify Your Angular Tests!]]></title>
            <link>https://medium.com/@dirkluijk/spectator-v4-a-powerful-tool-to-simplify-your-angular-tests-bd65a0bf317e?source=rss-8db9b1101a09------2</link>
            <guid isPermaLink="false">https://medium.com/p/bd65a0bf317e</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[open-source]]></category>
            <dc:creator><![CDATA[Dirk Luijk]]></dc:creator>
            <pubDate>Tue, 27 Aug 2019 15:07:20 GMT</pubDate>
            <atom:updated>2019-08-27T15:07:20.738Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NVPI9-5ITbDyJcl0zdGjSw.png" /></figure><p>We are proud to release the next major version of Spectator. In this version, we make Angular testing easier than ever before.</p><p>We believe that its simple API is what makes Spectator great. It is easy to learn and saves you a lot of boilerplate code. In Spectator v4, we wanted to improve this experience by making the API even more consistent and make every feature easily accessible.</p><blockquote><em>With Spectator, unit testing your Angular app is enjoyable again</em></blockquote><p>Internally, we did quite some refactoring to enforce the internal code reuse. This makes Spectator more robust and predictable. This also allowed us to add some awesome features easily.</p><h3>Why Spectator?</h3><p>Spectator helps you get rid of all the boilerplate grunt work, leaving you with readable, sleek and streamlined unit tests.</p><p>It allows you to write tests for components, directives, services, and more, without having to learn TestBed, ComponentFixture, and DebugElement APIs.</p><h3>Installation</h3><pre>$ npm install @ngneat/spectator --save-dev</pre><h3>Spectator V4 Overview</h3><p>In Spectator v4, we provide the following factory functions to unit test Angular building blocks:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/27731aab37c0648648133b681a2b6deb/href">https://medium.com/media/27731aab37c0648648133b681a2b6deb/href</a></iframe><p>Every factory supports a wide range of features, including:</p><ul><li><strong>Auto-mocking services</strong> for both Jasmine and Jest</li><li>Access <strong>injected services</strong> in a type-safe way</li><li>Support for <strong>entry components</strong></li><li><strong>Override providers </strong>easily</li><li>Powerful <strong>DOM querying</strong></li><li><strong>Event dispatching</strong>, like click, focus, blur and other (custom) events</li><li><strong>Global injections</strong> to reduce boilerplate code even more</li><li><strong>Awesome matchers</strong> for Jasmine/Jest</li></ul><p>On top of that, some factories provide additional specific features. Let’s take a look at some of them:</p><h3>Spectator Improvements</h3><p>Spectator loves component testing. In v4 we added some new features on top of the existing ones.</p><h4>Basic Spectator Example</h4><p>For a reference, this is how a simple Spectator test looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9b8f4919b53ddffe5c660f8613382d7c/href">https://medium.com/media/9b8f4919b53ddffe5c660f8613382d7c/href</a></iframe><p>You can also pass additional options to the factory.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/36fc2eaaa0e55457e79a5137605bb8f8/href">https://medium.com/media/36fc2eaaa0e55457e79a5137605bb8f8/href</a></iframe><p>Please find all the possible options in our <a href="https://github.com/ngneat/spectator/blob/master/README.md">documentation</a>.</p><h4>Global Queries</h4><p>Sometimes, we don’t want to query within your component, but on a global level. For example, when we are testing an overlay.</p><p>In Spectator v4, we can simple do a global query using the root key:</p><pre>const element = spectator.query(&#39;.some-overlay&#39;, {<br>  root: true<br>})</pre><p>This is useful if we want to access the element itself. If we wish to perform some tests right away, we can also use the power of Spectator matchers:</p><pre>expect(&#39;.some-overlay&#39;).toHaveText(&#39;Lorem ipsum&#39;);</pre><p>This will perform a global query as well.</p><h4>Global Injections</h4><p>It is now possible to add global configuration for Spectator. Use the defineGlobalsInjections() helper in your test setup to share some options across all Spectator tests:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/104a35fbb72672f60eeb921ce1abba2b/href">https://medium.com/media/104a35fbb72672f60eeb921ce1abba2b/href</a></iframe><p>This is very useful if you have modules, providers, or declarations that you need for (almost) every component. This way, you don’t have to repeat yourself.</p><h4>Additional Helpers</h4><p>We added the following helper functions to make DOM interaction easier:</p><ul><li>spectator.blur()</li><li>spectator.focus()</li><li>spectator.keyboard.pressKey(&#39;a&#39;)</li></ul><p>In addition to that, we still provide all the <a href="https://github.com/ngneat/spectator/blob/master/README.md#events-api">existing helpers</a> of Spectator 3.x. Every helper accepts an optional selector. If not provided, the component host element is targeted.</p><h4>Factory Overrides</h4><p>In every Spectator v4 factory, we can now consistently override the following options:</p><ul><li>providers: override providers for a specific test case</li><li>detectChanges: disable initial change detection if needed</li><li>props: override component/directive properties</li><li>hostProps: override host component properties</li><li>Route options (see <a href="#92c4">Routing support</a>)</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bcf1543647ca6a525517c9790d47fa6b/href">https://medium.com/media/bcf1543647ca6a525517c9790d47fa6b/href</a></iframe><h3>Mock Providers</h3><p>We can auto-mock providers by using the mocks option. Based on the class definition, it will automatically create a mock object by using Jasmine or Jest spies.</p><p>To mock components, directives, or pipes, we recommend you to use the library <a href="https://github.com/ike18t/ng-mocks">ng-mocks</a>. For example, if you want to replace any child component with a dummy implementation, you can do the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/98a6d30c89fa57b02aaf0cd0f86a8b33/href">https://medium.com/media/98a6d30c89fa57b02aaf0cd0f86a8b33/href</a></iframe><h4>Advanced Spectator Example</h4><p>Check out some of the advanced features in the snippet below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c7c25843378695bc1e356283d81b0e0/href">https://medium.com/media/9c7c25843378695bc1e356283d81b0e0/href</a></iframe><h3>Spectator with Host Improvements</h3><p>With Spectator, it is also possible to test a component together with a host component. By using an additional host component as a parent, it is easier to test the <em>interaction</em> of a component with its outside world (inputs, outputs).</p><h4>Basic Example</h4><p>Please find a basic example below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/38d0b47513276406739e44d642cd0be4/href">https://medium.com/media/38d0b47513276406739e44d642cd0be4/href</a></iframe><h4>What’s New?</h4><p>In Spectator v4, the SpectatorHost API shares all the mentioned features of Spectator, with on top of that:</p><ul><li>Defining a default template for the host component</li><li>Overriding this template</li><li>Overriding the host component properties</li><li>Using a custom host component (with complete type-safety)</li></ul><h4>Using a Custom Host Component</h4><p>For more advanced cases, it might be useful to use a custom host component. For example, when you want to define specific host properties or methods:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1e251eed118b255b846be05786fb5cc4/href">https://medium.com/media/1e251eed118b255b846be05786fb5cc4/href</a></iframe><h3>🎉 New! Spectator for Directives</h3><p>In Spectator v4, we added a new SpectatorDirective API for testing directives. A directive always needs a <em>host component</em>, so it looks a lot like the SpectatorHost API, with some small differences:</p><ul><li>All queries are relative from the host element</li><li>For event dispatching, if no query is provided, the directive’s element is the default target</li></ul><p>See its beauty in the example below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6847445998789dc0ad8ddabcc6b0c5ee/href">https://medium.com/media/6847445998789dc0ad8ddabcc6b0c5ee/href</a></iframe><h3>🎉 New! Spectator for Routing</h3><p>Spectator v4 introduces a new factory for <em>routed components</em>: components that are associated with a route (from @angular/router). They usually respond to routing changes (e.g., they rely on an ActivatedRoute).</p><p>The new SpectatorRouting API provides all the features of Spectator, with additionally:</p><ul><li>It automatically provides a stub implementation for ActivatedRoute</li><li>You can configure the params, queryParams, fragments, and data. You can also update them to test how your component reacts to changes.</li><li>It provides a stub for RouterLink directives</li></ul><h4>Additional options</h4><p>We can use the following additional options (they are all optional):</p><ul><li>params: Params - a map of initial route parameters</li><li>queryParams: Params - a map of initial route query parameters</li><li>fragment: string - an initial route fragment</li><li>data: Data - a map of initial route data</li></ul><h4>SpectatorRouting API</h4><p>We can update the route options and trigger navigation using the following methods:</p><ul><li>triggerNavigation(options?: RouteOptions): void</li><li>setRouteParam(name: string, value: string): void</li><li>setRouteQueryParam(name: string, value: string): void</li><li>setRouteData(name: string, value: string): void</li><li>setRouteFragment(fragment: string | null): void</li></ul><h4>Basic Routing Example</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1420c0239ba8f77cc7c0d00c31835771/href">https://medium.com/media/1420c0239ba8f77cc7c0d00c31835771/href</a></iframe><h3>Spectator for Services</h3><p>Testing services with Spectator is easy. In Spectator v4 we added a special factory for creating services: createServiceFactory which returns a factory for SpectatorService.</p><p>Of course, we can stick to plain-old class testing, but if we want to benefit from dependency injection and Spectator features, this is an easy alternative.<br>We can access the created service using the spectator.service property.</p><h4>Basic example</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3941b68728c4b336f3425c96ce0992c9/href">https://medium.com/media/3941b68728c4b336f3425c96ce0992c9/href</a></iframe><h4>Advanced Example</h4><p>In the following example, we can see that we can define imports, providers mocks and overrides for SpectatorService as well.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f702e0deb45ccf14e017ba57b68ac48/href">https://medium.com/media/0f702e0deb45ccf14e017ba57b68ac48/href</a></iframe><h3>Spectator for HTTP</h3><p>Data services are services that perform HTTP requests. In Spectator, there is a special factory for creating data services: createHttpFactory. It returns a factory for SpectatorHttp, which has the following members:</p><ul><li>service — returns the created service</li><li>expectOne(URL: string, method: HttpMethod) — to assert a request and returning the TestRequest</li><li>httpClient — to access the HttpClient from Angular</li><li>controller — to access the HttpTestingController from Angular</li></ul><h4>Basic HTTP example</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e09d731386bac51c007c4e44468f0e5b/href">https://medium.com/media/e09d731386bac51c007c4e44468f0e5b/href</a></iframe><h3>Additional Improvements</h3><ul><li>Improved general type-safety</li><li>Jasmine 3 support</li><li>Removed deprecated APIs from v3</li><li>Added support for mocking component providers</li><li>Added VSCode snippets package</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/656/1*irXnQ_CXr_b0gE67EGSmEg.png" /></figure><h3>🦊 New Docs</h3><p>We wrote our documentation from scratch, detailing everything the spectator has to give. We also migrate from Gitbook to Github so it will be easier to contribute and track docs changes.</p><p>We also created an online testing playground in <a href="https://ng-run.com/edit/VIYdwKFVnEgIc2m6tQYz?test=true">ng-run</a>. Thanks to <a href="https://medium.com/u/d59a9e801370">Alexey Zuev</a> for the cooperation.</p><h3>⭐️ We are Now Part of @ngneat!</h3><p>As of v4, Spectator has moved to the @ngneat organization. ngneat provides neat Angular libraries with high-quality standards.</p><p>Please check out some of our <a href="https://github.com/ngneat">other projects</a>!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OGU-9uv-7Cy9igWJfGvCWw.png" /></figure><h3>Migrating from 3.x</h3><p>We wrote a <a href="https://github.com/ngneat/spectator/blob/master/MIGRATION.md">migration guide</a> to make updating to Spectator 4 as easy as possible.</p><h3>What’s Next?</h3><p>Spectator v4 is a very stable release, and we don’t expect to have any breaking changes soon.</p><p>We might add some additional features to support edge cases even better. Please let us know if you miss a feature.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bd65a0bf317e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>