Fireship · @fireship

Angular Testing Quick Start

Uploaded December 3, 2017 · Length 12:15

Angular Testing Quick Start

Summary

Learn how to set up and write tests for an Angular application using Jasmine and Karma.

  • angular testing
  • jasmine
  • karma
  • test-driven development
  • angular tutorial

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    test-driven development is the single

  2. 0:01

    most powerful tool you have for

  3. 0:02

    preventing bugs within your application

  4. 0:04

    that's not just my opinion but it's a

  5. 0:06

    scientifically proven fact in exchange

  6. 0:08

    for effort and productivity you get

  7. 0:10

    better quality software that is more

  8. 0:12

    maintainable long-term lucky for you

  9. 0:14

    angular comes built-in with jasmine and

  10. 0:16

    karma so testing is really easy to get

  11. 0:18

    started with in this video I'll give you

  12. 0:20

    a tour of angular's testing tools and

  13. 0:22

    show you how to deal with third-party

  14. 0:23

    dependencies like firebase if you're new

  15. 0:26

    to the channel make sure to subscribe

  16. 0:27

    and you can always get the full source

  17. 0:29

    code on angular firebase com the first

  18. 0:32

    thing I'm going to do is generate a

  19. 0:33

    brand new angular 5 app and then I'll

  20. 0:35

    show you what all the different testing

  21. 0:36

    files do the angular CLI generates all

  22. 0:39

    of the boilerplate code automatically so

  23. 0:41

    we'll start by looking at the karma

  24. 0:43

    config file karma works by setting up a

  25. 0:45

    web server that will run all of your

  26. 0:47

    tests automatically every time a file

  27. 0:49

    changes you have some configuration

  28. 0:51

    options here if you want to customize

  29. 0:52

    that experience you also have a

  30. 0:55

    protractor config file which is

  31. 0:57

    specifically for end-to-end tests and

  32. 0:59

    end testing uses the browser to simulate

  33. 1:01

    how the end user sees your app so it's

  34. 1:04

    completely decoupled from your main app

  35. 1:05

    code that's why we have this été

  36. 1:07

    directory in the root of the project and

  37. 1:08

    I'll show you how to use it in a future

  38. 1:10

    video now moving into the source

  39. 1:12

    directory we have a test ts file it's

  40. 1:16

    required by karma to load all your spec

  41. 1:17

    files you shouldn't really have to

  42. 1:19

    change anything in here now let's take a

  43. 1:21

    look at our first real test which is the

  44. 1:22

    app component spec ts file this just

  45. 1:25

    looks like a big jumble of code so let's

  46. 1:27

    break the test down into its fundamental

  47. 1:29

    parts the first part of any test is to

  48. 1:31

    describe the test suite in Jasmine you

  49. 1:34

    do this by using the describe keyword

  50. 1:36

    followed by whatever you're testing the

  51. 1:38

    purple text represents jasmine the green

  52. 1:40

    represents what we're going to write we

  53. 1:42

    write all the tests inside this describe

  54. 1:44

    block each one starting with it so we're

  55. 1:47

    going to say it should be awesome then

  56. 1:49

    you'll write an expectation that

  57. 1:50

    verifies whether this is true or false

  58. 1:52

    so with jasmine we could do something

  59. 1:54

    like expect component to be awesome the

  60. 1:57

    end result is that you've described the

  61. 1:59

    behavior of your code in a way that can