Monday 16 March 2020

NG7 - How to unit test the angular application particular component or module

NG7 - How to unit test the angular application particular component or module :
-------------------------------------------------------------------------------

soln:1 - Ref:1
Ref: https://dzone.com/articles/running-karma-test-case-for-single-spec-file

Soln1: ref:2- shorthand
Step 1:
-------

Configure test.ts file inside src folder:

const context = require.context('./', true, /\.spec\.ts$/);
Replace /\.spec\.ts$/ with the file name you want to test. For example: /app.component\.spec\.ts$/

This will run test only for app.component.spec.ts.

Step 2
--------
Run ng test --code-coverage

Now Karma and Jasmine will check only Test-Demo.Spec.ts.

Before you push, you should run all the test cases and don’t push this file to the next repository.

Hope you enjoy.



Step 1.:- Module testing
--------
Run Karma on module base

const context = require.context('app/module-name/', true, /\.spec\.ts$/);
Replace require.context('./', true, with the module name you want to test. For example: 'custome-Module/', require.context(true)

No comments:

Post a Comment