Sébastien Lucas / Dev wiki
  • Blog Wiki
  • Story book
  • Developers comics
  • Angular
    • Angular CLI + Angular.json
    • ngTemplateOutlet
    • Angular Schematic
    • Interceptor
    • Micro frontend + Mono repo
    • Build a library module
    • Error handling
    • Virtual and infinite scroll
    • Angular i18n
    • Debug Angular
    • Angular LifeCycle Hook
    • Nested forms in Angular
    • Angular Recipes
    • Master component in Angular
    • Perfomance optimization
    • Service Workers + PWA
    • Mobile
    • Electron app / Desktop app
    • Unit test & Angular
      • Unit test recipes (Jasmine / Karma / Angular)
      • Testing services with httpMock
    • Communication between components
    • Angular snippet library
    • Release and version management
    • Angular tutorial selection
    • UI components libraries
    • Angular libraries
    • Angular Tutorials
    • NGRX
      • Angular NGRX / Tips and code examples
      • Angular new Redux (alternative to ngrx)
      • NGRX unit test
      • Angular ngrx / Basics
    • Angular 2/Angular 1 differences
  • Graphql
  • Three.js
  • Ag grid
  • Open source BIM and 3D
  • Javascript
    • Null vs undefined
    • Html API
    • Await API
    • Debug memory leaks
    • Offline and PWA
    • Javascript tutorials
    • Javascript recipes
    • Bluebird missing docs
    • Alternative to lodash with ES6
    • ES6 syntax the best parts
    • Vanilla JS
  • RXJS
    • Docs
    • Recipes
    • Mock API and sandbox
    • Observables rxjs recipes
    • Combination operators
  • NODE.js
    • Environment variables
    • Fix CORS
    • Pagination requests in API
    • API tests
    • Node.js security
    • Learn node.js
    • Best libraries for node.js
    • Mongoose recipe
    • Debug node.js
  • Gatsby / React
    • Hooks
    • React concepts
    • Gatsby internationalisation
  • Ghost blog
  • Services for developers
    • SaaS images services
    • Airtable API examples
  • MISC
    • JIRA debugging
    • Wordpress plugins
    • Interview Sébastien Lucas
    • English expression
    • True recipes
    • Science podcast
  • AI
    • Machine learning open source
    • Tensor flow
    • Machine learning
    • Code examples
    • Courses and tutorials
    • Datasets
    • The Future of AI
    • Learn algo and data structures
  • Typescript
    • Generic types
    • Typescript recipes
    • Advanced types
      • Conditional types
      • Type guards
    • d.ts files
  • Docker
    • Starting with docker
    • Dockerise simple node app
    • Docker by aymen el amri
  • Mongodb
    • Pattern and data modeling
  • Devops
    • Heroku
    • Scaleway
    • Github template
    • Gitlab CI
    • http2
    • nginx
    • zsh
    • CI Continuous integration
    • DNS
    • Devops resources
    • Gcloud useful commands
    • Authenticate Gcloud
    • Documentation generators
    • Firebase database
  • Developers ressources
    • Online platform coding
      • Online courses
      • Coding games
      • Coding test platforms
      • Links to check
    • Good developers blogs
    • Nice open source project / github
  • Tooling
    • The chrome urls
    • Linux Mac tips
    • Webstorm configuration
    • Develop in Windows
    • Mac debug hardware
    • Mac Setup for a developer
    • Chrome extension
    • Develop toolbox
  • HTML / CSS
    • Tailwind
    • Css grid
    • ☘️Nice styles & generators
    • Favicon
    • Flexbox grid
    • Flexbox layout
    • PUG templates tips
    • Html and css references
    • Css snippets
    • SASS-CSS libraries
    • New things in scss
    • SASS best practices
    • Style lint
  • Tests
    • Cypress
      • Learn cypress and more tests
      • Cypress commands
      • Cypress plugins
      • Page object && app actions
      • Flaky tests
    • Mobile test
    • BDD + Cucumber
    • Puppeteer
    • Type of tests + Ressources
    • Jasmine test
    • Mock, fake, stub in unit tests
    • E2e tests with protactor
    • Mocha
  • REVIT & AEC tools
  • Git
    • Git commits
    • Git tips
    • Git hooks
    • Set up a mono repo
  • Design Pattern
    • Functional Programming
  • Job board / Remote jobs
  • SVG
  • JSON
  • Github
    • Forking a github repo
  • NPM
    • Private NPM packages
    • Publish to NPM with np
  • Yarn
    • Yarn evolution / 2, 3...
    • Yarn Linking
Powered by GitBook
On this page
  • JASMINE before / after functions
  • Disable temporarily a test
  • The hierarchy of unit test
  • What do Karma do
  • Jasmine Spy
  • Remove code duplication in Jasmine
  • Debug Jasmine test

Was this helpful?

  1. Tests

Jasmine test

PreviousType of tests + RessourcesNextMock, fake, stub in unit tests

Last updated 6 years ago

Was this helpful?

Very good introduction to Jasmine

Video explanation : talk about unit test, what is it, test in general... The jasmine Documentation

JASMINE before / after functions

Run before all test suite tests

  • beforeAll

  • afterAll

Run before each test, before this test or after this test

  • beforeEach

  • afterEach

Disable temporarily a test

Put x in front of describe (the test suite = group of tests) xdesribe and x in front of it (the test) so xit

It allows to disable a test temporarily though still listing it and not commenting it To be able to see the test code.

Put f in front of it to focus only on one test (only run one test) so write fit('the test to focus on')

The hierarchy of unit test

  • A test suite : a group of test (describe)

  • A test spec : one individual test (it)

  • A test expectation : what we are testing (expect)

What do Karma do

Karma is a test runner, it is not compulsory to use it. Though it is easier :

  • It reruns the tests when code change

  • It open a separate browser (headless)

  • It gets Jasmine result in the command line

Jasmine Spy

Remove code duplication in Jasmine

Debug Jasmine test

  • Error due to a missing selector with debugElement

const myElement = fixture.debugElement.query(By.css(.missing-class));
// myElement.nativeElement will trigger an error

Failed: Uncaught (in promise): TypeError: Cannot read property 'nativeElement' of undefined

  • angular test with data that come from a resolver

Jasmine doc on spies

Jasmine Spy spreadsheet

Duplication using a way to invoke describe by using a function

Git that explain why using this in the test context. Some recent comment explained how to use it with typescript as well.

Discussion about how to write units tests with multiple expect in it or not, parameterized tests...

Example of parameterized test in JavaScript

https://codecraft.tv/courses/angular/unit-testing/jasmine-and-karma
https://jasmine.github.io/api/3.0/global.html#beforeEach
https://github.com/karma-runner/karma/releases
https://www.htmlgoodies.com/html5/javascript/spy-on-javascript-methods-using-the-jasmine-testing-framework.html
https://jasmine.github.io/api/3.0/global.html#spyOn
http://tobyho.com/2011/12/15/jasmine-spy-cheatsheet/
https://juristr.com/blog/2014/10/avoid-test-code-duplication-jasmine
https://gist.github.com/traviskaufman/11131303
https://www.reddit.com/r/javascript/comments/60a10r/improve_your_javascript_unit_testing_with/
https://gist.github.com/sinsunsan/cb1779b3b83a33fe8446bf27ef7f6149
https://stackoverflow.com/questions/42656045/angular2-testing-and-resolved-data-how-to-test-ngoninit