├── .gitignore
├── .npmignore
├── CHANGELOG.md
├── README.md
├── circle.yml
├── core.ts
├── dtsm.json
├── karma.conf.js
├── mocha.ts
├── package.json
├── src
├── core
│ ├── async.ts
│ ├── test_component_builder.ts
│ └── test_injector.ts
└── mocha
│ └── testing.ts
├── test
├── app
│ └── app.component.spec.ts
├── common
│ ├── models
│ │ └── test.model.spec.ts
│ └── services
│ │ └── test.service.spec.ts
├── index.spec.ts
└── tsconfig.json
└── tsconfig.json
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea/
2 | node_modules/
3 | typings/
4 |
5 | **/*.js
6 | **/*.d.ts
7 |
8 | !karma.conf.js
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | .idea/
2 | node_modules/
3 | test/
4 | typings/
5 |
6 | **/*.ts
7 | !**/*.d.ts
8 |
9 | dtsm.json
10 | karma.conf.js
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 |
2 | # [1.4.0](https://github.com/laco0416/angular2-testing-lite/compare/v1.3.4...v1.4.0) (2016-05-03)
3 |
4 |
5 | ### Features
6 |
7 | * **core:** update for rc.0([2cbc3b8](https://github.com/laco0416/angular2-testing-lite/commit/2cbc3b8))
8 |
9 |
10 |
11 |
12 | ## [1.3.4](https://github.com/laco0416/angular2-testing-lite/compare/v1.3.3...v1.3.4) (2016-04-29)
13 |
14 |
15 | ### Features
16 |
17 | * **core:** provide zone-patched async([585dd2e](https://github.com/laco0416/angular2-testing-lite/commit/585dd2e))
18 |
19 |
20 |
21 |
22 | ## [1.3.3](https://github.com/laco0416/angular2-testing-lite/compare/v1.3.2...v1.3.3) (2016-04-29)
23 |
24 |
25 | ### Features
26 |
27 | * **core:** override default ZoneSpec([935574c](https://github.com/laco0416/angular2-testing-lite/commit/935574c))
28 |
29 |
30 |
31 |
32 | ## [1.3.2](https://github.com/laco0416/angular2-testing-lite/compare/v1.3.1...v1.3.2) (2016-04-29)
33 |
34 |
35 | ### Features
36 |
37 | * **mocha:** Fix for beta.17 (drop FunctionWithParamTokens)([831c5e1](https://github.com/laco0416/angular2-testing-lite/commit/831c5e1))
38 |
39 |
40 |
41 |
42 | ## [1.3.1](https://github.com/laco0416/angular2-testing-lite/compare/v1.3.0...v1.3.1) (2016-04-29)
43 |
44 |
45 | ### Features
46 |
47 | * **mocha:** Use Zone as well as origin for Jasmine([5b822d9](https://github.com/laco0416/angular2-testing-lite/commit/5b822d9))
48 | * **package:** rename package to angular2-testing-lite([fcfe2bc](https://github.com/laco0416/angular2-testing-lite/commit/fcfe2bc))
49 |
50 |
51 | ### BREAKING CHANGES
52 |
53 | * package: change import path.
54 |
55 |
56 |
57 |
58 | # [1.3.0](https://github.com/laco0416/angular2-testing-lite/compare/v1.2.1...v1.3.0) (2016-04-28)
59 |
60 |
61 | ### Features
62 |
63 | * **core:** use parts of angular2/testing directly([6e7880a](https://github.com/laco0416/angular2-testing-lite/commit/6e7880a))
64 |
65 |
66 |
67 |
68 | ## [1.2.1](https://github.com/laco0416/angular2-testing-lite/compare/v1.2.0...v1.2.1) (2016-04-28)
69 |
70 |
71 | ### Features
72 |
73 | * **core:** support bootstrapProviders([7daea8b](https://github.com/laco0416/angular2-testing-lite/commit/7daea8b))
74 |
75 |
76 |
77 |
78 | # [1.2.0](https://github.com/laco0416/angular2-testing-lite/compare/v1.1.0...v1.2.0) (2016-04-28)
79 |
80 |
81 | ### Features
82 |
83 | * **core:** support setBaseTestProviders([b5b3313](https://github.com/laco0416/angular2-testing-lite/commit/b5b3313))
84 |
85 |
86 |
87 |
88 | # [1.1.0](https://github.com/laco0416/angular2-testing-lite/compare/v1.0.0...v1.1.0) (2016-04-27)
89 |
90 |
91 |
92 |
93 | # 1.0.0 (2016-03-25)
94 |
95 |
96 |
97 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # angular2-testing-lite
2 | [](https://circleci.com/gh/laco0416/angular2-testing-lite/tree/master)
3 | [](https://badge.fury.io/js/angular2-testing-lite)
4 |
5 | # OBSOLUTED
6 | Since RC.4, Angular 2 no longer depends on Jasmine API.
7 | **Please use official testing APIs. :)**
8 |
9 | ----
10 |
11 | Jasmine-free Angular 2 Testing Library
12 |
13 | ## Features
14 |
15 | - **Jasmine-free**
16 | - Same core API as `angular2/testing`
17 | - Less API: supports only `TestInjector` and `TestComponentBuilder`
18 | - Opt-in utilities for [Mocha](https://github.com/mochajs/mocha)
19 |
20 | ## Install
21 |
22 | ```
23 | $ npm install --save-dev angular2-testing-lite
24 | ```
25 |
26 | ## Usage ([Mocha](https://github.com/mochajs/mocha) & [power-assert](https:://github.com/power-assert-js/power-assert) example)
27 |
28 | ### Setup
29 |
30 | At an entry point of tests, you should set up a testing environment.
31 |
32 | ```ts
33 | import {
34 | setBaseTestProviders,
35 | resetBaseTestProviders,
36 | } from "angular2-testing-lite/core";
37 |
38 | import {
39 | BROWSER_APP_DYNAMIC_PROVIDERS
40 | } from "@angular/platform-browser-dynamic";
41 |
42 | import {
43 | TEST_BROWSER_STATIC_PLATFORM_PROVIDERS,
44 | ADDITIONAL_TEST_BROWSER_PROVIDERS,
45 | } from "@angular/platform-browser/testing/browser_static";
46 |
47 | resetBaseTestProviders();
48 | setBaseTestProviders(TEST_BROWSER_STATIC_PLATFORM_PROVIDERS, [BROWSER_APP_DYNAMIC_PROVIDERS, ADDITIONAL_TEST_BROWSER_PROVIDERS]);
49 | ```
50 |
51 | See [/test/index.spec.ts](https://github.com/laco0416/angular2-testing-lite/blob/master/test/index.spec.ts)
52 |
53 | ### Model
54 |
55 | Simple mocha test
56 |
57 | ```ts
58 | import assert = require("power-assert");
59 |
60 | describe("TestModel", () => {
61 |
62 | it("can instantiate", () => {
63 | let model = new TestModel("AAA");
64 | assert(!!model);
65 | });
66 |
67 | describe("toString()", () => {
68 | it("should return string", () => {
69 | const model = new TestModel("AAA");
70 | const str = model.toString();
71 | assert(typeof str === "string");
72 | });
73 | });
74 | });
75 | ```
76 |
77 | ### Service
78 | Inject `Http`
79 |
80 | ```ts
81 | import assert = require("power-assert");
82 | import {async, inject} from "angular2-testing-lite/core";
83 | import {it, describe, xdescribe, beforeEach, beforeEachProviders} from "angular2-testing-lite/mocha";
84 |
85 | describe("TestService", () => {
86 |
87 | beforeEachProviders(() => [
88 | BaseRequestOptions,
89 | MockBackend,
90 | provide(Http, {
91 | useFactory: (backend: MockBackend, options: BaseRequestOptions) => {
92 | return new Http(backend, options);
93 | }, deps: [MockBackend, BaseRequestOptions]
94 | }),
95 | TestService
96 | ]);
97 |
98 | it("can instantiate", inject([TestService], (service: TestService) => {
99 | assert(!!service);
100 | }));
101 | });
102 | ```
103 |
104 | ### Component
105 | Ported `TestComponentBuilder`
106 |
107 | ```ts
108 | import assert = require("power-assert");
109 | import {inject, async, TestComponentBuilder} from "angular2-testing-lite/core";
110 | import {describe, it, xit, beforeEachProviders, beforeEach} from "angular2-testing-lite/mocha";
111 | import {HTTP_PROVIDERS} from "@angular/http";
112 |
113 | describe("TestAppComponent", () => {
114 |
115 | beforeEachProviders(() => [
116 | HTTP_PROVIDERS,
117 | TestService,
118 | ]);
119 |
120 | it("can create", async(inject([TestComponentBuilder],
121 | (tcb: TestComponentBuilder) => {
122 | return tcb.createAsync(TestAppComponent)
123 | .then(fixture => {
124 | assert(!!fixture);
125 | });
126 | })
127 | ));
128 | });
129 | ```
130 |
131 |
132 | ### License
133 | MIT
134 |
--------------------------------------------------------------------------------
/circle.yml:
--------------------------------------------------------------------------------
1 | machine:
2 | node:
3 | version: 6
4 |
5 | ## Customize dependencies
6 | dependencies:
7 | pre:
8 | - npm install -g dtsm
9 | override:
10 | - dtsm --insight false install
11 | - npm install
12 |
--------------------------------------------------------------------------------
/core.ts:
--------------------------------------------------------------------------------
1 | export * from "./src/core/test_component_builder";
2 | export * from "./src/core/test_injector";
--------------------------------------------------------------------------------
/dtsm.json:
--------------------------------------------------------------------------------
1 | {
2 | "repos": [
3 | {
4 | "url": "https://github.com/DefinitelyTyped/DefinitelyTyped.git",
5 | "ref": "master"
6 | }
7 | ],
8 | "path": "typings",
9 | "bundle": "typings/bundle.d.ts",
10 | "link": {
11 | "npm": {
12 | "include": true
13 | }
14 | },
15 | "dependencies": {
16 | "mocha/mocha.d.ts": {
17 | "ref": "a44529fcb1e1cdc0355e71c42a6048de99b57c34"
18 | },
19 | "power-assert/power-assert.d.ts": {
20 | "ref": "a44529fcb1e1cdc0355e71c42a6048de99b57c34"
21 | }
22 | }
23 | }
--------------------------------------------------------------------------------
/karma.conf.js:
--------------------------------------------------------------------------------
1 | // Karma configuration
2 | // Generated on Fri Mar 25 2016 00:06:13 GMT+0900 (JST)
3 |
4 | module.exports = function (config) {
5 | config.set({
6 |
7 | // base path that will be used to resolve all patterns (eg. files, exclude)
8 | basePath: '',
9 |
10 |
11 | // frameworks to use
12 | // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
13 | frameworks: ['mocha'],
14 |
15 |
16 | // list of files / patterns to load in the browser
17 | files: [
18 | 'test/bundle.spec.espowered.js'
19 | ],
20 |
21 |
22 | // list of files to exclude
23 | exclude: [],
24 |
25 |
26 | // preprocess matching files before serving them to the browser
27 | // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
28 | preprocessors: {},
29 |
30 |
31 | // test results reporter to use
32 | // possible values: 'dots', 'progress'
33 | // available reporters: https://npmjs.org/browse/keyword/karma-reporter
34 | reporters: ['progress'],
35 |
36 |
37 | // web server port
38 | port: 9876,
39 |
40 |
41 | // enable / disable colors in the output (reporters and logs)
42 | colors: true,
43 |
44 |
45 | // level of logging
46 | // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
47 | logLevel: config.LOG_INFO,
48 |
49 |
50 | // enable / disable watching file and executing tests whenever any file changes
51 | autoWatch: false,
52 |
53 |
54 | // start these browsers
55 | // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
56 | browsers: ['PhantomJS'],
57 |
58 |
59 | // Continuous Integration mode
60 | // if true, Karma captures browsers, runs the tests and exits
61 | singleRun: true,
62 |
63 | // Concurrency level
64 | // how many browser should be started simultaneous
65 | concurrency: Infinity
66 | })
67 | }
68 |
--------------------------------------------------------------------------------
/mocha.ts:
--------------------------------------------------------------------------------
1 | export * from "./src/mocha/testing";
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "angular2-testing-lite",
3 | "version": "1.4.0",
4 | "description": "Jasmine-free Angular 2 Testing Library",
5 | "scripts": {
6 | "prepublish": "npm run build:lib",
7 | "tsconfig:lib": "tsconfig -u tsconfig.json",
8 | "build:lib": "tsc -p .",
9 | "prebuild:lib": "npm run tsconfig:lib",
10 | "test": "karma start",
11 | "pretest": "npm run build:test && npm run espower",
12 | "tsconfig:test": "tsconfig -u test/tsconfig.json",
13 | "build:test": "tsc -p test && browserify -o test/bundle.spec.js test/index.spec.js",
14 | "prebuild:test": "npm run tsconfig:test",
15 | "espower": "espower test/bundle.spec.js > test/bundle.spec.espowered.js",
16 | "postversion": "npm run changelog",
17 | "changelog": "node scripts/changelog.js",
18 | "postchangelog": "git add -u && git commit -m 'docs(changelog): Update CHANGELOG.md'"
19 | },
20 | "repository": {
21 | "url": "https://github.com/laco0416/angular2-testing-lite"
22 | },
23 | "keywords": [
24 | "angular2",
25 | "testing"
26 | ],
27 | "author": "Suguru Inatomi ",
28 | "license": "MIT",
29 | "devDependencies": {
30 | "@angular/common": "^2.0.0-rc.0",
31 | "@angular/compiler": "^2.0.0-rc.0",
32 | "@angular/core": "^2.0.0-rc.0",
33 | "@angular/http": "^2.0.0-rc.0",
34 | "@angular/platform-browser": "^2.0.0-rc.0",
35 | "@angular/platform-browser-dynamic": "^2.0.0-rc.0",
36 | "browserify": "^13.0.0",
37 | "conventional-changelog": "^1.1.0",
38 | "core-js": "^2.2.1",
39 | "espower-cli": "^1.0.0",
40 | "karma": "^0.13.22",
41 | "karma-mocha": "^0.2.2",
42 | "karma-phantomjs-launcher": "^1.0.0",
43 | "mocha": "^2.4.5",
44 | "phantomjs-prebuilt": "^2.1.6",
45 | "power-assert": "^1.3.1",
46 | "reflect-metadata": "0.1.2",
47 | "rxjs": "5.0.0-beta.6",
48 | "tsconfig-cli": "^1.0.2",
49 | "typescript": "^1.9.0-dev.20160502",
50 | "zone.js": "^0.6.12"
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/core/async.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Wraps a test function in an asynchronous test zone. The test will automatically
3 | * complete when all asynchronous calls within this zone are done. Can be used
4 | * to wrap an {@link inject} call.
5 | *
6 | * Example:
7 | *
8 | * ```
9 | * it('...', async(inject([AClass], (object) => {
10 | * object.doSomething.then(() => {
11 | * expect(...);
12 | * })
13 | * });
14 | * ```
15 | */
16 | export function async(fn: Function): Function {
17 | return () => {
18 | return new Promise((finishCallback, failCallback) => {
19 | const AsyncTestZoneSpec = (Zone)['AsyncTestZoneSpec'];
20 | let testZoneSpec = new AsyncTestZoneSpec(finishCallback, failCallback, 'test');
21 | // Override default error handler
22 | testZoneSpec.onHandleError = (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, error: any) => {
23 | const result = parentZoneDelegate.handleError(targetZone, error);
24 | if (result) {
25 | // NOTE: origin
26 | // testZoneSpec._failCallback(error.message ? error.message : 'unknown error');
27 | testZoneSpec._failCallback(error);
28 | testZoneSpec._alreadyErrored = true;
29 | }
30 | return false;
31 | };
32 | const testZone = Zone.current.fork(testZoneSpec);
33 | // enable onHandleError
34 | return testZone.runGuarded(fn);
35 | });
36 | }
37 | }
--------------------------------------------------------------------------------
/src/core/test_component_builder.ts:
--------------------------------------------------------------------------------
1 | // NOTE: not public
2 | export * from "@angular/compiler/testing/test_component_builder";
3 |
--------------------------------------------------------------------------------
/src/core/test_injector.ts:
--------------------------------------------------------------------------------
1 | // NOTE: not public
2 | export {
3 | getTestInjector,
4 | inject,
5 | InjectSetupWrapper,
6 | TestInjector,
7 | setBaseTestProviders,
8 | resetBaseTestProviders,
9 | withProviders
10 | } from "@angular/core/testing/test_injector";
11 |
12 | // Zone patched async
13 | export {async} from "./async";
--------------------------------------------------------------------------------
/src/mocha/testing.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * testing utilities for Mocha
3 | * Original: https://github.com/angular/angular/blob/master/modules/angular2/src/testing/testing.ts
4 | */
5 |
6 | import {global, isPromise} from "@angular/core/src/facade/lang";
7 | import {getTestInjector, TestInjector} from "../core/test_injector";
8 |
9 | const _global = (typeof window === "undefined" ? global : window);
10 |
11 | /**
12 | * Run a function (with an optional asynchronous callback) after each test case.
13 | *
14 | * See http://jasmine.github.io/ for more details.
15 | *
16 | * ## Example:
17 | *
18 | * {@example testing/ts/testing.ts region='afterEach'}
19 | */
20 | export const afterEach: Function = _global.afterEach;
21 |
22 | /**
23 | * Group test cases together under a common description prefix.
24 | *
25 | * See http://jasmine.github.io/ for more details.
26 | *
27 | * ## Example:
28 | *
29 | * {@example testing/ts/testing.ts region='describeIt'}
30 | */
31 | export const describe: Function = _global.describe;
32 |
33 | /**
34 | * Like {@link describe}, but instructs the test runner to exclude
35 | * this group of test cases from execution. This is useful for
36 | * debugging, or for excluding broken tests until they can be fixed.
37 | */
38 | export const xdescribe: Function = _global.xdescribe;
39 |
40 | const mochaBeforeEach: (action: (done?: MochaDone) => void) => void = _global.beforeEach;
41 | const mochaIt: Mocha.ITestDefinition = _global.it;
42 | const mochaXIt: Mocha.ITestDefinition = _global.xit;
43 |
44 | const testInjector: TestInjector = getTestInjector();
45 |
46 | // Reset the test providers before each test
47 | mochaBeforeEach(() => {
48 | testInjector.reset();
49 | });
50 |
51 | /**
52 | * Allows overriding default providers defined in test_injector.js.
53 | *
54 | * The given function must return a list of DI providers.
55 | *
56 | * Example:
57 | *
58 | * beforeEachProviders(() => [
59 | * provide(Compiler, {useClass: MockCompiler}),
60 | * provide(SomeToken, {useValue: myValue}),
61 | * ]);
62 | */
63 | export function beforeEachProviders(fn: () => any[]): void {
64 | mochaBeforeEach(() => {
65 | const providers = fn();
66 | if (!providers) return;
67 | try {
68 | testInjector.addProviders(providers);
69 | } catch (e) {
70 | throw new Error('beforeEachProviders was called after the injector had ' +
71 | 'been used in a beforeEach or it block. This invalidates the ' +
72 | 'test injector');
73 | }
74 | });
75 | }
76 |
77 | function _wrapTestFn(fn: Function) {
78 | // Wraps a test or beforeEach function to handle synchronous and asynchronous execution.
79 | return (done: MochaDone) => {
80 | if (fn.length === 0) {
81 | let retVal = fn();
82 | if (isPromise(retVal)) {
83 | // Asynchronous test function - wait for completion.
84 | (>retVal).then(() => done(), done);
85 | } else {
86 | // Synchronous test function - complete immediately.
87 | done();
88 | }
89 | } else {
90 | // Asynchronous test function that takes "done" as parameter.
91 | fn(done);
92 | }
93 | };
94 | }
95 |
96 | function _it(mochaFn: Function, name: string, testFn: Function): void {
97 | mochaFn(name, _wrapTestFn(testFn));
98 | }
99 |
100 | /**
101 | * Wrapper around Mocha beforeEach function.
102 | *
103 | * beforeEach may be used with the `inject` function to fetch dependencies.
104 | */
105 | export function beforeEach(fn: Function): void {
106 | mochaBeforeEach(_wrapTestFn(fn));
107 | }
108 |
109 | /**
110 | * Define a single test case with the given test name and execution function.
111 | *
112 | * The test function can be either a synchronous function, the result of {@link async},
113 | * or an injected function created via {@link inject}.
114 | *
115 | * Wrapper around Mocha it function.
116 | */
117 | export function it(name: string, fn: Function): void {
118 | return _it(mochaIt, name, fn);
119 | }
120 |
121 | /**
122 | * Like {@link it}, but instructs the test runner to exclude this test
123 | * entirely. Useful for debugging or for excluding broken tests until
124 | * they can be fixed.
125 | *
126 | * Wrapper around Mocha xit function.
127 | */
128 | export function xit(name: string, fn: Function): void {
129 | return _it(mochaXIt, name, fn);
130 | }
131 |
--------------------------------------------------------------------------------
/test/app/app.component.spec.ts:
--------------------------------------------------------------------------------
1 | import {Component, provide} from "@angular/core";
2 | import {TestService} from "../common/services/test.service.spec";
3 | import {TestModel} from "../common/models/test.model.spec";
4 |
5 | @Component({
6 | selector: "test-app",
7 | template: `
8 | Test App
9 | {{ model?.toString() }}
10 | `,
11 | providers: [TestService]
12 | })
13 | export class TestAppComponent {
14 |
15 | model: TestModel;
16 |
17 | constructor(private testService: TestService) {
18 | }
19 |
20 | ngOnInit() {
21 | this.testService.get("AAA")
22 | .subscribe(resp => {
23 | this.model = resp;
24 | });
25 | }
26 | }
27 |
28 | /**
29 | * ===== testing world =====
30 | */
31 |
32 | import assert = require("power-assert");
33 | import {inject, async, TestComponentBuilder} from "../../core";
34 | import {describe, it, xit, beforeEach} from "../../mocha";
35 | import {By} from "@angular/platform-browser";
36 | import {Observable} from "rxjs/Observable";
37 | import "rxjs/add/observable/of";
38 |
39 | class MockTestService {
40 |
41 | get(id: string): Observable {
42 | return Observable.of(new TestModel(id));
43 | }
44 | }
45 |
46 | describe("TestAppComponent", () => {
47 | let builder: TestComponentBuilder;
48 |
49 | beforeEach(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
50 | builder = tcb.overrideProviders(TestAppComponent, [
51 | provide(TestService, {useClass: MockTestService})
52 | ]);
53 | }));
54 |
55 | it("can create", async(() => {
56 | builder.createAsync(TestAppComponent)
57 | .then(fixture => {
58 | assert(!!fixture);
59 | });
60 | })
61 | );
62 |
63 | it("should has text: 'Test App'", async(() => {
64 | builder.createAsync(TestAppComponent)
65 | .then(fixture => {
66 | let el = fixture.debugElement;
67 | assert(el.query(By.css("h1")).nativeElement.innerHTML === "Test App");
68 | });
69 | })
70 | );
71 |
72 | it("should apply ngOnInit on detectChanges()", async(() => {
73 | builder.createAsync(TestAppComponent).then(fixture => {
74 | let el = fixture.debugElement;
75 | assert(el.query(By.css("p")).nativeElement.innerHTML === "");
76 | fixture.detectChanges();
77 | assert(el.query(By.css("p")).nativeElement.innerHTML !== "");
78 | });
79 | })
80 | );
81 | });
--------------------------------------------------------------------------------
/test/common/models/test.model.spec.ts:
--------------------------------------------------------------------------------
1 | export class TestModel {
2 | createdAt: number;
3 |
4 | constructor(public text: string) {
5 | this.createdAt = new Date().getTime();
6 | }
7 |
8 | toString(): string {
9 | return `[${this.createdAt.toString()}] ${this.text}`;
10 | }
11 | }
12 |
13 | /**
14 | * ===== testing world =====
15 | */
16 |
17 | import assert = require("power-assert");
18 |
19 | describe("TestModel", () => {
20 |
21 | it("can instantiate", () => {
22 | let model = new TestModel("AAA");
23 | assert(!!model);
24 | });
25 |
26 | describe("toString()", () => {
27 | it("should return string", () => {
28 | const model = new TestModel("AAA");
29 | const str = model.toString();
30 | assert(typeof str === "string");
31 | });
32 | });
33 | });
--------------------------------------------------------------------------------
/test/common/services/test.service.spec.ts:
--------------------------------------------------------------------------------
1 | import {Injectable, provide} from "@angular/core";
2 | import {Http, BaseRequestOptions, Response, ResponseOptions} from "@angular/http";
3 | import {TestModel} from "../models/test.model.spec";
4 | import {Observable} from "rxjs/Observable";
5 |
6 | @Injectable()
7 | export class TestService {
8 |
9 | constructor(private http: Http) {
10 | }
11 |
12 | get(id: string): Observable {
13 | return this.http.get(`/${id}`)
14 | .map(resp => resp.json() as TestModel);
15 | }
16 | }
17 |
18 | /**
19 | * ===== testing world =====
20 | */
21 |
22 | import assert = require("power-assert");
23 | import {MockBackend, MockConnection} from "@angular/http/testing";
24 | import {async, inject} from "../../../core";
25 | import {it, describe, xdescribe, beforeEach, beforeEachProviders} from "../../../mocha";
26 |
27 | describe("TestService", () => {
28 |
29 | beforeEachProviders(() => [
30 | BaseRequestOptions,
31 | MockBackend,
32 | provide(Http, {
33 | useFactory: (backend: MockBackend, options: BaseRequestOptions) => {
34 | return new Http(backend, options);
35 | }, deps: [MockBackend, BaseRequestOptions]
36 | }),
37 | TestService
38 | ]);
39 |
40 | it("can instantiate", inject([TestService], (service: TestService) => {
41 | assert(!!service);
42 | }));
43 |
44 | describe("get(id)", () => {
45 |
46 | beforeEach(inject([MockBackend], (backend: MockBackend) => {
47 | backend.connections.subscribe((c: MockConnection) => {
48 | let resp = new TestModel("mocked!");
49 | c.mockRespond(
50 | new Response(new ResponseOptions({
51 | status: 200,
52 | body: resp
53 | }))
54 | );
55 | });
56 | }));
57 |
58 | it("should return mocked TestModel", async(inject([TestService],
59 | (service: TestService) => {
60 | service.get("test").toPromise()
61 | .then(resp => {
62 | assert(!!resp);
63 | assert(resp.text === "mocked!");
64 | });
65 | })
66 | ));
67 | });
68 | });
--------------------------------------------------------------------------------
/test/index.spec.ts:
--------------------------------------------------------------------------------
1 | "use strict";
2 |
3 | // es6 shim
4 | import "core-js/shim";
5 |
6 | // ng2 deps
7 | // import "reflect-metadata";
8 | import "rxjs/add/operator/map";
9 | import "rxjs/add/operator/toPromise";
10 | import "zone.js/dist/zone";
11 | import "zone.js/dist/long-stack-trace-zone";
12 | import "zone.js/dist/async-test";
13 |
14 | import "./app/app.component.spec";
15 |
16 | import "./common/services/test.service.spec";
17 | import "./common/models/test.model.spec"
18 |
19 | import {
20 | setBaseTestProviders,
21 | resetBaseTestProviders,
22 | } from "../core";
23 |
24 | import {
25 | BROWSER_APP_DYNAMIC_PROVIDERS
26 | } from "@angular/platform-browser-dynamic";
27 | import {
28 | TEST_BROWSER_STATIC_PLATFORM_PROVIDERS,
29 | ADDITIONAL_TEST_BROWSER_PROVIDERS,
30 | } from "@angular/platform-browser/testing/browser_static";
31 |
32 | resetBaseTestProviders();
33 | setBaseTestProviders(TEST_BROWSER_STATIC_PLATFORM_PROVIDERS, [BROWSER_APP_DYNAMIC_PROVIDERS, ADDITIONAL_TEST_BROWSER_PROVIDERS]);
34 |
--------------------------------------------------------------------------------
/test/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "module": "commonjs",
4 | "target": "es5",
5 | "noImplicitAny": true,
6 | "moduleResolution": "node",
7 | "experimentalDecorators": true,
8 | "emitDecoratorMetadata": true
9 | },
10 | "filesGlob": [
11 | "../core.ts",
12 | "!../core.d.ts",
13 | "../mocha.ts",
14 | "!../mocha.d.ts",
15 | "**/*.ts",
16 | "!**/*.d.ts",
17 | "../src/**/*.ts",
18 | "!../src/**/*.d.ts",
19 | "../node_modules/zone.js/dist/zone.js.d.ts",
20 | "../typings/mocha/mocha.d.ts",
21 | "../typings/power-assert/power-assert.d.ts",
22 | "../node_modules/typescript/lib/lib.es6.d.ts"
23 | ],
24 | "files": [
25 | "../core.ts",
26 | "../mocha.ts",
27 | "app/app.component.spec.ts",
28 | "common/models/test.model.spec.ts",
29 | "common/services/test.service.spec.ts",
30 | "index.spec.ts",
31 | "../src/core/async.ts",
32 | "../src/core/test_component_builder.ts",
33 | "../src/core/test_injector.ts",
34 | "../src/mocha/testing.ts",
35 | "../node_modules/zone.js/dist/zone.js.d.ts",
36 | "../typings/mocha/mocha.d.ts",
37 | "../typings/power-assert/power-assert.d.ts",
38 | "../node_modules/typescript/lib/lib.es6.d.ts"
39 | ]
40 | }
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "module": "commonjs",
4 | "target": "es5",
5 | "noImplicitAny": true,
6 | "declaration": true,
7 | "moduleResolution": "node",
8 | "experimentalDecorators": true,
9 | "emitDecoratorMetadata": true
10 | },
11 | "filesGlob": [
12 | "**/*.ts",
13 | "!**/*.d.ts",
14 | "!test/**/*.ts",
15 | "!node_modules/**/*.*",
16 | "typings/mocha/mocha.d.ts",
17 | "node_modules/zone.js/dist/zone.js.d.ts",
18 | "node_modules/typescript/lib/lib.es6.d.ts"
19 | ],
20 | "files": [
21 | "core.ts",
22 | "mocha.ts",
23 | "src/core/async.ts",
24 | "src/core/test_component_builder.ts",
25 | "src/core/test_injector.ts",
26 | "src/mocha/testing.ts",
27 | "typings/mocha/mocha.d.ts",
28 | "node_modules/zone.js/dist/zone.js.d.ts",
29 | "node_modules/typescript/lib/lib.es6.d.ts"
30 | ]
31 | }
--------------------------------------------------------------------------------