Commit 1ba19a73 authored by Ward Bell's avatar Ward Bell

chore: add sample unit and e2e tests and explain them.

parent d31fc47b
typings/ typings/
node_modules/ node_modules/
*.js *.js
*.js.map *.js.map
\ No newline at end of file !**/*e2e-spec.js
_test-output
...@@ -13,7 +13,8 @@ ...@@ -13,7 +13,8 @@
* add run packages * add run packages
* a2-in-memory-web-api * a2-in-memory-web-api
* add testing dev-dependency packages * add testing dev-dependency packages
* canonical-path: 0.0.2,
* http-server: ^0.9.0, * http-server: ^0.9.0,
* jasmine-core: ~2.4.1, * jasmine-core: ~2.4.1,
* karma: ^0.13.22, * karma: ^0.13.22,
......
...@@ -3,6 +3,14 @@ ...@@ -3,6 +3,14 @@
This repository holds the TypeScript source code of the [angular.io quickstart](https://angular.io/docs/ts/latest/quickstart.html), This repository holds the TypeScript source code of the [angular.io quickstart](https://angular.io/docs/ts/latest/quickstart.html),
the foundation for most of the documentation samples and potentially a good starting point for your application. the foundation for most of the documentation samples and potentially a good starting point for your application.
It's been extended with testing support so you can start writing tests immediately.
**This is not the perfect arrangement for your application. It is not designed for production.
It exists primarily to get you started quickly with learning and prototyping in Angular 2**
We are unlikely to accept suggestions about how to grow this QuickStart into something it is not.
Please keep that in mind before posting issues and PRs.
## Create a new project based on the QuickStart ## Create a new project based on the QuickStart
Clone this repo into new project folder (e.g., `my-proj`). Clone this repo into new project folder (e.g., `my-proj`).
...@@ -35,17 +43,28 @@ Grab its address (e.g. *`https://github.com/<my-org>/my-proj.git`*) and push the ...@@ -35,17 +43,28 @@ Grab its address (e.g. *`https://github.com/<my-org>/my-proj.git`*) and push the
git remote add origin <repo-address> git remote add origin <repo-address>
git push -u origin master git push -u origin master
``` ```
### Start development ## Install npm packages
Install the npm packages described in the `package.json` and verify that it works: Install the npm packages described in the `package.json` and verify that it works:
**Attention Windows Developers: You must run all of these commands in administrator mode**
```bash ```bash
npm install npm install
npm start npm start
``` ```
The `npm start` command first compiles the application,
then simultaneously re-compiles and runs the `lite-server`.
Both the compiler and the server watch for file changes.
Shut it down manually with Ctrl-C.
You're ready to write your application. You're ready to write your application.
Remember the npm scripts in `package.json`: ### npm scripts
We've captured many of the most useful commands in npm scripts defined in the `package.json`:
* `npm start` - runs the compiler and a server at the same time, both in "watch mode". * `npm start` - runs the compiler and a server at the same time, both in "watch mode".
* `npm run tsc` - runs the TypeScript compiler once. * `npm run tsc` - runs the TypeScript compiler once.
...@@ -56,3 +75,52 @@ Remember the npm scripts in `package.json`: ...@@ -56,3 +75,52 @@ Remember the npm scripts in `package.json`:
with excellent support for Angular apps that use routing. with excellent support for Angular apps that use routing.
* `npm run typings` - runs the typings tool. * `npm run typings` - runs the typings tool.
* `npm run postinstall` - called by *npm* automatically *after* it successfully completes package installation. This script installs the TypeScript definition files this app requires. * `npm run postinstall` - called by *npm* automatically *after* it successfully completes package installation. This script installs the TypeScript definition files this app requires.
Here are the test related scripts:
* `npm test` - compiles, runs and watches the karma unit tests
* `npm webdriver:update` - ONE TIME update for protractor end-to-end (e2e) tests
* `npm run e2e` - run protractor e2e tests, written in JavaScript (*e2e-spec.js)
## Testing
The QuickStart documentation doesn't discuss testing.
This repo adds both karma/jasmine unit test and protractor end-to-end testing support.
These tools are configured for specific conventions described below.
*It is unwise and rarely possible to run the application, the unit tests, and the e2e tests at the same time.
We recommend that you shut down one before starting another.*
### Unit Tests
TypeScript unit-tests are usually in the `app` folder. Their filenames must end in `.spec`.
Look for the example `app/app.component.spec.ts`.
Add more `.spec.ts` files as you wish; we configured karma to find them.
Run it with `npm tests`.
That command first compiles the application, then simultaneously re-compiles and runs the karma test-runner.
Both the compiler and the karma watch for (different) file changes.
Shut it down manually with Ctrl-C.
### End-to-end (E2E) Tests
**BEFORE RUNNING THE FIRST TEST** you must update the Selenium webdriver. Run `npm webdriver:update`.
E2E tests are usually at the project root, above the `app` folder.
Their filenames must end in `e2e-spec.js`.
E2E tests must be written in JavaScript (the author has not figured out how to write them in TS yet).
Look for the example `e2e-spec.ts` in the root folder.
Add more `e2e-spec.js` files as you wish (although one usually suffices for small projects);
we configured protractor to find them.
Thereafter, run them with `npm run e2e`.
That command first compiles, then simultaneously starts the Http-Server at `localhost:8080`
and launches protractor. The pass/fail test results appear at the bottom of the terminal window.
Shut it down manually with Ctrl-C.
/* tslint:disable:no-unused-variable */
import { AppComponent } from './app.component';
import {
it,
iit,
xit,
describe,
ddescribe,
xdescribe,
expect,
fakeAsync,
tick,
beforeEach,
inject,
injectAsync,
withProviders,
beforeEachProviders,
TestComponentBuilder
} from 'angular2/testing';
import { provide } from 'angular2/core';
import { ViewMetadata } from 'angular2/core';
import { PromiseWrapper } from 'angular2/src/facade/promise';
/////////// Module Preparation ///////////////////////
interface Done {
(): void;
fail: (err: any) => void;
}
//////// SPECS /////////////
/// Delete thesVerify can use Angular testing's DOM abstraction to access DOM
describe('Smoke test', () => {
it('should run a passing test', () => {
expect(true).toEqual(true, 'should pass');
});
});
describe('AppComponent', function() {
it('should instantiate component',
injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => {
return tcb.createAsync(AppComponent).then(fixture => {
expect(fixture.componentInstance instanceof AppComponent).toBe(true, 'should create AppComponent');
});
}));
it('should have expected <h1> text',
injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => {
return tcb.createAsync(AppComponent).then(fixture => {
// fixture.detectChanges(); // need for a binding; we don't have one
let h1 = fixture.debugElement.query(el => el.name === 'h1').nativeElement;
expect(h1.innerText).toMatch(/angular 2 app/i, '<h1> should say something about "Angular 2 App"');
});
}));
});
describe('QuickStart E2E Tests', function () {
var expectedMsg = 'My First Angular 2 App';
beforeEach(function () {
browser.get('');
});
it('should display: ' + expectedMsg, function () {
expect(element(by.css('h1')).getText()).toEqual(expectedMsg);
});
});
...@@ -6,19 +6,20 @@ ...@@ -6,19 +6,20 @@
"build-and-test": "npm run tsc && npm run test", "build-and-test": "npm run tsc && npm run test",
"docker-build": "docker build -t ng2-quickstart .", "docker-build": "docker build -t ng2-quickstart .",
"docker": "npm run docker-build && docker run -it --rm -p 3000:3000 -p 3001:3001 ng2-quickstart", "docker": "npm run docker-build && docker run -it --rm -p 3000:3000 -p 3001:3001 ng2-quickstart",
"e2e": "tsc && http-server && protractor protractor.config.js", "e2e": "tsc && concurrently \"http-server\" \"protractor protractor.config.js\"",
"tsc": "tsc", "tsc": "tsc",
"tsc:w": "tsc -w", "tsc:w": "tsc -w",
"lite": "lite-server", "lite": "lite-server",
"test": "tsc && concurrently \"tsc -w\" \"karma start karma.conf.js\"", "test": "tsc && concurrently \"tsc -w\" \"karma start karma.conf.js\"",
"typings": "typings", "typings": "typings",
"webdriver:update": "webdriver-manager update",
"postinstall": "typings install" "postinstall": "typings install"
}, },
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"angular2": "2.0.0-beta.15", "angular2": "2.0.0-beta.15",
"a2-in-memory-web-api": "^0.1.14", "a2-in-memory-web-api": "^0.1.14",
"systemjs": "0.19.25", "systemjs": "0.19.25",
"es6-shim": "^0.35.0", "es6-shim": "^0.35.0",
"reflect-metadata": "0.1.2", "reflect-metadata": "0.1.2",
...@@ -26,18 +27,19 @@ ...@@ -26,18 +27,19 @@
"zone.js": "0.6.10" "zone.js": "0.6.10"
}, },
"devDependencies": { "devDependencies": {
"canonical-path": "0.0.2",
"concurrently": "^2.0.0", "concurrently": "^2.0.0",
"http-server": "^0.9.0", "http-server": "^0.9.0",
"jasmine-core": "~2.4.1", "jasmine-core": "~2.4.1",
"karma": "^0.13.22", "karma": "^0.13.22",
"karma-chrome-launcher": "^0.2.3", "karma-chrome-launcher": "^0.2.3",
"karma-cli": "^0.1.2", "karma-cli": "^0.1.2",
"karma-htmlfile-reporter": "^0.2.2", "karma-htmlfile-reporter": "^0.2.2",
"karma-jasmine": "^0.3.8", "karma-jasmine": "^0.3.8",
"lite-server": "^2.2.0", "lite-server": "^2.2.0",
"protractor": "^3.2.2", "protractor": "^3.2.2",
"rimraf": "^2.5.2", "rimraf": "^2.5.2",
"typescript": "^1.8.10", "typescript": "^1.8.10",
"typings":"^0.7.12" "typings":"^0.7.12"
} }
} }
\ No newline at end of file
// TO RUN THE TESTS // FIRST TIME ONLY- run:
//
// The first time, run:
// ./node_modules/.bin/webdriver-manager update // ./node_modules/.bin/webdriver-manager update
// Make sure the test server is running. Then do. //
// ./node_modules/.bin/protractor protractor.config.js // Try: `npm run webdriver:update`
//
// AND THEN EVERYTIME ...
// 1. Compile with `tsc`
// 2. Make sure the test server (e.g., http-server: localhost:8080) is running.
// 3. ./node_modules/.bin/protractor protractor.config.js
//
// To do all steps, try: `npm run e2e`
var fs = require('fs'); var fs = require('fs');
var path = require('canonical-path'); var path = require('canonical-path');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment