| Start |
|---|
| Installing the Jasmine testing framework The most common way to test Angular 2 apps is with the Jasmine test framework. Jasmine allows you to test your code in the browser. InstallTo get started, all you need is the VerifyTo verify that Jasmine is set up properly, create the file |
| Setting up testing with Gulp, Webpack, Karma and Jasmine The first thing we need is to tell karma to use Webpack to read our tests, under a configuration we set for the webpack engine. Here, I am using babel because I write my code in ES6, you can change that for other flavors, such as Typescript. Or I use Pug (formerly Jade) templates, you don't have to. Still, the strategy remains the same. So, this is a webpack config: And then, we need a karma.config.js file to use that webpack config: So far, we have told Karma to use webpack, and we have told it to start at a file called karma.shim.js. this file will have the job of acting as the starting point for webpack. webpack will read this file and use the import and require statements to gather all our dependencies and run our tests. So now, let's look at the karma.shim.js file: In essence, we are importing TestBed from angular core testing, and initiating the environment, as it needs to be initiated only once for all of our tests. Then, we are going through the src/app directory recursively and reading every file that ends with .spec.js and feed them to testContext, so they will run. I usually try to put my tests the same place as the class. Personat taste, it makes it easier for me to import dependencies and refactor tests with classes. But if you want to put your tests somewhere else, like under src/test directory for example, here is you chance. change the line before last in the karma.shim.js file. Perfect. what is left? ah, the gulp task that uses the karma.config.js file we made above: I am now starting the server with the config file we created, telling it to run once and don't watch for changes. I find this to suite me better as the tests will run only if I am ready for them to run, but of course if you want different you know where to change. And as my final code sample, here is a set of tests for the Angular 2 tutorial, "Tour of Heroes". Noteworthy in this is how we have beforeEach() configure a test module and create the component in test, and how we call detectChanges() so that angular actually goes through the double-binding and all. Notice that each test is a call to async() and it always waits for whenStable promise to resolve before examining the fixture. It then has access to the component through componentInstance and to the element through nativeElement. There is one test which is checking the correct styling. as part of the Tutorial, Angular team demonstrates use of styles inside components. In our test, we use getComputedStyle() to check that styles are coming from where we specified, however we need the Window object for that, and we are getting it from the element as you can see in the test. |
| Testing Http Service Usually, services call remote Api to retrieve/send data. But unit tests shouldn't do network calls. Angular internally uses
|
| Testing Angular Components - Basic The component code is given as below. For angular testing, angular provide its testing utilities along with the testing framework which helps in writing the good test case in angular. Angular utilities can be imported from In the above example, there is only one test case which explain the test case for component existence. In the above example angular testing utilities like
|
Read Article
https://develop.horje.com/learn/1434/reference