Reputation: 29
I am new to angular and ionic, I need to know how to unit test, specifically, are there any test files that I write my test code in?
I am currently working on ionic 3.
I have installed karma and jasmine using npm, my devDependencies look like this
"devDependencies": {
"@ionic/app-scripts": "3.2.1",
"jasmine-core": "^3.3.0",
"karma": "^4.0.0",
"typescript": "~3.2.4"
},
Say I have a function like this in a component.ts file:
add (a:number,b:number) {
return a+b
}
Where do I write the test function for it? also how do I run the test function? how do I see the results?
Upvotes: 1
Views: 895
Reputation: 549
Usually unit tests for angular/ typescript will be written in separate files which are typically named as .spec.ts (which can be stored in the same directory as the component file or in a tests folder).
in the package.json under scripts section we declare the npm alias to trigger the ng tests.
"scripts": {
.....
"test": "ng test"
...
}
which can be triggered in console running in package.json directory
npm run test
karma picks up default settings in karma.conf.js and looks for files which are named in format **.spec.ts. (which can be configured)
The success / failure messages will be displayed in the console. you can also use other node modules like istanbul and generate nice colorful stats which can be view-able along with coverage report.
Upvotes: 1