In this lecture, we are going to write our first test to get started.
We will update the test file for the app component in your editor.
Search for this file in our project.
Let's rename the file by changing the extension from spec broken to spec.
Next, let's clear the contents of the file.
The first test will be writing is called a sanity test.
Similar to the development process of an application, you are bound to encounter errors while writing
tests.
You're going to spend time figuring out why a test isn't working.
In some cases, a test may not fail because there's anything wrong with your code.
The cause of a failure can come from your tool set.
It's common for tools to fail.
If a tool fails, we can safely assume there's nothing wrong with our code.
That brings us to the question how do we verify a tool fails?
The most common solution is to write a sanity test.
A sanity test is a test that always passes.
If a sanity test fails, we can assume there's something wrong with our tools.
I highly recommend writing a sanity test as your first test.
We're going to write a sanity test inside the app test file first at a function called Describe.
The described function is defined by the Jasmyn Library.
It's not uncommon for an application to have hundreds of tests.
Organizing your tests is very important.
It'll help you debug your application.
The described function allows us to create a test suite, which is another way of saying a group of
tests.
This function has two arguments.
The first argument is a description of the test suite.
Typically, developers will write the name of the component.
Let's set this argument to app component.
The second argument is a function let's pass in an arrow function.
Inside this function, we can begin defining our tests.
Let's run a function called it.
Immediately.
You'll notice we're not importing these functions before using them.
We're running our code through Jasmine by default.
Jasmine will define a set of global functions.
The IT function is predefined for us.
We don't have to do anything to make sure it's available.
The IT function allows us to write a single test.
Initially, the name of the function might sound strange.
However, throughout this section you're going to quickly discover that tests are verbose.
Keep following along and you'll see what I mean.
This function has two arguments.
The first argument should describe what the test is performing.
Jasmine and Karma will generate a test report.
The value of this argument will appear in the report.
The identifier should be short and concise.
Let's type the following should pass sanity test.
As I mentioned earlier, tests should be descriptive.
Typically, testing libraries will name their functions.
To help you describe the purpose of a test.
For example, we can read this line of code like so it should pass sanity test.
The second argument is a function that will run.
When we test our application, we will pass in an arrow function.
We're going to create what's called an assertion.
In the software development world, an assertion is an expression that evaluates to either true or false.
It's as simple as that.
If the assertion we create evaluates to false, the test fails.
If the assertion evaluates to true.
The test passes.
We're going to create an assertion that checks if true is equal to true.
We can use a comparison operator to create this expression, but Jasmine has an even better solution
inside the function.
Add the following.
Expect true to be true.
Thi.
We're using another global function defined by Jasmine called expect.
This function will return an object with functions that are called matters.
In the resource section of this lecture, I provide a link to a list of functions.
The expect function takes in a value.
It can be any type of value.
It will return an object with methods you can use to perform a test on the value you passed in.
These functions are referred to as matters.
The purpose of these functions is to perform.
A test on the value passed in this page provides a comprehensive list of available matters.
One of the measures we're using is called to be truth.
The to be truth.
You matcher can check a boolean value.
The Boolean value must be true.
Let's head back to our editors.
The tests we've written will always pass.
That's the goal of a sanity test.
We're creating a test that will always pass to verify our tools are working as intended.
If the test doesn't pass, we'll know that there's a problem with the tools and not our tests.
Let's switch over to the browser.
If the test does not show up, you may need to restart the command.
Jasmine has generated a test report.
It's a list of tests that have been executed.
In this list, the sanity test has passed.
This response indicates that the test passed.
We can begin to write more tests in the next set of lectures.
Let's try writing more practical tests.