{"id":2405,"date":"2022-12-02T14:35:06","date_gmt":"2022-12-02T13:35:06","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=2405"},"modified":"2023-01-25T09:35:18","modified_gmt":"2023-01-25T08:35:18","slug":"how-to-execute-only-one-test-spec-with-angular-cli","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2022\/12\/02\/how-to-execute-only-one-test-spec-with-angular-cli\/","title":{"rendered":"How to run only specific test spec file in Angular"},"content":{"rendered":"\n<p>\u00a0there is a way to run tests against only one selected spec file. For doing that, we should configure\u00a0<code>test.ts<\/code>\u00a0file inside\u00a0<strong><code>src<\/code>\u00a0<\/strong>folder. This file contains the below line of code which configures to include test files while running the\u00a0<code>ng test<\/code>\u00a0or\u00a0<code>ng t<\/code>\u00a0command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const context = require.context('.\/', true, \/\\.spec\\.ts$\/);<\/code><\/pre>\n\n\n\n<p>The first parameter is the repository path (<em>here, all files of an angular project<\/em>) the third parameter includes all spec files.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>let&#8217;s assume now, we have a\u00a0 &#8220;<code>brice.component.spec.ts<\/code>&#8221; \u00a0that we want to run a single component test file for debugging and testing it. There are multiple ways we can do it. <\/p>\n\n\n\n<h4>configure test.js to execute a single spec file<\/h4>\n\n\n\n<p>Replace\u00a0<code><strong>\/\\.spec\\.ts$\/<\/strong><\/code>\u00a0with the file name you want to test. For example:\u00a0<code><strong>\/brice.component\\.spec\\.ts$\/<\/strong><\/code><\/p>\n\n\n\n<p>then Runs the following command :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ng test &#91;project]\n\/\/or \nng t &#91;project]<\/code><\/pre>\n\n\n\n<p><em>This will run test only for\u00a0<code>app.component.spec.ts<\/code>.<\/em><\/p>\n\n\n\n<p><strong>PS: <\/strong><em><span class=\"has-inline-color has-vivid-cyan-blue-color\">if we want to run all test spec files inside a folder, we can configure them like this<\/span><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const context = require.context('.\/service', true, \/\\.spec\\.ts$\/);<\/code><\/pre>\n\n\n\n<p>this will run all spec files under the\u00a0<code>service<\/code>\u00a0folder.<\/p>\n\n\n\n<h4>command line to run single test spec file<\/h4>\n\n\n\n<p>use\u00a0<code>--include<\/code><\/p>\n\n\n\n<p>Angular CLI provides the\u00a0<code>--include<\/code>\u00a0option to include a regular expression for including single or multiple files\/folders.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ng test --include=**\/brice.component.spec.ts \/\/ single file\r\nng test --include'**\/service\/*.spec.ts \/\/ multiple files inside a folder<\/code><\/pre>\n\n\n\n<p>or<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ single file\r\nnpm run test -- --include src\/app\/brice.component.spec.ts\r\n\r\n\/\/ all files inside the service folder\r\nnpm run test -- --include src\/app\/service<\/code><\/pre>\n\n\n\n<h4>npm script to single test spec file<\/h4>\n\n\n\n<p>we can also configure the\u00a0<code>scripts<\/code>\u00a0tag in package.json<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\r\r\n    \"scripts\": {\r\n        \"singletest\": \"ng test --include=**\/brice.component.spec.ts\"\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<p>then run it with &#8220;<em><strong>npm run singletest<\/strong><\/em>&#8221; command.<\/p>\n\n\n\n<h4>jasmine and karma allow executing one component test spec<\/h4>\n\n\n\n<p>Jasmine provides\u00a0<code><em><span class=\"has-inline-color has-vivid-red-color\">fdescribe<\/span><\/em><\/code>\u00a0and\u00a0<code><em><span class=\"has-inline-color has-vivid-red-color\">fit<\/span><\/em><\/code>\u00a0allowing us to focus specific test cases to run it and skipped non-focused test case execution.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fdescribe('MyComponent', () => {\r\n  fit('should create', () => {\r\n    \/\/test for undefined \r\n  }\r\n}<\/code><\/pre>\n\n\n\n<ul id=\"block-66380076-3e05-4e9c-9105-277fe89a7032\"><li><code><em><strong>fdescribe<\/strong><\/em> -> Allows to run a group of tests<\/code><\/li><li><code><em><strong>fit -<\/strong><\/em>> Allows running a single test case<\/code><\/li><\/ul>\n\n\n\n<p><strong><span class=\"has-inline-color has-vivid-red-color\"><em>PS: <\/em><\/span><\/strong> <em>Please note that use\u00a0<span class=\"has-inline-color has-vivid-red-color\"><code>ddescribe<\/code>\u00a0<\/span>for jasmine version &lt;2.1 version, and use\u00a0<code><span class=\"has-inline-color has-vivid-red-color\">fdescribe<\/span><\/code>\u00a0for >2.1 version.<\/em><\/p>\n\n\n\n<h4>mocha and karma allow running a single component spec file <\/h4>\n\n\n\n<p>mocha provides <em><span class=\"has-inline-color has-vivid-red-color\">describe.only<\/span><\/em> and <code><em><span class=\"has-inline-color has-vivid-red-color\">it.only<\/span><\/em><\/code>\u00a0allowing us to focus specific test cases to run it and skipped non-focused test case execution.<\/p>\n\n\n\n<ul><li><code>describe.only<\/code>&#8211; Allows to run a group of tests<\/li><li><code>it.only<\/code>&nbsp;&#8211; Allows running a single test case<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>describe.only('MyComponent', () => {\r\n  it.only('should create', () => {\r\n    \/\/test for undefined \r\n  }\r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0there is a way to run tests against only one selected spec file. For doing that, we should configure\u00a0test.ts\u00a0file inside\u00a0src\u00a0folder. This file contains the below line of code which configures to include test files while running the\u00a0ng test\u00a0or\u00a0ng t\u00a0command. The first parameter is the repository path (here, all files of an angular project) the third [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1965,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[37],"tags":[643,161,217,642],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2405"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=2405"}],"version-history":[{"count":5,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2405\/revisions"}],"predecessor-version":[{"id":2502,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2405\/revisions\/2502"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1965"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=2405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=2405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=2405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}