Angular Unit Testing Interview Questions
Last updated
Was this helpful?
Last updated
Was this helpful?
Unit Tests sits at the base of the Testing Pyramid. This means they take lesser time to code and classes that you are testing should be more isolated. For example, a Login Feature normally has Validation and Authentication
logic. They should be written in two separate classes like LoginValidator
and LoginAuthenticator
. Based on these two classes, you write test scripts.
When you try to test Networking functions, like in LoginAuthenticator
, you should use Mock Response to test for both positive and negative responses.
Integration Tests sits at the next level after Unit Tests. Here, you will use Real Network Responses, by hitting the real server endpoint, and test that the small units you have written are able to work together.
UI Testing automates repetitive tasks to ensure that your most critical UI interactions keep working as you’re adding new features or refactoring your app’s codebase. It may also help new developers to understand the app as they just need to run the UI Test once, he is able to see the automated test script running the app and interpret the app’s user journey from there.
Going back to the example of a Login Feature, UI Tests will
Tap on the Username.
Key in “Username”.
Tap on Password.
Key in “******”.
Tap on Submit.
Here the script will mock a success or failure response to move the app to the next screen, or show an alert box if failed.
Just like how Integration Tests works for Unit Tests, E2E Tests should test the user journeys of the app by using Real Network Response by hitting the Real server endpoints.
a.) Resolving via TestBed
The TestBed
acts as a dummy Angular Module and we can configure it like one including with a set of providers like so:
We can then ask the TestBed
to resolve a token into a dependency using it’s an internal injector, like so:
If most of our test specs need the same dependency mocked the same way we can resolve it once in the beforeEach
function and mock it it there.
b.) Resolving via the inject function
The inject
function wraps the test spec function but lets us also inject dependencies using the parent injector in the TestBed
.
The first param is an array of tokens we want to resolve dependencies for, the second parameter is a function whose arguments are the resolved dependencies.
Using the inject
function:
Makes it clear what dependencies each spec function uses.
If each test spec requires different mocks and spys this is a better solution that resolving it once per test suite.
c.) Overriding the components providers
Before we create a component via the TestBed
we can override it’s providers. Lets imagine we have a mock AuthService
like so:
We can override the components providers to use this mocked AuthService
like so.
The syntax is pretty specific, it’s called a MetaDataOverride
and it can have the properties set
, add
and remove
. We use set
to completely replace the providers array with the values we’ve set.
d.) Resolving via the component injector When the component is created since it has it’s own injector it will resolve the AuthService itself and not forward the request to it’s parent TestBed injector.
If we wanted to get the same instance of dependency that was passed to the component constructor we need to resolve using the component injector, we can do that through the component fixture like so:
login.component.spec.ts
End to End: A helper robot that behaves like a user to click around the app and verify that it functions correctly. Sometimes called "functional testing" or e2e.
Integration: Verify that several units work together in harmony.
Unit: Verify that individual, isolated parts work as expected.
Static: Catch typos and type errors as you write the code.
Karma is a tool for executing source code against test code inside a browser environment. It supports the running of tests in each browser it’s configured for. Results are displayed on both the command line and on the browser for the developer to inspect which tests have passed or failed. Karma also watches the files and can trigger a test rerun whenever a file changes. At the root of the Angular project, we have the file karma.conf
that’s used to configure Karma.
The contents should look something like this:
Jasmine is a javascript testing framework that supports a software development practice called Behaviour Driven Development
, or BDD
for short. It’s a specific flavour of Test Driven Development (TDD)
.
Jasmine, and BDD in general, attempts to describe tests in a human readable format so that non-technical people can understand what is being tested.
For example if we wanted to test this function:
We would write a jasmine test spec like so:
The describe(string, function)
function defines what we call a Test Suite, a collection of individual Test Specs.
The it(string, function)
function defines an individual Test Spec, this contains one or more Test Expectations.
The expect(actual)
expression is what we call an Expectation. In conjunction with a Matcher it describes an expected piece of behaviour in the application.
The matcher(expected)
expression is what we call a Matcher. It does a boolean comparison with the expected value passed in vs. the actual value passed to the expect function, if they are false the spec fails.
Built-in matchers
Setup and teardown
beforeAll: This function is called once, before all the specs in describe test suite are run. afterAll: This function is called once after all the specs in a test suite are finished. beforeEach: This function is called before each test specification, it function, has been run. afterEach: This function is called after each test specification has been run.
We might use these functions like so:
Protractor is an end-to-end test framework for Angular. It runs your tests inside a real browser, interacting with it as real person would. Unlike unit tests, where we test individual functions, here we test the entire logic. Protractor is able to fill in forms, click buttons and confirm that the expected data and styling is displayed in the HTML document. Just like Karma, Protractor has its own configuration file at the root of your Angular project, protractor.conf
:
LoginComponent.ts
We create a User class which holds the model of a logged in user.
The button is sometimes disabled depending on the enabled input property value and on clicking the button we call the login function.
The component has an output event called loggedIn.
The component has an input property called enabled.
In the login function we emit a new user model on the loggedIn event.
LoginComponent.spec.ts
Testing @Inputs To test inputs we need to do things:
We need to be able to change the input property enabled on our component.
We need to check that the button is enabled or disabled depending on the value of our input property.
Testing @Outputs
Setup data in our input controls.
Trigger a click on our submit button, this synchronously emits the user object in the subscribe callback!
Since the output event is actually an Observable we can subscribe to it and get a callback for every item emitted. We store the emitted value to a user object and then add some expectations on the user object.
We can test inputs by just setting values on a components input properties. We can test outputs by subscribing to an EventEmitters observable and storing the emitted values on local variables.
Karma
Protractor
Karma
Protractor
Unit tests
End-to-end tests
Both of the above
Services
Components
Both of the above
AngularTestUtil
NgTest
NgTestBed
TestBed
Services
Pipes
Both of the above
None of the above
.test.ts
.spec.ts
Both of the above
createTestModule
configureTestModule
configureTestingModule
createTestingModule
createComponent
createTestingComponent
configureComponent
configureTestingComponent
createComponent
API on TestBed.True
False
The types of Testing looks are:
Unit Test
Integration Test
End to End (e2e) Test
The Unit Test is used to testing a single function, single components in Isolation. This is very fast.
In this Test, we are not able to say that everything is all right in the application. Just for a single Unit or function assure that working fine.
Example: app.component.ts
app.component.spec.ts
app.component.html
The Integration Testing is used to testing a component with templates and this testing containing more time as per comparison Unit Test.
The End to End Testing is used to testing the entire application looks like -
All User Interactions
All Service Calls
Authentication/Authorization of app
Everything of App
This is the actual testing of your append it is fast action. Unit testing and Integrations testing will do as fake calls but e2e testing is done with your actual Services and APIs calls.
Recommended Unit Testing Tools – Angular 4/2 and Angular
Karma
Jasmine and
QUnit
Test Function – After installing everything as per your project requirements, CREATE your project. The following Steps –
ng new YourTestProject
ng install
ng serve/ng test
Note – If you are going to development then type ng server command and if you want to test your project, you should type ng test command. After type ng test command and press inter. It’s taking some time to installing everything in your project for test.
describe()
– Test suit (just a function)
it()
- The spec or test
expect()
- Expected outcome.
Triple Rule of Testing –
Arrange - Create and Initialize the Components
Act - Invoke the Methods/Functions of Components
Assert - Assert the expected outcome/behaviour
The quick list of best practices.
Use beforeEach()
to Initialize the context for your tests.
Make sure the string descriptions you put in describe()
and it()
make sense as output
Use after()
and afterEach()
to clean-up your tests if there is any state that may bleed over.
If any one test is over 15 lines of code, you may need to refactor the test
Example: Login Testing login.component.ts
login.component.spec.ts
login.component.html
Using TestBed
The CLI can run unit tests and create code coverage reports. Code coverage reports show you any parts of our code base that may not be properly tested by your unit tests.
To generate a coverage report run the following command in the root of your project.
When the tests are complete, the command creates a new /coverage
folder in the project. Open the index.html
file to see a report with your source code and code coverage values.
If you want to create code-coverage reports every time you test, you can set the following option in the CLI configuration file, angular.json
:
Code coverage enforcement
The code coverage percentages let you estimate how much of your code is tested. If your team decides on a set minimum amount to be unit tested, you can enforce this minimum with the Angular CLI.
For example, suppose you want the code base to have a minimum of 80% code coverage. To enable this, open the Karma test platform configuration file, karma.conf.js
, and add the following in the coverageIstanbulReporter
: key.
The thresholds property causes the tool to enforce a minimum of 80% code coverage when the unit tests are run in the project.↥ back to top
Installation
Mocha is a JavaScript test framework running on Node.js and in the browser. Mocha allows asynchronous testing, test coverage reports, and use of any assertion library. Mocha gives us a Suite for describing, running and building tests but it does not give us a way to check values.
Chai is a BDD / TDD assertion library for NodeJS and the browser that can be delightfully paired with any javascript testing framework.
Basically, mocha is a framework and chai is a library. Let's go a little deeper in mocha. Mocha uses hooks to organize its structure.
describe(): It's used to group, which you can nest as deep;
it(): It's the test case;
before(): It's a hook to run before the first it() or describe();
beforeEach(): It’s a hook to run before each it() or describe();
after(): It’s a hook to run after it() or describe();
afterEach(): It’s a hook to run after each it() or describe();
Scenario 1: One test case
In this case, we can just call a solitary it()
, mocha will run this only test.
Scenario 2: A nested test case
In this case, we can nest some describes()
hooks and finally call it()
to execute the test.
Scenario 3: Two test cases in one test
Here, inside the describe()
we have two it()
that will execute the tests.
Scenario 4: Run just once before the first test case
In this scenario, before the first it()
mocha will execute what is inside before()
, but only once.
Scenario 5: Run once before each test case
On the contrary of before()
, beforeEach() is executed each time for each it()
[or describe()
] that we have. If we have one it()
, it will be executed just once. if we have two it()
it will be executed twice and so go on.
Scenario 6: Two tests in a big test
In this last scenario, mocha will nest the describe()
and execute it()
.
Run
Q. What are some of the different tests types you can write?
Q. How do you mock a service to inject in an integration test?
Q. How do you mock a module in an integration test?
Q. How do you test a component that has a dependency to an async service?
Q. What is the difference between 'async()' and 'fakeAsync()'?