Login Unit Tests
Explore how to implement and test user login functionality in Angular using unit tests and Cypress. Learn the setup of TestBed, mock services, page objects, and test scenarios for successful login and error handling.
We'll cover the following...
Now that our login functionality is working, we can move on. Below is our updated code. Use this to make further changes mentioned in the lesson.
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"lets-get-lunch": {
"projectType": "application",
"schematics": {},
"root": "",
"sourceRoot": "src",
"prefix": "app",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/lets-get-lunch",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.app.json",
"aot": true,
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"node_modules/bootstrap/dist/css/bootstrap.min.css",
"src/styles.css"
],
"scripts": [
"node_modules/jquery/dist/jquery.min.js",
"node_modules/bootstrap/dist/js/bootstrap.min.js"
]
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "6kb",
"maximumError": "10kb"
}
]
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "lets-get-lunch:build"
},
"configurations": {
"production": {
"browserTarget": "lets-get-lunch:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "lets-get-lunch:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.spec.json",
"karmaConfig": "karma.conf.js",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"node_modules/bootstrap/dist/css/bootstrap.min.css",
"src/styles.css"
],
"scripts": [
"node_modules/jquery/dist/jquery.min.js",
"node_modules/bootstrap/dist/js/bootstrap.min.js"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"tsconfig.app.json",
"tsconfig.spec.json",
"e2e/tsconfig.json"
],
"exclude": [
"**/node_modules/**"
]
}
},
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "lets-get-lunch:serve"
},
"configurations": {
"production": {
"devServerTarget": "lets-get-lunch:serve:production"
}
}
}
}
}
},
"defaultProject": "lets-get-lunch"
}Fixing test
The above widget shows our test in a failing state.
We must first import a few services and our LoginModule to make the test pass.
Update our test to what’s shown below to get the test to pass.
-
Begin by moving the
componentandfixturevariables outside of the outerdescribe. This avoids any scoping issues we’ll hit once we create the page object for our login page. -
Also add two additional variables,
authServiceandrouter, for local references to the services we’ll provide toLoginComponent. -
Update
configureTestingModuleby changing its defaultdeclarationstoimports, which importing ourLoginModule. Without this, the test will throw errors about the directives within our forms, becuaseFormsModuleis imported withinLoginModule. -
Chain
overrideComponentafter ...