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
|
|
|
|
*/
|
|
|
|
|
feat(testing): add implicit test module
Every test now has an implicit module. It can be configured via `configureModule` (from @angular/core/testing)
to add providers, directives, pipes, ...
The compiler now has to be configured separately via `configureCompiler` (from @angular/core/testing)
to add providers or define whether to use jit.
BREAKING CHANGE:
- Application providers can no longer inject compiler internals (i.e. everything
from `@angular/compiler). Inject `Compiler` instead. This reflects the
changes to `bootstrap` for module support (3f55aa609f60f130f1d69188ed057214b1267cb3).
- Compiler providers can no longer be added via `addProviders` / `withProviders`.
Use the new method `configureCompiler` instead.
- Platform directives / pipes need to be provided via
`configureModule` and can no longer be provided via the
`PLATFORM_PIPES` / `PLATFORM_DIRECTIVES` tokens.
- `setBaseTestProviders()` was renamed into `initTestEnvironment` and
now takes a `PlatformRef` and a factory for a
`Compiler`.
- E.g. for the browser platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS} from ‘@angular/platform-browser-dynamic/testing’;
setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
```
AFTER:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {browserTestCompiler, browserDynamicTestPlatform,
BrowserDynamicTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
browserTestCompiler,
browserDynamicTestPlatform(),
BrowserDynamicTestModule);
```
- E.g. for the server platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS} from ‘@angular/platform-server/testing/server’;
setBaseTestProviders(TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS);
```
AFTER:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {serverTestCompiler, serverTestPlatform,
ServerTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
serverTestCompiler,
serverTestPlatform(),
ServerTestModule);
```
Related to #9726
Closes #9846
2016-07-04 12:37:30 -04:00
|
|
|
import {NgIf} from '@angular/common';
|
|
|
|
import {CompilerConfig, XHR} from '@angular/compiler';
|
|
|
|
import {AppModule, Component, ComponentFactoryResolver, Directive, Injectable, Input, Pipe, ViewMetadata, provide} from '@angular/core';
|
2016-07-11 19:04:32 -04:00
|
|
|
import {TestComponentBuilder, addProviders, async, configureCompiler, configureModule, fakeAsync, inject, tick, withModule, withProviders} from '@angular/core/testing';
|
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
|
|
|
import {expect} from '@angular/platform-browser/testing/matchers';
|
feat(testing): add implicit test module
Every test now has an implicit module. It can be configured via `configureModule` (from @angular/core/testing)
to add providers, directives, pipes, ...
The compiler now has to be configured separately via `configureCompiler` (from @angular/core/testing)
to add providers or define whether to use jit.
BREAKING CHANGE:
- Application providers can no longer inject compiler internals (i.e. everything
from `@angular/compiler). Inject `Compiler` instead. This reflects the
changes to `bootstrap` for module support (3f55aa609f60f130f1d69188ed057214b1267cb3).
- Compiler providers can no longer be added via `addProviders` / `withProviders`.
Use the new method `configureCompiler` instead.
- Platform directives / pipes need to be provided via
`configureModule` and can no longer be provided via the
`PLATFORM_PIPES` / `PLATFORM_DIRECTIVES` tokens.
- `setBaseTestProviders()` was renamed into `initTestEnvironment` and
now takes a `PlatformRef` and a factory for a
`Compiler`.
- E.g. for the browser platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS} from ‘@angular/platform-browser-dynamic/testing’;
setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
```
AFTER:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {browserTestCompiler, browserDynamicTestPlatform,
BrowserDynamicTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
browserTestCompiler,
browserDynamicTestPlatform(),
BrowserDynamicTestModule);
```
- E.g. for the server platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS} from ‘@angular/platform-server/testing/server’;
setBaseTestProviders(TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS);
```
AFTER:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {serverTestCompiler, serverTestPlatform,
ServerTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
serverTestCompiler,
serverTestPlatform(),
ServerTestModule);
```
Related to #9726
Closes #9846
2016-07-04 12:37:30 -04:00
|
|
|
|
|
|
|
import {stringify} from '../../http/src/facade/lang';
|
2016-04-28 20:50:03 -04:00
|
|
|
import {PromiseWrapper} from '../../http/src/facade/promise';
|
2015-10-08 18:33:17 -04:00
|
|
|
|
|
|
|
// Services, and components for the tests.
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component(
|
|
|
|
{selector: 'child-comp', template: `<span>Original {{childBinding}}</span>`, directives: []})
|
2015-10-08 18:33:17 -04:00
|
|
|
@Injectable()
|
|
|
|
class ChildComp {
|
|
|
|
childBinding: string;
|
|
|
|
constructor() { this.childBinding = 'Child'; }
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({selector: 'child-comp', template: `<span>Mock</span>`})
|
2015-10-08 18:33:17 -04:00
|
|
|
@Injectable()
|
|
|
|
class MockChildComp {
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({
|
|
|
|
selector: 'parent-comp',
|
|
|
|
template: `Parent(<child-comp></child-comp>)`,
|
|
|
|
directives: [ChildComp]
|
|
|
|
})
|
2015-10-08 18:33:17 -04:00
|
|
|
@Injectable()
|
|
|
|
class ParentComp {
|
|
|
|
}
|
|
|
|
|
2016-07-19 11:59:14 -04:00
|
|
|
@Component({selector: 'my-if-comp', template: `MyIf(<span *ngIf="showMore">More</span>)`})
|
2015-10-08 18:33:17 -04:00
|
|
|
@Injectable()
|
|
|
|
class MyIfComp {
|
|
|
|
showMore: boolean = false;
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({selector: 'child-child-comp', template: `<span>ChildChild</span>`})
|
2015-10-08 18:33:17 -04:00
|
|
|
@Injectable()
|
|
|
|
class ChildChildComp {
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({
|
|
|
|
selector: 'child-comp',
|
2015-10-08 18:33:17 -04:00
|
|
|
template: `<span>Original {{childBinding}}(<child-child-comp></child-child-comp>)</span>`,
|
|
|
|
directives: [ChildChildComp]
|
|
|
|
})
|
|
|
|
@Injectable()
|
|
|
|
class ChildWithChildComp {
|
|
|
|
childBinding: string;
|
|
|
|
constructor() { this.childBinding = 'Child'; }
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({selector: 'child-child-comp', template: `<span>ChildChild Mock</span>`})
|
2015-10-08 18:33:17 -04:00
|
|
|
@Injectable()
|
|
|
|
class MockChildChildComp {
|
|
|
|
}
|
|
|
|
|
|
|
|
class FancyService {
|
|
|
|
value: string = 'real value';
|
|
|
|
getAsyncValue() { return Promise.resolve('async value'); }
|
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 13:59:38 -04:00
|
|
|
getTimeoutValue() {
|
|
|
|
return new Promise((resolve, reject) => { setTimeout(() => {resolve('timeout value')}, 10); })
|
|
|
|
}
|
2015-10-08 18:33:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class MockFancyService extends FancyService {
|
|
|
|
value: string = 'mocked out value';
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({
|
|
|
|
selector: 'my-service-comp',
|
|
|
|
providers: [FancyService],
|
|
|
|
template: `injected value: {{fancyService.value}}`
|
|
|
|
})
|
2015-10-08 18:33:17 -04:00
|
|
|
class TestProvidersComp {
|
|
|
|
constructor(private fancyService: FancyService) {}
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({
|
|
|
|
selector: 'my-service-comp',
|
|
|
|
viewProviders: [FancyService],
|
|
|
|
template: `injected value: {{fancyService.value}}`
|
|
|
|
})
|
2015-10-08 18:33:17 -04:00
|
|
|
class TestViewProvidersComp {
|
|
|
|
constructor(private fancyService: FancyService) {}
|
|
|
|
}
|
|
|
|
|
feat(testing): add implicit test module
Every test now has an implicit module. It can be configured via `configureModule` (from @angular/core/testing)
to add providers, directives, pipes, ...
The compiler now has to be configured separately via `configureCompiler` (from @angular/core/testing)
to add providers or define whether to use jit.
BREAKING CHANGE:
- Application providers can no longer inject compiler internals (i.e. everything
from `@angular/compiler). Inject `Compiler` instead. This reflects the
changes to `bootstrap` for module support (3f55aa609f60f130f1d69188ed057214b1267cb3).
- Compiler providers can no longer be added via `addProviders` / `withProviders`.
Use the new method `configureCompiler` instead.
- Platform directives / pipes need to be provided via
`configureModule` and can no longer be provided via the
`PLATFORM_PIPES` / `PLATFORM_DIRECTIVES` tokens.
- `setBaseTestProviders()` was renamed into `initTestEnvironment` and
now takes a `PlatformRef` and a factory for a
`Compiler`.
- E.g. for the browser platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS} from ‘@angular/platform-browser-dynamic/testing’;
setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
```
AFTER:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {browserTestCompiler, browserDynamicTestPlatform,
BrowserDynamicTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
browserTestCompiler,
browserDynamicTestPlatform(),
BrowserDynamicTestModule);
```
- E.g. for the server platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS} from ‘@angular/platform-server/testing/server’;
setBaseTestProviders(TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS);
```
AFTER:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {serverTestCompiler, serverTestPlatform,
ServerTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
serverTestCompiler,
serverTestPlatform(),
ServerTestModule);
```
Related to #9726
Closes #9846
2016-07-04 12:37:30 -04:00
|
|
|
@Directive({selector: '[someDir]', host: {'[title]': 'someDir'}})
|
|
|
|
class SomeDirective {
|
|
|
|
@Input()
|
|
|
|
someDir: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Pipe({name: 'somePipe'})
|
|
|
|
class SomePipe {
|
|
|
|
transform(value: string): any { return `transformed ${value}`; }
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({selector: 'comp', template: `<div [someDir]="'someValue' | somePipe"></div>`})
|
|
|
|
class CompUsingModuleDirectiveAndPipe {
|
|
|
|
}
|
|
|
|
|
|
|
|
@AppModule({})
|
|
|
|
class SomeNestedModule {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({selector: 'comp', templateUrl: 'someTemplate.html'})
|
|
|
|
class CompWithUrlTemplate {
|
|
|
|
}
|
|
|
|
|
2015-10-08 18:33:17 -04:00
|
|
|
export function main() {
|
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 13:59:38 -04:00
|
|
|
describe('using the async helper', () => {
|
|
|
|
var actuallyDone: boolean;
|
2015-11-17 15:37:39 -05: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 13:59:38 -04:00
|
|
|
beforeEach(() => { actuallyDone = false; });
|
2015-11-17 15:37:39 -05: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 13:59:38 -04:00
|
|
|
afterEach(() => { expect(actuallyDone).toEqual(true); });
|
2015-11-17 15:37:39 -05: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 13:59:38 -04:00
|
|
|
it('should run normal tests', () => { actuallyDone = true; });
|
2015-10-08 18:33:17 -04:00
|
|
|
|
2016-06-08 18:45:15 -04:00
|
|
|
it('should run normal async tests', (done: any /** TODO #9100 */) => {
|
2015-10-08 18:33:17 -04:00
|
|
|
setTimeout(() => {
|
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 13:59:38 -04:00
|
|
|
actuallyDone = true;
|
2015-10-08 18:33:17 -04:00
|
|
|
done();
|
|
|
|
}, 0);
|
|
|
|
});
|
|
|
|
|
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 13:59:38 -04:00
|
|
|
it('should run async tests with tasks',
|
|
|
|
async(() => { setTimeout(() => { actuallyDone = true; }, 0); }));
|
2015-10-14 12:41:15 -04: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 13:59:38 -04:00
|
|
|
it('should run async tests with promises', async(() => {
|
|
|
|
var p = new Promise((resolve, reject) => { setTimeout(resolve, 10); });
|
|
|
|
p.then(() => { actuallyDone = true; });
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('using the test injector with the inject helper', () => {
|
2015-10-08 18:33:17 -04:00
|
|
|
describe('setting up Providers', () => {
|
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
|
|
|
beforeEach(() => addProviders([{provide: FancyService, useValue: new FancyService()}]));
|
2015-10-08 18:33:17 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should use set up providers', inject([FancyService], (service: any /** TODO #9100 */) => {
|
|
|
|
expect(service.value).toEqual('real value');
|
|
|
|
}));
|
2015-10-08 18:33:17 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should wait until returned promises',
|
|
|
|
async(inject([FancyService], (service: any /** TODO #9100 */) => {
|
|
|
|
service.getAsyncValue().then(
|
|
|
|
(value: any /** TODO #9100 */) => { expect(value).toEqual('async value'); });
|
|
|
|
service.getTimeoutValue().then(
|
|
|
|
(value: any /** TODO #9100 */) => { expect(value).toEqual('timeout value'); });
|
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 13:59:38 -04:00
|
|
|
})));
|
2015-12-14 21:51:37 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should allow the use of fakeAsync',
|
|
|
|
fakeAsync(inject([FancyService], (service: any /** TODO #9100 */) => {
|
2016-06-08 18:45:15 -04:00
|
|
|
var value: any /** TODO #9100 */;
|
|
|
|
service.getAsyncValue().then(function(val: any /** TODO #9100 */) { value = val; });
|
2016-04-18 19:04:35 -04:00
|
|
|
tick();
|
|
|
|
expect(value).toEqual('async value');
|
|
|
|
})));
|
|
|
|
|
2016-06-08 18:45:15 -04:00
|
|
|
it('should allow use of "done"', (done: any /** TODO #9100 */) => {
|
|
|
|
inject([FancyService], (service: any /** TODO #9100 */) => {
|
2016-04-26 16:06:50 -04:00
|
|
|
let count = 0;
|
|
|
|
let id = setInterval(() => {
|
|
|
|
count++;
|
|
|
|
if (count > 2) {
|
|
|
|
clearInterval(id);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
}, 5);
|
|
|
|
})(); // inject needs to be invoked explicitly with ().
|
|
|
|
});
|
|
|
|
|
2015-10-08 18:33:17 -04:00
|
|
|
describe('using beforeEach', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
beforeEach(inject([FancyService], (service: any /** TODO #9100 */) => {
|
|
|
|
service.value = 'value modified in beforeEach';
|
|
|
|
}));
|
2015-10-08 18:33:17 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should use modified providers',
|
|
|
|
inject([FancyService], (service: any /** TODO #9100 */) => {
|
2015-10-08 18:33:17 -04:00
|
|
|
expect(service.value).toEqual('value modified in beforeEach');
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('using async beforeEach', () => {
|
2016-06-08 18:45:15 -04:00
|
|
|
beforeEach(async(inject([FancyService], (service: any /** TODO #9100 */) => {
|
2016-06-08 19:38:52 -04:00
|
|
|
service.getAsyncValue().then(
|
|
|
|
(value: any /** TODO #9100 */) => { service.value = value; });
|
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 13:59:38 -04:00
|
|
|
})));
|
2015-10-08 18:33:17 -04:00
|
|
|
|
|
|
|
it('should use asynchronously modified value',
|
2016-06-08 19:38:52 -04:00
|
|
|
inject([FancyService], (service: any /** TODO #9100 */) => {
|
|
|
|
expect(service.value).toEqual('async value');
|
|
|
|
}));
|
2015-10-08 18:33:17 -04:00
|
|
|
});
|
|
|
|
});
|
2015-12-10 15:00:48 -05:00
|
|
|
|
|
|
|
describe('per test providers', () => {
|
|
|
|
it('should allow per test providers',
|
2016-06-02 20:30:40 -04:00
|
|
|
withProviders(() => [{provide: FancyService, useValue: new FancyService()}])
|
2016-06-08 19:38:52 -04:00
|
|
|
.inject([FancyService], (service: any /** TODO #9100 */) => {
|
|
|
|
expect(service.value).toEqual('real value');
|
|
|
|
}));
|
2016-04-26 16:06:50 -04:00
|
|
|
|
|
|
|
it('should return value from inject', () => {
|
2016-06-02 20:30:40 -04:00
|
|
|
let retval = withProviders(() => [{provide: FancyService, useValue: new FancyService()}])
|
2016-06-08 18:45:15 -04:00
|
|
|
.inject([FancyService], (service: any /** TODO #9100 */) => {
|
2016-04-26 16:06:50 -04:00
|
|
|
expect(service.value).toEqual('real value');
|
|
|
|
return 10;
|
|
|
|
})();
|
|
|
|
expect(retval).toBe(10);
|
|
|
|
});
|
2015-12-10 15:00:48 -05:00
|
|
|
});
|
2015-10-08 18:33:17 -04:00
|
|
|
});
|
|
|
|
|
feat(testing): add implicit test module
Every test now has an implicit module. It can be configured via `configureModule` (from @angular/core/testing)
to add providers, directives, pipes, ...
The compiler now has to be configured separately via `configureCompiler` (from @angular/core/testing)
to add providers or define whether to use jit.
BREAKING CHANGE:
- Application providers can no longer inject compiler internals (i.e. everything
from `@angular/compiler). Inject `Compiler` instead. This reflects the
changes to `bootstrap` for module support (3f55aa609f60f130f1d69188ed057214b1267cb3).
- Compiler providers can no longer be added via `addProviders` / `withProviders`.
Use the new method `configureCompiler` instead.
- Platform directives / pipes need to be provided via
`configureModule` and can no longer be provided via the
`PLATFORM_PIPES` / `PLATFORM_DIRECTIVES` tokens.
- `setBaseTestProviders()` was renamed into `initTestEnvironment` and
now takes a `PlatformRef` and a factory for a
`Compiler`.
- E.g. for the browser platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS} from ‘@angular/platform-browser-dynamic/testing’;
setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
```
AFTER:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {browserTestCompiler, browserDynamicTestPlatform,
BrowserDynamicTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
browserTestCompiler,
browserDynamicTestPlatform(),
BrowserDynamicTestModule);
```
- E.g. for the server platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS} from ‘@angular/platform-server/testing/server’;
setBaseTestProviders(TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS);
```
AFTER:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {serverTestCompiler, serverTestPlatform,
ServerTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
serverTestCompiler,
serverTestPlatform(),
ServerTestModule);
```
Related to #9726
Closes #9846
2016-07-04 12:37:30 -04:00
|
|
|
describe('using the test injector with modules', () => {
|
|
|
|
let moduleConfig: any;
|
|
|
|
beforeEach(() => {
|
|
|
|
moduleConfig = {
|
|
|
|
providers: [FancyService],
|
|
|
|
directives: [SomeDirective],
|
|
|
|
pipes: [SomePipe],
|
|
|
|
precompile: [CompUsingModuleDirectiveAndPipe],
|
|
|
|
modules: [SomeNestedModule]
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setting up a module', () => {
|
|
|
|
beforeEach(() => configureModule(moduleConfig));
|
|
|
|
|
|
|
|
it('should use set up providers', inject([FancyService], (service: FancyService) => {
|
|
|
|
expect(service.value).toEqual('real value');
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should use set up directives and pipes',
|
|
|
|
inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
|
|
|
let compFixture = tcb.createSync(CompUsingModuleDirectiveAndPipe);
|
|
|
|
let el = compFixture.debugElement;
|
|
|
|
|
|
|
|
compFixture.detectChanges();
|
|
|
|
expect(el.children[0].properties['title']).toBe('transformed someValue');
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should use set up nested modules',
|
|
|
|
inject([SomeNestedModule], (nestedModule: SomeNestedModule) => {
|
|
|
|
expect(nestedModule).toBeAnInstanceOf(SomeNestedModule);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should use set up precompile components',
|
|
|
|
inject([ComponentFactoryResolver], (resolver: ComponentFactoryResolver) => {
|
|
|
|
expect(resolver.resolveComponentFactory(CompUsingModuleDirectiveAndPipe).componentType)
|
|
|
|
.toBe(CompUsingModuleDirectiveAndPipe);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('per test modules', () => {
|
|
|
|
it('should use set up providers',
|
|
|
|
withModule(() => moduleConfig).inject([FancyService], (service: FancyService) => {
|
|
|
|
expect(service.value).toEqual('real value');
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should use set up directives and pipes',
|
|
|
|
withModule(() => moduleConfig)
|
|
|
|
.inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
|
|
|
let compFixture = tcb.createSync(CompUsingModuleDirectiveAndPipe);
|
|
|
|
let el = compFixture.debugElement;
|
|
|
|
|
|
|
|
compFixture.detectChanges();
|
|
|
|
expect(el.children[0].properties['title']).toBe('transformed someValue');
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should use set up nested modules',
|
|
|
|
withModule(() => moduleConfig)
|
|
|
|
.inject([SomeNestedModule], (nestedModule: SomeNestedModule) => {
|
|
|
|
expect(nestedModule).toBeAnInstanceOf(SomeNestedModule);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should use set up precompile components',
|
|
|
|
withModule(() => moduleConfig)
|
|
|
|
.inject([ComponentFactoryResolver], (resolver: ComponentFactoryResolver) => {
|
|
|
|
expect(
|
|
|
|
resolver.resolveComponentFactory(CompUsingModuleDirectiveAndPipe).componentType)
|
|
|
|
.toBe(CompUsingModuleDirectiveAndPipe);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('precompile components with template url', () => {
|
|
|
|
let xhrGet: jasmine.Spy;
|
|
|
|
beforeEach(() => {
|
|
|
|
xhrGet = jasmine.createSpy('xhrGet').and.returnValue(Promise.resolve('Hello world!'));
|
|
|
|
configureCompiler({providers: [{provide: XHR, useValue: {get: xhrGet}}]});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow to precompile components with templateUrl using the async helper',
|
|
|
|
async(withModule(() => {
|
|
|
|
return {precompile: [CompWithUrlTemplate]};
|
|
|
|
}).inject([ComponentFactoryResolver], (resolver: ComponentFactoryResolver) => {
|
|
|
|
expect(resolver.resolveComponentFactory(CompWithUrlTemplate).componentType)
|
|
|
|
.toBe(CompWithUrlTemplate);
|
|
|
|
})));
|
|
|
|
|
|
|
|
it('should allow to precompile components with templateUrl using the fakeAsync helper',
|
|
|
|
fakeAsync(withModule(() => {
|
|
|
|
return {precompile: [CompWithUrlTemplate]};
|
|
|
|
}).inject([ComponentFactoryResolver], (resolver: ComponentFactoryResolver) => {
|
|
|
|
expect(resolver.resolveComponentFactory(CompWithUrlTemplate).componentType)
|
|
|
|
.toBe(CompWithUrlTemplate);
|
|
|
|
})));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setting up the compiler', () => {
|
|
|
|
|
|
|
|
describe('providers', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
let xhrGet = jasmine.createSpy('xhrGet').and.returnValue(Promise.resolve('Hello world!'));
|
|
|
|
configureCompiler({providers: [{provide: XHR, useValue: {get: xhrGet}}]});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should use set up providers',
|
|
|
|
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
|
|
|
let compFixture = tcb.createFakeAsync(CompWithUrlTemplate);
|
|
|
|
expect(compFixture.nativeElement).toHaveText('Hello world!');
|
|
|
|
})));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('useJit true', () => {
|
|
|
|
beforeEach(() => { configureCompiler({useJit: true}); });
|
|
|
|
it('should set the value into CompilerConfig',
|
|
|
|
inject([CompilerConfig], (config: CompilerConfig) => {
|
|
|
|
expect(config.useJit).toBe(true);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
describe('useJit false', () => {
|
|
|
|
beforeEach(() => { configureCompiler({useJit: false}); });
|
|
|
|
it('should set the value into CompilerConfig',
|
|
|
|
inject([CompilerConfig], (config: CompilerConfig) => {
|
|
|
|
expect(config.useJit).toBe(false);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-10-08 18:33:17 -04:00
|
|
|
describe('errors', () => {
|
|
|
|
var originalJasmineIt: any;
|
|
|
|
var originalJasmineBeforeEach: any;
|
2015-11-18 21:46:24 -05:00
|
|
|
|
2015-10-08 18:33:17 -04:00
|
|
|
var patchJasmineIt = () => {
|
2015-11-18 21:46:24 -05:00
|
|
|
var deferred = PromiseWrapper.completer();
|
2015-10-08 18:33:17 -04:00
|
|
|
originalJasmineIt = jasmine.getEnv().it;
|
2016-06-08 18:45:15 -04:00
|
|
|
jasmine.getEnv().it = (description: string, fn: any /** TODO #9100 */) => {
|
2015-11-18 21:46:24 -05:00
|
|
|
var done = () => { deferred.resolve() };
|
2016-06-08 18:45:15 -04:00
|
|
|
(<any>done).fail = (err: any /** TODO #9100 */) => { deferred.reject(err) };
|
2015-10-08 18:33:17 -04:00
|
|
|
fn(done);
|
|
|
|
return null;
|
2015-11-18 21:46:24 -05:00
|
|
|
};
|
|
|
|
return deferred.promise;
|
2015-10-08 18:33:17 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
var restoreJasmineIt = () => { jasmine.getEnv().it = originalJasmineIt; };
|
|
|
|
|
|
|
|
var patchJasmineBeforeEach = () => {
|
2015-12-08 19:44:04 -05:00
|
|
|
var deferred = PromiseWrapper.completer();
|
2015-10-08 18:33:17 -04:00
|
|
|
originalJasmineBeforeEach = jasmine.getEnv().beforeEach;
|
|
|
|
jasmine.getEnv().beforeEach = (fn: any) => {
|
2015-12-08 19:44:04 -05:00
|
|
|
var done = () => { deferred.resolve() };
|
2016-06-08 18:45:15 -04:00
|
|
|
(<any>done).fail = (err: any /** TODO #9100 */) => { deferred.reject(err) };
|
2015-10-08 18:33:17 -04:00
|
|
|
fn(done);
|
|
|
|
return null;
|
2015-12-08 19:44:04 -05:00
|
|
|
};
|
|
|
|
return deferred.promise;
|
2015-10-08 18:33:17 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
var restoreJasmineBeforeEach =
|
2016-05-20 19:11:49 -04:00
|
|
|
() => { jasmine.getEnv().beforeEach = originalJasmineBeforeEach; };
|
2015-10-08 18:33:17 -04:00
|
|
|
|
2016-06-08 18:45:15 -04:00
|
|
|
it('should fail when an asynchronous error is thrown', (done: any /** TODO #9100 */) => {
|
2015-11-18 21:46:24 -05:00
|
|
|
var itPromise = patchJasmineIt();
|
2016-06-20 20:54:12 -04:00
|
|
|
var barError = new Error('bar');
|
2015-11-18 21:46:24 -05:00
|
|
|
|
|
|
|
it('throws an async error',
|
2016-06-20 20:54:12 -04:00
|
|
|
async(inject([], () => { setTimeout(() => { throw barError; }, 0); })));
|
2015-11-18 21:46:24 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
itPromise.then(
|
|
|
|
() => { done.fail('Expected test to fail, but it did not'); },
|
|
|
|
(err) => {
|
2016-06-20 20:54:12 -04:00
|
|
|
expect(err).toEqual(barError);
|
2016-06-08 19:38:52 -04:00
|
|
|
done();
|
|
|
|
});
|
2015-10-08 18:33:17 -04:00
|
|
|
restoreJasmineIt();
|
|
|
|
});
|
|
|
|
|
2016-06-08 18:45:15 -04:00
|
|
|
it('should fail when a returned promise is rejected', (done: any /** TODO #9100 */) => {
|
2015-11-18 21:46:24 -05:00
|
|
|
var itPromise = patchJasmineIt();
|
2015-10-08 18:33:17 -04: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 13:59:38 -04:00
|
|
|
it('should fail with an error from a promise', async(inject([], () => {
|
2015-11-18 21:46:24 -05:00
|
|
|
var deferred = PromiseWrapper.completer();
|
|
|
|
var p = deferred.promise.then(() => { expect(1).toEqual(2); });
|
|
|
|
|
|
|
|
deferred.reject('baz');
|
|
|
|
return p;
|
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 13:59:38 -04:00
|
|
|
})));
|
2015-11-18 21:46:24 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
itPromise.then(
|
|
|
|
() => { done.fail('Expected test to fail, but it did not'); },
|
|
|
|
(err) => {
|
2016-06-20 20:54:12 -04:00
|
|
|
expect(err.message).toEqual('Uncaught (in promise): baz');
|
2016-06-08 19:38:52 -04:00
|
|
|
done();
|
|
|
|
});
|
2015-11-18 21:46:24 -05:00
|
|
|
restoreJasmineIt();
|
2015-10-08 18:33:17 -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
|
|
|
describe('using addProviders', () => {
|
|
|
|
beforeEach(() => addProviders([{provide: FancyService, useValue: new FancyService()}]));
|
2015-10-08 18:33:17 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
beforeEach(inject([FancyService], (service: any /** TODO #9100 */) => {
|
|
|
|
expect(service.value).toEqual('real value');
|
|
|
|
}));
|
2015-10-08 18:33:17 -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
|
|
|
describe('nested addProviders', () => {
|
2015-10-08 18:33:17 -04:00
|
|
|
|
|
|
|
it('should fail when the injector has already been used', () => {
|
2015-12-08 19:44:04 -05:00
|
|
|
patchJasmineBeforeEach();
|
2015-10-08 18:33:17 -04:00
|
|
|
expect(() => {
|
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
|
|
|
beforeEach(() => addProviders([{provide: FancyService, useValue: new FancyService()}]));
|
2015-10-08 18:33:17 -04:00
|
|
|
})
|
2016-06-08 19:38:52 -04:00
|
|
|
.toThrowError(
|
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
|
|
|
'addProviders can\'t be called after the injector has been already created for this test. ' +
|
|
|
|
'This is most likely because you\'ve already used the injector to inject a beforeEach or the ' +
|
|
|
|
'current `it` function.');
|
2015-10-08 18:33:17 -04:00
|
|
|
restoreJasmineBeforeEach();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
feat(testing): add implicit test module
Every test now has an implicit module. It can be configured via `configureModule` (from @angular/core/testing)
to add providers, directives, pipes, ...
The compiler now has to be configured separately via `configureCompiler` (from @angular/core/testing)
to add providers or define whether to use jit.
BREAKING CHANGE:
- Application providers can no longer inject compiler internals (i.e. everything
from `@angular/compiler). Inject `Compiler` instead. This reflects the
changes to `bootstrap` for module support (3f55aa609f60f130f1d69188ed057214b1267cb3).
- Compiler providers can no longer be added via `addProviders` / `withProviders`.
Use the new method `configureCompiler` instead.
- Platform directives / pipes need to be provided via
`configureModule` and can no longer be provided via the
`PLATFORM_PIPES` / `PLATFORM_DIRECTIVES` tokens.
- `setBaseTestProviders()` was renamed into `initTestEnvironment` and
now takes a `PlatformRef` and a factory for a
`Compiler`.
- E.g. for the browser platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS} from ‘@angular/platform-browser-dynamic/testing’;
setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
```
AFTER:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {browserTestCompiler, browserDynamicTestPlatform,
BrowserDynamicTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
browserTestCompiler,
browserDynamicTestPlatform(),
BrowserDynamicTestModule);
```
- E.g. for the server platform:
BEFORE:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS} from ‘@angular/platform-server/testing/server’;
setBaseTestProviders(TEST_SERVER_PLATFORM_PROVIDERS,
TEST_SERVER_APPLICATION_PROVIDERS);
```
AFTER:
```
import {setBaseTestProviders} from ‘@angular/core/testing’;
import {serverTestCompiler, serverTestPlatform,
ServerTestModule} from ‘@angular/platform-browser-dynamic/testing’;
initTestEnvironment(
serverTestCompiler,
serverTestPlatform(),
ServerTestModule);
```
Related to #9726
Closes #9846
2016-07-04 12:37:30 -04:00
|
|
|
|
|
|
|
describe('precompile', () => {
|
|
|
|
let xhrGet: jasmine.Spy;
|
|
|
|
beforeEach(() => {
|
|
|
|
xhrGet = jasmine.createSpy('xhrGet').and.returnValue(Promise.resolve('Hello world!'));
|
|
|
|
configureCompiler({providers: [{provide: XHR, useValue: {get: xhrGet}}]});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should report an error for precompile components with templateUrl and sync tests', () => {
|
|
|
|
var itPromise = patchJasmineIt();
|
|
|
|
|
|
|
|
expect(
|
|
|
|
() => it(
|
|
|
|
'should fail',
|
|
|
|
withModule(() => { return {precompile: [CompWithUrlTemplate]}; })
|
|
|
|
.inject(
|
|
|
|
[ComponentFactoryResolver],
|
|
|
|
(resolver: ComponentFactoryResolver) => {
|
|
|
|
expect(
|
|
|
|
resolver.resolveComponentFactory(CompWithUrlTemplate).componentType)
|
|
|
|
.toBe(CompWithUrlTemplate);
|
|
|
|
})))
|
|
|
|
.toThrowError(
|
|
|
|
`This test module precompiles the component ${stringify(CompWithUrlTemplate)} which is using a "templateUrl", but the test is synchronous. ` +
|
|
|
|
'Please use the "async(...)" or "fakeAsync(...)" helper functions to make the test asynchronous.');
|
|
|
|
|
|
|
|
restoreJasmineIt();
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2015-10-08 18:33:17 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('test component builder', function() {
|
|
|
|
it('should instantiate a component with valid DOM',
|
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 13:59:38 -04:00
|
|
|
async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
2015-10-08 18:33:17 -04: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 13:59:38 -04:00
|
|
|
tcb.createAsync(ChildComp).then((componentFixture) => {
|
2015-10-31 12:50:19 -04:00
|
|
|
componentFixture.detectChanges();
|
2015-10-08 18:33:17 -04:00
|
|
|
|
2015-10-31 12:50:19 -04:00
|
|
|
expect(componentFixture.debugElement.nativeElement).toHaveText('Original Child');
|
2015-10-08 18:33:17 -04: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 13:59:38 -04:00
|
|
|
})));
|
2015-10-08 18:33:17 -04:00
|
|
|
|
|
|
|
it('should allow changing members of the component',
|
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 13:59:38 -04:00
|
|
|
async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
2015-10-08 18:33:17 -04: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 13:59:38 -04:00
|
|
|
tcb.createAsync(MyIfComp).then((componentFixture) => {
|
2015-10-31 12:50:19 -04:00
|
|
|
componentFixture.detectChanges();
|
|
|
|
expect(componentFixture.debugElement.nativeElement).toHaveText('MyIf()');
|
2015-10-08 18:33:17 -04:00
|
|
|
|
2015-10-31 12:50:19 -04:00
|
|
|
componentFixture.debugElement.componentInstance.showMore = true;
|
|
|
|
componentFixture.detectChanges();
|
|
|
|
expect(componentFixture.debugElement.nativeElement).toHaveText('MyIf(More)');
|
2015-10-08 18:33:17 -04: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 13:59:38 -04:00
|
|
|
})));
|
2015-10-08 18:33:17 -04:00
|
|
|
|
2015-12-08 19:44:04 -05:00
|
|
|
it('should override a template',
|
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 13:59:38 -04:00
|
|
|
async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
2015-10-08 18:33:17 -04: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 13:59:38 -04:00
|
|
|
tcb.overrideTemplate(MockChildComp, '<span>Mock</span>')
|
2015-10-08 18:33:17 -04:00
|
|
|
.createAsync(MockChildComp)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((componentFixture) => {
|
|
|
|
componentFixture.detectChanges();
|
|
|
|
expect(componentFixture.debugElement.nativeElement).toHaveText('Mock');
|
2015-10-08 18:33:17 -04: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 13:59:38 -04:00
|
|
|
})));
|
2015-10-08 18:33:17 -04:00
|
|
|
|
2015-12-08 19:44:04 -05:00
|
|
|
it('should override a view',
|
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 13:59:38 -04:00
|
|
|
async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
2015-10-08 18:33:17 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
tcb.overrideView(
|
|
|
|
ChildComp, new ViewMetadata({template: '<span>Modified {{childBinding}}</span>'}))
|
2015-10-08 18:33:17 -04:00
|
|
|
.createAsync(ChildComp)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((componentFixture) => {
|
|
|
|
componentFixture.detectChanges();
|
|
|
|
expect(componentFixture.debugElement.nativeElement).toHaveText('Modified Child');
|
2015-10-08 18:33:17 -04: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 13:59:38 -04:00
|
|
|
})));
|
2015-10-08 18:33:17 -04:00
|
|
|
|
|
|
|
it('should override component dependencies',
|
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 13:59:38 -04:00
|
|
|
async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
2015-10-08 18:33:17 -04: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 13:59:38 -04:00
|
|
|
tcb.overrideDirective(ParentComp, ChildComp, MockChildComp)
|
2015-10-08 18:33:17 -04:00
|
|
|
.createAsync(ParentComp)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((componentFixture) => {
|
|
|
|
componentFixture.detectChanges();
|
|
|
|
expect(componentFixture.debugElement.nativeElement).toHaveText('Parent(Mock)');
|
2015-10-08 18:33:17 -04: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 13:59:38 -04:00
|
|
|
})));
|
2015-10-08 18:33:17 -04:00
|
|
|
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should override child component\'s dependencies',
|
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 13:59:38 -04:00
|
|
|
async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
2015-10-08 18:33:17 -04: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 13:59:38 -04:00
|
|
|
tcb.overrideDirective(ParentComp, ChildComp, ChildWithChildComp)
|
2015-10-08 18:33:17 -04:00
|
|
|
.overrideDirective(ChildWithChildComp, ChildChildComp, MockChildChildComp)
|
|
|
|
.createAsync(ParentComp)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((componentFixture) => {
|
|
|
|
componentFixture.detectChanges();
|
|
|
|
expect(componentFixture.debugElement.nativeElement)
|
2015-10-08 18:33:17 -04:00
|
|
|
.toHaveText('Parent(Original Child(ChildChild Mock))');
|
|
|
|
|
|
|
|
});
|
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 13:59:38 -04:00
|
|
|
})));
|
2015-10-08 18:33:17 -04:00
|
|
|
|
2015-12-08 19:44:04 -05:00
|
|
|
it('should override a provider',
|
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 13:59:38 -04:00
|
|
|
async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
2015-10-08 18:33:17 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
tcb.overrideProviders(
|
|
|
|
TestProvidersComp, [{provide: FancyService, useClass: MockFancyService}])
|
2015-10-08 18:33:17 -04:00
|
|
|
.createAsync(TestProvidersComp)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((componentFixture) => {
|
|
|
|
componentFixture.detectChanges();
|
|
|
|
expect(componentFixture.debugElement.nativeElement)
|
2015-10-08 18:33:17 -04:00
|
|
|
.toHaveText('injected value: mocked out value');
|
|
|
|
});
|
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 13:59:38 -04:00
|
|
|
})));
|
2015-10-08 18:33:17 -04:00
|
|
|
|
|
|
|
|
|
|
|
it('should override a viewProvider',
|
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 13:59:38 -04:00
|
|
|
async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
2015-10-08 18:33:17 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
tcb.overrideViewProviders(
|
|
|
|
TestViewProvidersComp, [{provide: FancyService, useClass: MockFancyService}])
|
2015-10-08 18:33:17 -04:00
|
|
|
.createAsync(TestViewProvidersComp)
|
2015-10-31 12:50:19 -04:00
|
|
|
.then((componentFixture) => {
|
|
|
|
componentFixture.detectChanges();
|
|
|
|
expect(componentFixture.debugElement.nativeElement)
|
2015-10-08 18:33:17 -04:00
|
|
|
.toHaveText('injected value: mocked out value');
|
|
|
|
});
|
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 13:59:38 -04:00
|
|
|
})));
|
2015-10-08 18:33:17 -04:00
|
|
|
});
|
|
|
|
}
|