2016-06-23 12:47:54 -04: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
|
|
|
|
*/
|
|
|
|
|
refactor(testing): remove wrapping of Jasmine functions (#9564)
Instead, the async function now determines whether it should return a promise
or instead call a done function parameter. Importing Jasmine functions
from `@angular/core/testing` is no longer necessary and is now deprecated.
Additionally, beforeEachProviders is also deprecated, as it is specific
to the testing framework. Instead, use the new addProviders method directly.
Before:
```js
import {beforeEachProviders, it, describe, inject} from 'angular2/testing/core';
describe('my code', () => {
beforeEachProviders(() => [MyService]);
it('does stuff', inject([MyService], (service) => {
// actual test
});
});
```
After:
```js
import {addProviders, inject} from 'angular2/testing/core';
describe('my code', () => {
beforeEach(() => {
addProviders([MyService]);
});
it('does stuff', inject([MyService], (service) => {
// actual test
});
});
```
2016-06-24 20:48:35 -04:00
|
|
|
declare var global: any;
|
|
|
|
|
|
|
|
var _global = <any>(typeof window === 'undefined' ? global : window);
|
|
|
|
|
2016-04-26 16:06:50 -04:00
|
|
|
/**
|
|
|
|
* Wraps a test function in an asynchronous test zone. The test will automatically
|
|
|
|
* complete when all asynchronous calls within this zone are done. Can be used
|
|
|
|
* to wrap an {@link inject} call.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* it('...', async(inject([AClass], (object) => {
|
|
|
|
* object.doSomething.then(() => {
|
|
|
|
* expect(...);
|
|
|
|
* })
|
|
|
|
* });
|
|
|
|
* ```
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
|
|
|
* @stable
|
2016-04-26 16:06:50 -04:00
|
|
|
*/
|
refactor(testing): remove wrapping of Jasmine functions (#9564)
Instead, the async function now determines whether it should return a promise
or instead call a done function parameter. Importing Jasmine functions
from `@angular/core/testing` is no longer necessary and is now deprecated.
Additionally, beforeEachProviders is also deprecated, as it is specific
to the testing framework. Instead, use the new addProviders method directly.
Before:
```js
import {beforeEachProviders, it, describe, inject} from 'angular2/testing/core';
describe('my code', () => {
beforeEachProviders(() => [MyService]);
it('does stuff', inject([MyService], (service) => {
// actual test
});
});
```
After:
```js
import {addProviders, inject} from 'angular2/testing/core';
describe('my code', () => {
beforeEach(() => {
addProviders([MyService]);
});
it('does stuff', inject([MyService], (service) => {
// actual test
});
});
```
2016-06-24 20:48:35 -04:00
|
|
|
export function async(fn: Function): (done: any) => any {
|
|
|
|
// If we're running using the Jasmine test framework, adapt to call the 'done'
|
|
|
|
// function when asynchronous activity is finished.
|
|
|
|
if (_global.jasmine) {
|
|
|
|
return (done: any) => {
|
|
|
|
runInTestZone(fn, done, (err: string | Error) => {
|
|
|
|
if (typeof err === 'string') {
|
|
|
|
return done.fail(new Error(<string>err));
|
|
|
|
} else {
|
|
|
|
done.fail(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// Otherwise, return a promise which will resolve when asynchronous activity
|
|
|
|
// is finished. This will be correctly consumed by the Mocha framework with
|
|
|
|
// it('...', async(myFn)); or can be used in a custom framework.
|
2016-05-26 16:33:53 -04:00
|
|
|
return () => new Promise<void>((finishCallback, failCallback) => {
|
refactor(testing): remove wrapping of Jasmine functions (#9564)
Instead, the async function now determines whether it should return a promise
or instead call a done function parameter. Importing Jasmine functions
from `@angular/core/testing` is no longer necessary and is now deprecated.
Additionally, beforeEachProviders is also deprecated, as it is specific
to the testing framework. Instead, use the new addProviders method directly.
Before:
```js
import {beforeEachProviders, it, describe, inject} from 'angular2/testing/core';
describe('my code', () => {
beforeEachProviders(() => [MyService]);
it('does stuff', inject([MyService], (service) => {
// actual test
});
});
```
After:
```js
import {addProviders, inject} from 'angular2/testing/core';
describe('my code', () => {
beforeEach(() => {
addProviders([MyService]);
});
it('does stuff', inject([MyService], (service) => {
// actual test
});
});
```
2016-06-24 20:48:35 -04:00
|
|
|
runInTestZone(fn, finishCallback, failCallback);
|
2016-06-08 19:38:52 -04:00
|
|
|
});
|
2016-04-26 16:06:50 -04:00
|
|
|
}
|
refactor(testing): remove wrapping of Jasmine functions (#9564)
Instead, the async function now determines whether it should return a promise
or instead call a done function parameter. Importing Jasmine functions
from `@angular/core/testing` is no longer necessary and is now deprecated.
Additionally, beforeEachProviders is also deprecated, as it is specific
to the testing framework. Instead, use the new addProviders method directly.
Before:
```js
import {beforeEachProviders, it, describe, inject} from 'angular2/testing/core';
describe('my code', () => {
beforeEachProviders(() => [MyService]);
it('does stuff', inject([MyService], (service) => {
// actual test
});
});
```
After:
```js
import {addProviders, inject} from 'angular2/testing/core';
describe('my code', () => {
beforeEach(() => {
addProviders([MyService]);
});
it('does stuff', inject([MyService], (service) => {
// actual test
});
});
```
2016-06-24 20:48:35 -04:00
|
|
|
|
|
|
|
function runInTestZone(fn: Function, finishCallback: Function, failCallback: Function) {
|
|
|
|
var AsyncTestZoneSpec = (Zone as any /** TODO #9100 */)['AsyncTestZoneSpec'];
|
|
|
|
if (AsyncTestZoneSpec === undefined) {
|
|
|
|
throw new Error(
|
|
|
|
'AsyncTestZoneSpec is needed for the async() test helper but could not be found. ' +
|
|
|
|
'Please make sure that your environment includes zone.js/dist/async-test.js');
|
|
|
|
}
|
|
|
|
var testZoneSpec = new AsyncTestZoneSpec(finishCallback, failCallback, 'test');
|
|
|
|
var testZone = Zone.current.fork(testZoneSpec);
|
|
|
|
return testZone.run(fn);
|
|
|
|
}
|