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
|
|
|
|
*/
|
|
|
|
|
2018-04-01 00:44:38 +09:00
|
|
|
import {asyncFallback} from './async_fallback';
|
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 17:48:35 -07:00
|
|
|
|
2016-04-26 13:06:50 -07: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 12:27:23 -07:00
|
|
|
*
|
2018-10-19 16:27:04 +01:00
|
|
|
* @publicApi
|
2016-04-26 13:06:50 -07: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 17:48:35 -07:00
|
|
|
export function async(fn: Function): (done: any) => any {
|
2018-04-01 00:44:38 +09:00
|
|
|
const _Zone: any = typeof Zone !== 'undefined' ? Zone : null;
|
|
|
|
if (!_Zone) {
|
|
|
|
return function() {
|
|
|
|
return Promise.reject(
|
|
|
|
'Zone is needed for the async() test helper but could not be found. ' +
|
|
|
|
'Please make sure that your environment includes zone.js/dist/zone.js');
|
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 17:48:35 -07:00
|
|
|
};
|
|
|
|
}
|
2018-04-01 00:44:38 +09:00
|
|
|
const asyncTest = _Zone && _Zone[_Zone.__symbol__('asyncTest')];
|
|
|
|
if (typeof asyncTest === 'function') {
|
|
|
|
return asyncTest(fn);
|
2016-08-19 12:10:53 -07:00
|
|
|
}
|
2018-04-01 00:44:38 +09:00
|
|
|
// not using new version of zone.js
|
|
|
|
// TODO @JiaLiPassion, remove this after all library updated to
|
|
|
|
// newest version of zone.js(0.8.25)
|
|
|
|
return asyncFallback(fn);
|
|
|
|
}
|