2015-10-09 13:33:12 -04:00
include ../../../../_includes/_util-fns
2015-11-10 13:31:46 -05:00
:marked
2016-01-19 04:15:26 -05:00
In this chapter we'll setup the environment for testing our sample application and write a few easy Jasmine tests of the app's simplest parts.
2015-10-14 23:25:19 -04:00
We'll learn:
- to test one of our application classes
- why we prefer our test files to be next to their corresponding source files
- to run tests with an `npm` command
2016-02-11 18:08:06 -05:00
- load the test file with SystemJS
2015-10-14 23:25:19 -04:00
.callout.is-helpful
header Prior Knowledge
2015-11-10 13:31:46 -05:00
:marked
2015-10-14 23:25:19 -04:00
The Unit Testing chapters build upon each other. We recommend reading them in order.
We're also assuming that you're already comfortable with basic Angular 2 concepts and the tools
we introduced in the [QuickStart](../quickstart.html) and
2015-10-15 02:04:58 -04:00
the [Tour of Heroes](../tutorial/) tutorial
2015-10-14 23:25:19 -04:00
such as <code>npm</code>, <code>gulp</code>, and <code>live-server</code>.
2015-10-16 04:06:56 -04:00
.l-main-section
2015-11-10 13:31:46 -05:00
:marked
2015-10-14 23:25:19 -04:00
## Create the test-runner HTML
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
Step away from the Jasmine 101 folder and turn to the root folder of the application that we downloaded in the previous chapter.
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
Locate the `src` folder that contains the application `index.html`
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
Create a new, sibling HTML file, ** `unit-tests.html` ** and copy over the same basic material from the `unit-tests.html` in the [Jasmine 101](./jasmine-testing-101.html) chapter.
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
```
<html>
2015-10-18 15:20:52 -04:00
<head>
<title>1st Jasmine Tests</title>
<link rel="stylesheet" href="../node_modules/jasmine-core/lib/jasmine-core/jasmine.css">
2015-10-09 13:33:12 -04:00
2015-10-18 15:20:52 -04:00
<script src="../node_modules/jasmine-core/lib/jasmine-core/jasmine.js"></script>
<script src="../node_modules/jasmine-core/lib/jasmine-core/jasmine-html.js"></script>
2016-02-01 18:25:20 -05:00
<script src="../node_modules/jasmine-core/lib/jasmine-core/boot.js"></script>
2015-10-14 23:25:19 -04:00
</head>
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
<body>
</body>
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
</html>
```
2015-10-09 13:33:12 -04:00
2016-01-19 04:15:26 -05:00
We're picking up right where we left off. All we've done is change the title.
2015-10-09 13:33:12 -04:00
2015-10-16 04:06:56 -04:00
.l-main-section
2015-11-10 13:31:46 -05:00
:marked
2015-10-14 23:25:19 -04:00
## Update `package.json` for testing
2015-10-09 13:33:12 -04:00
2016-01-19 04:15:26 -05:00
We'll assume that the application has `package.json` file that looks more or less like
the one we prescribed in the in the "Install npm packages locally" section of the
2015-10-14 23:25:19 -04:00
[QuickStart](../quickstart.html).
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
We must install the Jasmine package as well:
2015-10-09 13:33:12 -04:00
pre.prettyprint.lang-bash
2015-10-14 23:25:19 -04:00
code npm install jasmine-core --save-dev --save-exact
2015-10-09 13:33:12 -04:00
2015-10-10 06:29:34 -04:00
.alert.is-important Be sure to install <code>jasmine-core</code> , not <code>jasmine</code>!
2015-10-09 13:33:12 -04:00
2015-11-10 13:31:46 -05:00
:marked
2016-01-19 04:15:26 -05:00
Let's make one more change to the `package.json` script commands.
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
**Open the `package.json` ** and scroll to the `scripts` node. Look for the command named `test`. Change it to:
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
"test": "live-server --open=src/unit-tests.html"
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
That command will launch `live-server` and open a browser to the `unit-tests.html` page we just wrote.
2015-10-09 13:33:12 -04:00
2015-10-16 04:06:56 -04:00
.l-main-section
2015-11-10 13:31:46 -05:00
:marked
2015-10-14 23:25:19 -04:00
## First app tests
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
Believe it or not … we could start testing *some* of our app right away. For example, we can test the `Hero` class:
```
let nextId = 30;
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
export class Hero {
constructor(
public id?: number,
public name?: string,
public power?: string,
public alterEgo?: string
) {
this.id = id || nextId++;
}
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
clone() { return Hero.clone(this); }
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
static clone = (h:any) => new Hero(h.id, h.name, h.alterEgo, h.power);
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
static setNextId = (next:number) => nextId = next;
}
```
2015-10-09 13:33:12 -04:00
2016-01-19 04:15:26 -05:00
Let's add a couple of simple tests in the `<body>` element.
2015-10-09 13:33:12 -04:00
2016-01-19 04:15:26 -05:00
First, we'll load the JavaScript file that defines the `Hero` class.
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
```
<!-- load the application's Hero definition -->
<script src="app/hero.js"></script>
```
2015-10-09 13:33:12 -04:00
2016-01-19 04:15:26 -05:00
Next, we'll add an inline script element with the `Hero`tests themselves
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
```
<script>
// Demo only!
describe('Hero', function() {
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
it('has name given in the constructor', function() {
var hero = new Hero(1, 'Super Cat');
expect(hero.name).toEqual('Super Cat');
});
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
it('has the id given in the constructor', function() {
var hero = new Hero(1, 'Super Cat');
expect(hero.id).toEqual(1);
});
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
});
</script>
```
2015-10-09 13:33:12 -04:00
2016-01-19 04:15:26 -05:00
That's the basic Jasmine we learned back in "Jasmine 101".
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
Notice that we surrounded our tests with ** `describe('Hero')` **.
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
**By convention, our test always begin with a `describe` that identifies the application part under test.**
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
The description should be sufficient to identify the tested application part and its source file. Almost any convention will do as long as you and your team follow it consistently and are never confused.
2015-10-09 13:33:12 -04:00
2015-10-16 04:06:56 -04:00
.l-main-section
2015-11-10 13:31:46 -05:00
:marked
2015-10-14 23:25:19 -04:00
## Run the tests
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
Open one terminal window and run the watching compiler command: `npm run tsc`
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
Open another terminal window and run live-server: `npm test`
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
The browser should launch and display the two passing tests:
2015-10-09 13:33:12 -04:00
figure.image-display
2015-10-14 23:25:19 -04:00
img(src='/resources/images/devguide/first-app-tests/passed-2-specs-0-failures.png' style="width:400px;" alt="Two passing tests")
2015-10-09 13:33:12 -04:00
2015-10-16 04:06:56 -04:00
.l-main-section
2015-11-10 13:31:46 -05:00
:marked
2015-10-14 23:25:19 -04:00
## Critique
2015-10-09 13:33:12 -04:00
2016-01-19 04:15:26 -05:00
Is this `Hero` class even worth testing? It's essentially a property bag with almost no logic. Maybe we should have tested the cloning feature. Maybe we should have tested id generation. We didn't bother because there wasn't much to learn by doing that.
2015-10-09 13:33:12 -04:00
2016-01-19 04:15:26 -05:00
It's more important to take note of the `//Demo only` comment in the `unit-tests.html`.
2015-10-09 13:33:12 -04:00
2016-01-19 04:15:26 -05:00
** We'll never write real tests in the HTML this way**. It's nice that we can write *some* of our application tests directly in the HTML. But dumping all of our tests into HTML is not sustainable and even if we didn't mind that approach, we could only test a tiny fraction of our app this way.
2015-10-09 13:33:12 -04:00
2016-01-19 04:15:26 -05:00
We need to relocate these tests to a separate file. Let's do that next.
2015-10-09 13:33:12 -04:00
2015-10-16 04:06:56 -04:00
.l-main-section
2015-11-10 13:31:46 -05:00
:marked
2015-10-14 23:25:19 -04:00
## Where do tests go?
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
Some people like to keep their tests in a `tests` folder parallel to the application source folder.
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
We are not those people. We like our unit tests to be close to the source code that they test. We prefer this approach because
- The tests are easy to find
- We see at a glance if an application part lacks tests.
- Nearby tests can teach us about how the part works; they express the developers intention and reveal how the developer thinks the part should behave under a variety of circumstances.
- When we move the source (inevitable), we remember to move the test.
- When we rename the source file (inevitable), we remember to rename the test file.
2015-10-09 13:33:12 -04:00
2016-01-19 04:15:26 -05:00
We can't think of a downside. The server doesn't care where they are. They are easy to find and distinguish from application files when named conventionally.
2015-10-09 13:33:12 -04:00
2016-01-19 04:15:26 -05:00
You may put your tests elsewhere if you wish. We're putting ours inside the app, next to the source files that they test.
2015-10-09 13:33:12 -04:00
2015-10-16 04:06:56 -04:00
.l-main-section
2015-11-10 13:31:46 -05:00
:marked
2015-10-14 23:25:19 -04:00
## First spec file
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
**Create** a new file, ** `hero.spec.ts` ** in `src/app` next to `hero.ts`.
2015-10-09 13:33:12 -04:00
2016-01-19 04:15:26 -05:00
Notice the ".spec" suffix in the test file's filename, appended to the name of the file holding the application part we're testing.
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
.alert.is-important All of our unit test files follow this .spec naming pattern.
2015-10-09 13:33:12 -04:00
2015-11-10 13:31:46 -05:00
:marked
2015-10-14 23:25:19 -04:00
Move the tests we just wrote in`unit-tests.html` to `hero.spec.ts` and convert them from JavaScript into TypeScript:
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
```
import {Hero} from './hero';
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
describe('Hero', () => {
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
it('has name given in the constructor', () => {
let hero = new Hero(1, 'Super Cat');
expect(hero.name).toEqual('Super Cat');
});
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
it('has id given in the constructor', () => {
let hero = new Hero(1, 'Super Cat');
expect(hero.id).toEqual(1);
});
})
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
```
2015-10-09 13:33:12 -04:00
2016-01-19 04:15:26 -05:00
### Import the part we're testing
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
During our conversion to TypeScript, we added an `import {Hero} from './hero' ` statement.
2015-10-09 13:33:12 -04:00
2016-01-19 04:15:26 -05:00
If we forgot this import, a TypeScript-aware editor would warn us, with a squiggly red underline, that it can't find the definition of the `Hero` class.
2015-10-09 13:33:12 -04:00
2016-01-19 04:15:26 -05:00
TypeScript doesn't know what a `Hero` is. It doesn't know about the script tag back in the `unit-tests.html` that loads the `hero.js` file.
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
### Update unit-tests.html
2015-10-09 13:33:12 -04:00
2016-02-29 00:18:31 -05:00
Next we update the `unit-tests.html` with a reference to our new `hero.spec.ts` file. Delete the inline test code. The revised pertinent HTML looks like this:
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
<script src="app/hero.js"></script>
<script src="app/hero.spec.js"></script>
2015-10-09 13:33:12 -04:00
2015-10-16 04:06:56 -04:00
### Run and Fail
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
Look over at the browser (live-server will have reloaded it). The browser displays
2015-10-09 13:33:12 -04:00
figure.image-display
2015-10-14 23:25:19 -04:00
img(src='/resources/images/devguide/first-app-tests/Jasmine-not-running-tests.png' style="width:400px;" alt="Jasmine not running any tests")
2015-10-09 13:33:12 -04:00
2015-11-10 13:31:46 -05:00
:marked
2016-01-19 04:15:26 -05:00
That's Jasmine saying "**things are _so_ bad that _I'm not running any tests_.**"
2015-10-09 13:33:12 -04:00
2016-01-19 04:15:26 -05:00
Open the browser's Developer Tools (F12, Ctrl-Shift-i). There's an error:
2015-11-10 13:31:46 -05:00
2015-10-16 04:06:56 -04:00
code-example(format="" language="html").
2015-10-14 23:25:19 -04:00
Uncaught ReferenceError: exports is not defined
2015-10-09 13:33:12 -04:00
2015-10-16 04:06:56 -04:00
.l-main-section
2015-11-10 13:31:46 -05:00
:marked
2016-02-11 18:08:06 -05:00
## Load tests with SystemJS
2015-10-09 13:33:12 -04:00
2015-11-10 13:31:46 -05:00
The immediate cause of the error is the `export` statement in `hero.ts`.
That error was there all along.
2016-01-19 04:15:26 -05:00
It wasn't a problem until we tried to `import` the `Hero` class in our tests.
2015-10-09 13:33:12 -04:00
2015-11-10 13:31:46 -05:00
Our test environment lacks support for module loading.
2016-01-19 04:15:26 -05:00
Apparently we can't simply load our application and test scripts like we do with 3rd party JavaScript libraries.
2015-10-09 13:33:12 -04:00
2015-11-10 13:31:46 -05:00
We are committed to module loading in our application.
2015-10-14 23:25:19 -04:00
Our app will call `import`. Our tests must do so too.
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
We add module loading support in four steps:
2015-10-09 13:33:12 -04:00
2016-02-11 18:08:06 -05:00
1. add the *SystemJS* module management library
1. configure *SystemJS* to look for JavaScript files by default
2015-10-14 23:25:19 -04:00
1. import our test files
1. tell Jasmine to run the imported tests
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
These steps are all clearly visible, in exactly that order, in the following lines that
replace the `<body>` contents in `unit-tests.html`:
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
```
<body>
<!-- #1. add the system.js library -->
<script src="../node_modules/systemjs/dist/system.src.js"></script>
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
<script>
2016-02-11 18:08:06 -05:00
// #2. Configure SystemJS to use the .js extension
2015-10-14 23:25:19 -04:00
// for imports from the app folder
System.config({
packages: {
'app': {defaultExtension: 'js'}
}
});
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
// #3. Import the spec file explicitly
System.import('app/hero.spec')
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
// #4. wait for all imports to load ...
// then re-execute `window.onload` which
// triggers the Jasmine test-runner start
// or explain what went wrong
.then(window.onload)
.catch(console.error.bind(console));
</script>
</body>
```
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
Look in the browser window. Our tests pass once again.
2015-10-09 13:33:12 -04:00
figure.image-display
2015-10-14 23:25:19 -04:00
img(src='/resources/images/devguide/first-app-tests/test-passed-once-again.png' style="width:400px;" alt="Tests passed once again")
2015-10-09 13:33:12 -04:00
2015-10-16 04:06:56 -04:00
.l-main-section
2015-11-10 13:31:46 -05:00
:marked
2015-10-14 23:25:19 -04:00
## Observations
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
### System.config
2015-11-10 13:31:46 -05:00
System.js demands that we specify a default extension for the filenames that correspond to whatever it is asked to import.
2016-01-19 04:15:26 -05:00
Without that default, it would translate an import statement such as `import {Hero} from './hero'` to a request for the file named `hero`.
Not `hero.js`. Just plain `hero`. Our server error with "404 - not found" because it doesn't have a file of that name.
2015-10-09 13:33:12 -04:00
2016-02-11 18:08:06 -05:00
Once configured with a default extension of 'js', SystemJS requests `hero.js` which *does* exist and is promptly returned by our server.
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
### Asynchronous System.import
2016-01-19 04:15:26 -05:00
The call to `System.import` shouldn't surprise us but it's asynchronous nature might.
2015-11-10 13:31:46 -05:00
If we ponder this for a moment, we realize that it must be asynchronous because
2015-10-14 23:25:19 -04:00
System.js may have to fetch the corresponding JavaScript file from the server.
2015-11-10 13:31:46 -05:00
Accordingly, `System.import` returns a promise and we must wait for that promise to resolve.
2015-10-14 23:25:19 -04:00
Only then can Jasmine start evaluating the imported tests.
2015-10-09 13:33:12 -04:00
2015-10-14 23:25:19 -04:00
### window.onload
2016-01-19 04:15:26 -05:00
Jasmine doesn't have a `start` method. It wires its own start to the browser window's `load` event.
That makes sense if we're loading our tests with script tags.
2015-10-14 23:25:19 -04:00
The browser raises the `load` event when it finishes loading all scripts.
2015-10-09 13:33:12 -04:00
2016-01-19 04:15:26 -05:00
But we're not loading test scripts inline anymore.
2016-02-11 18:08:06 -05:00
We're using the SystemJS module loader and it won't be done until long after the browser raised the `load` event.
2015-10-14 23:25:19 -04:00
Meanwhile, Jasmine started and ran to completion … with no tests to evaluate … before the import completed.
2015-10-09 13:33:12 -04:00
2015-11-10 13:31:46 -05:00
So we must wait until the import completes and only then call the window `onLoad` handler.
2015-10-14 23:25:19 -04:00
Jasmine re-starts, this time with our imported test queued up.
2015-10-09 13:33:12 -04:00
2015-10-16 04:06:56 -04:00
.l-main-section
2015-11-10 13:31:46 -05:00
:marked
2016-01-19 04:15:26 -05:00
## What's Next?
2015-11-10 13:31:46 -05:00
We are able to test a part of our application with simple Jasmine tests.
2015-10-14 23:25:19 -04:00
The part was a stand-alone class that made no mention or use of Angular.
2015-10-09 13:33:12 -04:00
2016-01-19 04:15:26 -05:00
That's not rare but it's not typical either. Most of our application parts make some use of the Angular framework.
2015-10-09 13:33:12 -04:00
2016-02-01 18:25:20 -05:00
In the [next chapter](testing-an-angular-pipe.html), we'll test a class that does rely on Angular.