Fireship · @fireship
Angular Testing Quick Start
Uploaded December 3, 2017 · Length 12:15
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.
- 0:00
test-driven development is the single
- 0:01
most powerful tool you have for
- 0:02
preventing bugs within your application
- 0:04
that's not just my opinion but it's a
- 0:06
scientifically proven fact in exchange
- 0:08
for effort and productivity you get
- 0:10
better quality software that is more
- 0:12
maintainable long-term lucky for you
- 0:14
angular comes built-in with jasmine and
- 0:16
karma so testing is really easy to get
- 0:18
started with in this video I'll give you
- 0:20
a tour of angular's testing tools and
- 0:22
show you how to deal with third-party
- 0:23
dependencies like firebase if you're new
- 0:26
to the channel make sure to subscribe
- 0:27
and you can always get the full source
- 0:29
code on angular firebase com the first
- 0:32
thing I'm going to do is generate a
- 0:33
brand new angular 5 app and then I'll
- 0:35
show you what all the different testing
- 0:36
files do the angular CLI generates all
- 0:39
of the boilerplate code automatically so
- 0:41
we'll start by looking at the karma
- 0:43
config file karma works by setting up a
- 0:45
web server that will run all of your
- 0:47
tests automatically every time a file
- 0:49
changes you have some configuration
- 0:51
options here if you want to customize
- 0:52
that experience you also have a
- 0:55
protractor config file which is
- 0:57
specifically for end-to-end tests and
- 0:59
end testing uses the browser to simulate
- 1:01
how the end user sees your app so it's
- 1:04
completely decoupled from your main app
- 1:05
code that's why we have this été
- 1:07
directory in the root of the project and
- 1:08
I'll show you how to use it in a future
- 1:10
video now moving into the source
- 1:12
directory we have a test ts file it's
- 1:16
required by karma to load all your spec
- 1:17
files you shouldn't really have to
- 1:19
change anything in here now let's take a
- 1:21
look at our first real test which is the
- 1:22
app component spec ts file this just
- 1:25
looks like a big jumble of code so let's
- 1:27
break the test down into its fundamental
- 1:29
parts the first part of any test is to
- 1:31
describe the test suite in Jasmine you
- 1:34
do this by using the describe keyword
- 1:36
followed by whatever you're testing the
- 1:38
purple text represents jasmine the green
- 1:40
represents what we're going to write we
- 1:42
write all the tests inside this describe
- 1:44
block each one starting with it so we're
- 1:47
going to say it should be awesome then
- 1:49
you'll write an expectation that
- 1:50
verifies whether this is true or false
- 1:52
so with jasmine we could do something
- 1:54
like expect component to be awesome the
- 1:57
end result is that you've described the
- 1:59
behavior of your code in a way that can


