| 
									
										
										
										
											2016-06-23 09:47:54 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-08 15:33:17 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Public Test Library for unit testing Angular2 Applications. Uses the | 
					
						
							|  |  |  |  * Jasmine framework. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-06-20 17:54:12 -07:00
										 |  |  | import {isPromise, isString} from '../src/facade/lang'; | 
					
						
							| 
									
										
										
										
											2015-10-08 15:33:17 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-23 17:10:22 -07:00
										 |  |  | import {TestInjector, getTestInjector} from './test_injector'; | 
					
						
							| 
									
										
										
										
											2015-10-08 15:33:17 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-15 09:37:33 -07:00
										 |  |  | declare var global: any; | 
					
						
							| 
									
										
										
										
											2015-10-08 15:33:17 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 10:28:57 -08:00
										 |  |  | var _global = <any>(typeof window === 'undefined' ? global : window); | 
					
						
							| 
									
										
										
										
											2015-10-08 15:33:17 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | export var expect: Function = _global.expect; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-18 18:46:24 -08:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2015-11-30 08:28:54 -08:00
										 |  |  |  * Run a function (with an optional asynchronous callback) after each test case. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * See http://jasmine.github.io/ for more details.
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ## Example: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * {@example testing/ts/testing.ts region='afterEach'} | 
					
						
							| 
									
										
										
										
											2015-11-18 18:46:24 -08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-10-08 15:33:17 -07:00
										 |  |  | export var afterEach: Function = _global.afterEach; | 
					
						
							| 
									
										
										
										
											2015-11-18 18:46:24 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2015-11-30 08:28:54 -08:00
										 |  |  |  * Group test cases together under a common description prefix. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * See http://jasmine.github.io/ for more details.
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ## Example: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * {@example testing/ts/testing.ts region='describeIt'} | 
					
						
							| 
									
										
										
										
											2015-11-18 18:46:24 -08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-10-08 15:33:17 -07:00
										 |  |  | export var describe: Function = _global.describe; | 
					
						
							| 
									
										
										
										
											2015-11-18 18:46:24 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2015-11-30 08:28:54 -08:00
										 |  |  |  * See {@link fdescribe}. | 
					
						
							| 
									
										
										
										
											2015-11-18 18:46:24 -08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-10-08 15:33:17 -07:00
										 |  |  | export var ddescribe: Function = _global.fdescribe; | 
					
						
							| 
									
										
										
										
											2015-11-18 18:46:24 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2015-11-30 08:28:54 -08:00
										 |  |  |  * Like {@link describe}, but instructs the test runner to only run | 
					
						
							|  |  |  |  * the test cases in this group. This is useful for debugging. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * See http://jasmine.github.io/ for more details.
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ## Example: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * {@example testing/ts/testing.ts region='fdescribe'} | 
					
						
							| 
									
										
										
										
											2015-11-18 18:46:24 -08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-10-08 15:33:17 -07:00
										 |  |  | export var fdescribe: Function = _global.fdescribe; | 
					
						
							| 
									
										
										
										
											2015-11-18 18:46:24 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2015-11-30 08:28:54 -08:00
										 |  |  |  * Like {@link describe}, but instructs the test runner to exclude | 
					
						
							|  |  |  |  * this group of test cases from execution. This is useful for | 
					
						
							|  |  |  |  * debugging, or for excluding broken tests until they can be fixed. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * See http://jasmine.github.io/ for more details.
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ## Example: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * {@example testing/ts/testing.ts region='xdescribe'} | 
					
						
							| 
									
										
										
										
											2015-11-18 18:46:24 -08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-10-08 15:33:17 -07:00
										 |  |  | export var xdescribe: Function = _global.xdescribe; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var jsmBeforeEach = _global.beforeEach; | 
					
						
							|  |  |  | var jsmIt = _global.it; | 
					
						
							|  |  |  | var jsmIIt = _global.fit; | 
					
						
							|  |  |  | var jsmXIt = _global.xit; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-23 17:10:22 -07:00
										 |  |  | // TODO(juliemr): override the globals and make them throw with a useful message.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-08 19:03:21 -08:00
										 |  |  | var testInjector: TestInjector = getTestInjector(); | 
					
						
							| 
									
										
										
										
											2015-10-08 15:33:17 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Reset the test providers before each test.
 | 
					
						
							| 
									
										
										
										
											2015-12-08 19:03:21 -08:00
										 |  |  | jsmBeforeEach(() => { testInjector.reset(); }); | 
					
						
							| 
									
										
										
										
											2015-10-08 15:33:17 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Allows overriding default providers of the test injector, | 
					
						
							| 
									
										
										
										
											2015-11-18 18:46:24 -08:00
										 |  |  |  * which are defined in test_injector.js. | 
					
						
							| 
									
										
										
										
											2015-10-08 15:33:17 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * The given function must return a list of DI providers. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-11-30 08:28:54 -08:00
										 |  |  |  * ## Example: | 
					
						
							| 
									
										
										
										
											2015-10-08 15:33:17 -07:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-11-30 08:28:54 -08:00
										 |  |  |  * {@example testing/ts/testing.ts region='beforeEachProviders'} | 
					
						
							| 
									
										
										
										
											2015-10-08 15:33:17 -07:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-06-04 19:52:51 -07:00
										 |  |  | export function beforeEachProviders(fn: () => Array<any>): void { | 
					
						
							| 
									
										
										
										
											2015-10-08 15:33:17 -07:00
										 |  |  |   jsmBeforeEach(() => { | 
					
						
							|  |  |  |     var providers = fn(); | 
					
						
							|  |  |  |     if (!providers) return; | 
					
						
							| 
									
										
										
										
											2015-12-08 19:03:21 -08:00
										 |  |  |     try { | 
					
						
							|  |  |  |       testInjector.addProviders(providers); | 
					
						
							|  |  |  |     } catch (e) { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       throw new Error( | 
					
						
							|  |  |  |           'beforeEachProviders was called after the injector had ' + | 
					
						
							|  |  |  |           'been used in a beforeEach or it block. This invalidates the ' + | 
					
						
							|  |  |  |           'test injector'); | 
					
						
							| 
									
										
										
										
											2015-10-08 15:33:17 -07:00
										 |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-26 13:06:50 -07:00
										 |  |  | function _wrapTestFn(fn: Function) { | 
					
						
							|  |  |  |   // Wraps a test or beforeEach function to handle synchronous and asynchronous execution.
 | 
					
						
							|  |  |  |   return (done: any) => { | 
					
						
							|  |  |  |     if (fn.length === 0) { | 
					
						
							|  |  |  |       let retVal = fn(); | 
					
						
							|  |  |  |       if (isPromise(retVal)) { | 
					
						
							|  |  |  |         // Asynchronous test function - wait for completion.
 | 
					
						
							| 
									
										
										
										
											2016-06-20 17:54:12 -07:00
										 |  |  |         (<Promise<any>>retVal).then(done, (err) => { | 
					
						
							|  |  |  |           if (isString(err)) { | 
					
						
							|  |  |  |             return done.fail(new Error(err)); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           return done.fail(err); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2015-12-08 16:44:04 -08:00
										 |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2016-04-26 13:06:50 -07:00
										 |  |  |         // Synchronous test function - complete immediately.
 | 
					
						
							| 
									
										
										
										
											2016-01-20 15:55:30 -08:00
										 |  |  |         done(); | 
					
						
							| 
									
										
										
										
											2015-11-18 18:46:24 -08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2016-04-26 13:06:50 -07:00
										 |  |  |     } else { | 
					
						
							|  |  |  |       // Asynchronous test function that takes "done" as parameter.
 | 
					
						
							|  |  |  |       fn(done); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function _it(jsmFn: Function, name: string, testFn: Function, testTimeOut: number): void { | 
					
						
							|  |  |  |   jsmFn(name, _wrapTestFn(testFn), testTimeOut); | 
					
						
							| 
									
										
										
										
											2015-10-08 15:33:17 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-18 18:46:24 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Wrapper around Jasmine beforeEach function. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * beforeEach may be used with the `inject` function to fetch dependencies. | 
					
						
							| 
									
										
										
										
											2015-11-30 08:28:54 -08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * See http://jasmine.github.io/ for more details.
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ## Example: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * {@example testing/ts/testing.ts region='beforeEach'} | 
					
						
							| 
									
										
										
										
											2015-11-18 18:46:24 -08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-04-26 13:06:50 -07:00
										 |  |  | export function beforeEach(fn: Function): void { | 
					
						
							|  |  |  |   jsmBeforeEach(_wrapTestFn(fn)); | 
					
						
							| 
									
										
										
										
											2015-10-08 15:33:17 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-18 18:46:24 -08:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2015-11-30 08:28:54 -08:00
										 |  |  |  * Define a single test case with the given test name and execution function. | 
					
						
							| 
									
										
										
										
											2015-11-18 18:46:24 -08:00
										 |  |  |  * | 
					
						
							| 
									
										
											  
											
												feat(tests): manage asynchronous tests using zones
Instead of using injectAsync and returning a promise, use the `async` function
to wrap tests. This will run the test inside a zone which does not complete
the test until all asynchronous tasks have been completed.
`async` may be used with the `inject` function, or separately.
BREAKING CHANGE:
`injectAsync` is now deprecated. Instead, use the `async` function
to wrap any asynchronous tests.
Before:
```
it('should wait for returned promises', injectAsync([FancyService], (service) => {
  return service.getAsyncValue().then((value) => { expect(value).toEqual('async value'); });
}));
it('should wait for returned promises', injectAsync([], () => {
  return somePromise.then(() => { expect(true).toEqual(true); });
}));
```
After:
```
it('should wait for returned promises', async(inject([FancyService], (service) => {
  service.getAsyncValue().then((value) => { expect(value).toEqual('async value'); });
})));
// Note that if there is no injection, we no longer need `inject` OR `injectAsync`.
it('should wait for returned promises', async(() => {
  somePromise.then() => { expect(true).toEqual(true); });
}));
```
Closes #7735
											
										 
											2016-03-23 10:59:38 -07:00
										 |  |  |  * The test function can be either a synchronous function, the result of {@link async}, | 
					
						
							|  |  |  |  * or an injected function created via {@link inject}. | 
					
						
							| 
									
										
										
										
											2015-11-30 08:28:54 -08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Wrapper around Jasmine it function. See http://jasmine.github.io/ for more details.
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ## Example: | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  |  * {@example testing/ts/testing.ts region='describeIt'} | 
					
						
							| 
									
										
										
										
											2015-11-18 18:46:24 -08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-04-26 13:06:50 -07:00
										 |  |  | export function it(name: string, fn: Function, timeOut: number = null): void { | 
					
						
							| 
									
										
										
										
											2015-10-08 15:33:17 -07:00
										 |  |  |   return _it(jsmIt, name, fn, timeOut); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-18 18:46:24 -08:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2015-11-30 08:28:54 -08:00
										 |  |  |  * Like {@link it}, but instructs the test runner to exclude this test | 
					
						
							|  |  |  |  * entirely. Useful for debugging or for excluding broken tests until | 
					
						
							|  |  |  |  * they can be fixed. | 
					
						
							| 
									
										
										
										
											2015-11-18 18:46:24 -08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-11-30 08:28:54 -08:00
										 |  |  |  * Wrapper around Jasmine xit function. See http://jasmine.github.io/ for more details.
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ## Example: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * {@example testing/ts/testing.ts region='xit'} | 
					
						
							| 
									
										
										
										
											2015-11-18 18:46:24 -08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-04-26 13:06:50 -07:00
										 |  |  | export function xit(name: string, fn: Function, timeOut: number = null): void { | 
					
						
							| 
									
										
										
										
											2015-10-08 15:33:17 -07:00
										 |  |  |   return _it(jsmXIt, name, fn, timeOut); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-18 18:46:24 -08:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2015-11-30 08:28:54 -08:00
										 |  |  |  * See {@link fit}. | 
					
						
							| 
									
										
										
										
											2015-11-18 18:46:24 -08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-04-26 13:06:50 -07:00
										 |  |  | export function iit(name: string, fn: Function, timeOut: number = null): void { | 
					
						
							| 
									
										
										
										
											2015-10-08 15:33:17 -07:00
										 |  |  |   return _it(jsmIIt, name, fn, timeOut); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-18 18:46:24 -08:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2015-11-30 08:28:54 -08:00
										 |  |  |  * Like {@link it}, but instructs the test runner to only run this test. | 
					
						
							|  |  |  |  * Useful for debugging. | 
					
						
							| 
									
										
										
										
											2015-11-18 18:46:24 -08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-11-30 08:28:54 -08:00
										 |  |  |  * Wrapper around Jasmine fit function. See http://jasmine.github.io/ for more details.
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ## Example: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * {@example testing/ts/testing.ts region='fit'} | 
					
						
							| 
									
										
										
										
											2015-11-18 18:46:24 -08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-04-26 13:06:50 -07:00
										 |  |  | export function fit(name: string, fn: Function, timeOut: number = null): void { | 
					
						
							| 
									
										
										
										
											2015-10-08 15:33:17 -07:00
										 |  |  |   return _it(jsmIIt, name, fn, timeOut); | 
					
						
							|  |  |  | } |