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
* /
2016-08-17 12:24:44 -04:00
import { CompilerConfig , ResourceLoader } from '@angular/compiler' ;
2019-09-06 12:26:17 -04:00
import { CUSTOM_ELEMENTS_SCHEMA , Compiler , Component , ComponentFactoryResolver , Directive , Inject , Injectable , InjectionToken , Injector , Input , NgModule , Optional , Pipe , SkipSelf , ɵ stringify as stringify } from '@angular/core' ;
2016-12-14 18:05:17 -05:00
import { TestBed , async , fakeAsync , getTestBed , inject , tick , withModule } from '@angular/core/testing' ;
2017-03-02 15:12:46 -05:00
import { expect } from '@angular/platform-browser/testing/src/matchers' ;
2019-02-07 17:29:28 -05:00
import { ivyEnabled , modifiedInIvy , obsoleteInIvy , onlyInIvy } from '@angular/private/testing' ;
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
2015-10-08 18:33:17 -04:00
// Services, and components for the tests.
2016-08-10 14:51:40 -04:00
@Component ( { selector : 'child-comp' , template : ` <span>Original {{childBinding}}</span> ` } )
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>) ` ,
} )
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> ` ,
} )
@Injectable ( )
class ChildWithChildComp {
childBinding : string ;
constructor ( ) { this . childBinding = 'Child' ; }
}
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() {
2016-10-30 15:39:53 -04:00
return new Promise < string > ( ( resolve , reject ) = > setTimeout ( ( ) = > resolve ( 'timeout value' ) , 10 ) ) ;
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
}
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 {
2018-06-18 19:38:33 -04:00
// TODO(issue/24571): remove '!'.
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
@Input ( )
2018-06-18 19:38:33 -04:00
someDir ! : string ;
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
}
@Pipe ( { name : 'somePipe' } )
class SomePipe {
2016-10-30 15:39:53 -04:00
transform ( value : string ) { return ` transformed ${ value } ` ; }
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
}
@Component ( { selector : 'comp' , template : ` <div [someDir]="'someValue' | somePipe"></div> ` } )
class CompUsingModuleDirectiveAndPipe {
}
2016-07-18 06:50:31 -04:00
@NgModule ( )
class SomeLibModule {
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
}
2018-06-04 18:11:40 -04:00
@Component ( {
selector : 'comp' ,
templateUrl : '/base/angular/packages/platform-browser/test/static_assets/test.html'
} )
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
class CompWithUrlTemplate {
}
2019-08-28 19:22:36 -04:00
const aTok = new InjectionToken < string > ( 'a' ) ;
const bTok = new InjectionToken < string > ( 'b' ) ;
2017-12-16 17:42:55 -05:00
{
2016-08-10 14:51:40 -04:00
describe ( 'public testing API' , ( ) = > {
2016-12-31 05:50:03 -05:00
describe ( 'using the async helper with context passing' , ( ) = > {
2019-06-14 06:19:09 -04:00
type TestContext = { actuallyDone : boolean } ;
2015-11-17 15:37:39 -05:00
2019-06-14 06:19:09 -04:00
beforeEach ( function ( this : TestContext ) { this . actuallyDone = false ; } ) ;
2015-11-17 15:37:39 -05:00
2019-06-14 06:19:09 -04:00
afterEach ( function ( this : TestContext ) { expect ( this . actuallyDone ) . toEqual ( true ) ; } ) ;
2015-10-08 18:33:17 -04:00
2019-06-14 06:19:09 -04:00
it ( 'should run normal tests' , function ( this : TestContext ) { this . actuallyDone = true ; } ) ;
it ( 'should run normal async tests' , function ( this : TestContext , done ) {
2016-08-10 14:51:40 -04:00
setTimeout ( ( ) = > {
2016-12-31 05:50:03 -05:00
this . actuallyDone = true ;
2016-08-10 14:51:40 -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
2016-08-10 14:51:40 -04:00
it ( 'should run async tests with tasks' ,
2019-06-14 06:19:09 -04:00
async ( function ( this : TestContext ) { setTimeout ( ( ) = > this . actuallyDone = true , 0 ) ; } ) ) ;
2015-10-08 18:33:17 -04:00
2019-06-14 06:19:09 -04:00
it ( 'should run async tests with promises' , async ( function ( this : TestContext ) {
2016-10-30 15:39:53 -04:00
const p = new Promise ( ( resolve , reject ) = > setTimeout ( resolve , 10 ) ) ;
2016-12-31 05:50:03 -05:00
p . then ( ( ) = > this . actuallyDone = true ) ;
} ) ) ;
} ) ;
describe ( 'basic context passing to inject, fakeAsync and withModule helpers' , ( ) = > {
const module Config = {
providers : [ FancyService ] ,
} ;
2019-06-14 06:19:09 -04:00
type TestContext = { contextModified : boolean } ;
beforeEach ( function ( this : TestContext ) { this . contextModified = false ; } ) ;
2016-12-31 05:50:03 -05:00
2019-06-14 06:19:09 -04:00
afterEach ( function ( this : TestContext ) { expect ( this . contextModified ) . toEqual ( true ) ; } ) ;
2016-12-31 05:50:03 -05:00
it ( 'should pass context to inject helper' ,
2019-06-14 06:19:09 -04:00
inject ( [ ] , function ( this : TestContext ) { this . contextModified = true ; } ) ) ;
2016-12-31 05:50:03 -05:00
it ( 'should pass context to fakeAsync helper' ,
2019-06-14 06:19:09 -04:00
fakeAsync ( function ( this : TestContext ) { this . contextModified = true ; } ) ) ;
2016-12-31 05:50:03 -05:00
it ( 'should pass context to withModule helper - simple' ,
2019-06-14 06:19:09 -04:00
withModule ( module Config , function ( this : TestContext ) { this . contextModified = true ; } ) ) ;
2016-12-31 05:50:03 -05:00
it ( 'should pass context to withModule helper - advanced' ,
2019-06-14 06:19:09 -04:00
withModule ( module Config )
. inject ( [ FancyService ] , function ( this : TestContext , service : FancyService ) {
expect ( service . value ) . toBe ( 'real value' ) ;
this . contextModified = true ;
} ) ) ;
2016-12-31 05:50:03 -05:00
it ( 'should preserve context when async and inject helpers are combined' ,
2019-06-14 06:19:09 -04:00
async ( inject ( [ ] , function ( this : TestContext ) {
setTimeout ( ( ) = > this . contextModified = true , 0 ) ;
} ) ) ) ;
2016-12-31 05:50:03 -05:00
it ( 'should preserve context when fakeAsync and inject helpers are combined' ,
2019-06-14 06:19:09 -04:00
fakeAsync ( inject ( [ ] , function ( this : TestContext ) {
2016-12-31 05:50:03 -05:00
setTimeout ( ( ) = > this . contextModified = true , 0 ) ;
tick ( 1 ) ;
} ) ) ) ;
2016-08-10 14:51:40 -04:00
} ) ;
2015-10-08 18:33:17 -04:00
2016-08-10 14:51:40 -04:00
describe ( 'using the test injector with the inject helper' , ( ) = > {
describe ( 'setting up Providers' , ( ) = > {
beforeEach ( ( ) = > {
TestBed . configureTestingModule (
{ providers : [ { provide : FancyService , useValue : new FancyService ( ) } ] } ) ;
2016-04-26 16:06:50 -04:00
2016-10-30 15:39:53 -04:00
it ( 'should use set up providers' , inject ( [ FancyService ] , ( service : FancyService ) = > {
2016-08-10 14:51:40 -04:00
expect ( service . value ) . toEqual ( 'real value' ) ;
} ) ) ;
2015-10-08 18:33:17 -04:00
2016-08-10 14:51:40 -04:00
it ( 'should wait until returned promises' ,
2016-10-30 15:39:53 -04:00
async ( inject ( [ FancyService ] , ( service : FancyService ) = > {
service . getAsyncValue ( ) . then ( ( value ) = > expect ( value ) . toEqual ( 'async value' ) ) ;
service . getTimeoutValue ( ) . then ( ( value ) = > expect ( value ) . toEqual ( 'timeout value' ) ) ;
2016-08-10 14:51:40 -04:00
} ) ) ) ;
it ( 'should allow the use of fakeAsync' ,
2016-10-30 15:39:53 -04:00
fakeAsync ( inject ( [ FancyService ] , ( service : FancyService ) = > {
2017-03-24 12:59:41 -04:00
let value : string = undefined ! ;
2016-10-30 15:39:53 -04:00
service . getAsyncValue ( ) . then ( ( val ) = > value = val ) ;
2016-08-10 14:51:40 -04:00
tick ( ) ;
expect ( value ) . toEqual ( 'async value' ) ;
} ) ) ) ;
2016-10-30 15:39:53 -04:00
it ( 'should allow use of "done"' , ( done ) = > {
inject ( [ FancyService ] , ( service : FancyService ) = > {
2016-08-10 14:51:40 -04:00
let count = 0 ;
2016-11-12 08:08:58 -05:00
const id = setInterval ( ( ) = > {
2016-08-10 14:51:40 -04:00
count ++ ;
if ( count > 2 ) {
clearInterval ( id ) ;
done ( ) ;
}
} , 5 ) ;
} ) ( ) ; // inject needs to be invoked explicitly with ().
} ) ;
2015-10-08 18:33:17 -04:00
2016-08-10 14:51:40 -04:00
describe ( 'using beforeEach' , ( ) = > {
2016-10-30 15:39:53 -04:00
beforeEach ( inject ( [ FancyService ] , ( service : FancyService ) = > {
2016-08-10 14:51:40 -04:00
service . value = 'value modified in beforeEach' ;
} ) ) ;
2015-10-08 18:33:17 -04:00
2016-10-30 15:39:53 -04:00
it ( 'should use modified providers' , inject ( [ FancyService ] , ( service : FancyService ) = > {
2016-08-10 14:51:40 -04:00
expect ( service . value ) . toEqual ( 'value modified in beforeEach' ) ;
} ) ) ;
} ) ;
2015-12-10 15:00:48 -05:00
2016-08-10 14:51:40 -04:00
describe ( 'using async beforeEach' , ( ) = > {
2016-10-30 15:39:53 -04:00
beforeEach ( async ( inject ( [ FancyService ] , ( service : FancyService ) = > {
service . getAsyncValue ( ) . then ( ( value ) = > service . value = value ) ;
2016-08-10 14:51:40 -04:00
} ) ) ) ;
2016-04-26 16:06:50 -04:00
2016-08-10 14:51:40 -04:00
it ( 'should use asynchronously modified value' ,
2016-10-30 15:39:53 -04:00
inject ( [ FancyService ] , ( service : FancyService ) = > {
2016-08-10 14:51:40 -04:00
expect ( service . value ) . toEqual ( 'async value' ) ;
} ) ) ;
} ) ;
} ) ;
2016-04-26 16:06:50 -04:00
} ) ;
2015-12-10 15:00:48 -05:00
} ) ;
2015-10-08 18:33:17 -04:00
2016-08-10 14:51:40 -04:00
describe ( 'using the test injector with modules' , ( ) = > {
2016-11-12 08:08:58 -05:00
const module Config = {
2016-08-10 14:51:40 -04:00
providers : [ FancyService ] ,
imports : [ SomeLibModule ] ,
declarations : [ SomeDirective , SomePipe , CompUsingModuleDirectiveAndPipe ] ,
} ;
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
2016-08-10 14:51:40 -04:00
describe ( 'setting up a module' , ( ) = > {
beforeEach ( ( ) = > TestBed . configureTestingModule ( module Config ) ) ;
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
2016-08-10 14:51:40 -04:00
it ( 'should use set up providers' , inject ( [ FancyService ] , ( service : FancyService ) = > {
expect ( service . value ) . toEqual ( 'real value' ) ;
} ) ) ;
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
2016-08-10 14:51:40 -04:00
it ( 'should be able to create any declared components' , ( ) = > {
const compFixture = TestBed . createComponent ( CompUsingModuleDirectiveAndPipe ) ;
expect ( compFixture . componentInstance ) . toBeAnInstanceOf ( CompUsingModuleDirectiveAndPipe ) ;
} ) ;
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
2019-01-22 18:02:52 -05:00
it ( 'should use set up directives and pipes' , ( ) = > {
const compFixture = TestBed . createComponent ( CompUsingModuleDirectiveAndPipe ) ;
const el = compFixture . debugElement ;
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
2019-01-22 18:02:52 -05:00
compFixture . detectChanges ( ) ;
expect ( el . children [ 0 ] . properties [ 'title' ] ) . toBe ( 'transformed someValue' ) ;
} ) ;
2016-07-28 07:54:49 -04:00
2016-08-10 14:51:40 -04:00
it ( 'should use set up imported modules' ,
inject ( [ SomeLibModule ] , ( libModule : SomeLibModule ) = > {
expect ( libModule ) . toBeAnInstanceOf ( SomeLibModule ) ;
} ) ) ;
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
2016-08-10 14:51:40 -04:00
describe ( 'provided schemas' , ( ) = > {
@Component ( { template : '<some-element [someUnknownProp]="true"></some-element>' } )
class ComponentUsingInvalidProperty {
}
2016-07-25 06:02:57 -04:00
2016-08-10 14:51:40 -04:00
beforeEach ( ( ) = > {
TestBed . configureTestingModule (
{ schemas : [ CUSTOM_ELEMENTS_SCHEMA ] , declarations : [ ComponentUsingInvalidProperty ] } ) ;
} ) ;
2016-07-25 06:02:57 -04:00
2016-08-10 14:51:40 -04:00
it ( 'should not error on unknown bound properties on custom elements when using the CUSTOM_ELEMENTS_SCHEMA' ,
( ) = > {
expect ( TestBed . createComponent ( ComponentUsingInvalidProperty ) . componentInstance )
. toBeAnInstanceOf ( ComponentUsingInvalidProperty ) ;
} ) ;
} ) ;
2016-07-25 06:02:57 -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
2016-08-10 14:51:40 -04:00
describe ( 'per test modules' , ( ) = > {
it ( 'should use set up providers' ,
withModule ( module Config ) . inject ( [ FancyService ] , ( service : FancyService ) = > {
expect ( service . value ) . toEqual ( 'real value' ) ;
} ) ) ;
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
2019-01-22 18:02:52 -05:00
it ( 'should use set up directives and pipes' , withModule ( module Config , ( ) = > {
const compFixture = TestBed . createComponent ( CompUsingModuleDirectiveAndPipe ) ;
const el = compFixture . debugElement ;
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
2019-01-22 18:02:52 -05:00
compFixture . detectChanges ( ) ;
expect ( el . children [ 0 ] . properties [ 'title' ] ) . toBe ( 'transformed someValue' ) ;
} ) ) ;
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
2016-08-10 14:51:40 -04:00
it ( 'should use set up library modules' ,
withModule ( module Config ) . inject ( [ SomeLibModule ] , ( libModule : SomeLibModule ) = > {
expect ( libModule ) . toBeAnInstanceOf ( SomeLibModule ) ;
} ) ) ;
} ) ;
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
2016-08-10 14:51:40 -04:00
describe ( 'components with template url' , ( ) = > {
beforeEach ( async ( ( ) = > {
TestBed . configureTestingModule ( { declarations : [ CompWithUrlTemplate ] } ) ;
TestBed . compileComponents ( ) ;
} ) ) ;
2016-07-28 07:54:49 -04:00
2018-12-03 20:57:07 -05:00
isBrowser &&
2019-01-14 08:46:21 -05:00
it ( 'should allow to createSync components with templateUrl after explicit async compilation' ,
( ) = > {
const fixture = TestBed . createComponent ( CompWithUrlTemplate ) ;
expect ( fixture . nativeElement ) . toHaveText ( 'from external template' ) ;
} ) ;
2016-08-10 14:51:40 -04:00
} ) ;
2016-07-28 07:54:49 -04:00
2016-08-10 14:51:40 -04:00
describe ( 'overwriting metadata' , ( ) = > {
@Pipe ( { name : 'undefined' } )
class SomePipe {
transform ( value : string ) : string { return ` transformed ${ value } ` ; }
}
2016-07-28 07:54:49 -04:00
2016-08-10 14:51:40 -04:00
@Directive ( { selector : '[undefined]' } )
class SomeDirective {
someProp = 'hello' ;
}
2016-07-28 07:54:49 -04:00
2016-08-10 14:51:40 -04:00
@Component ( { selector : 'comp' , template : 'someText' } )
class SomeComponent {
}
2016-07-28 07:54:49 -04:00
2016-08-10 14:51:40 -04:00
@Component ( { selector : 'comp' , template : 'someOtherText' } )
class SomeOtherComponent {
}
2016-07-28 07:54:49 -04:00
2016-08-10 14:51:40 -04:00
@NgModule ( { declarations : [ SomeComponent , SomeDirective , SomePipe ] } )
class SomeModule {
}
2016-07-28 07:54:49 -04:00
2016-10-30 15:39:53 -04:00
beforeEach ( ( ) = > TestBed . configureTestingModule ( { imports : [ SomeModule ] } ) ) ;
2016-07-28 07:54:49 -04:00
2016-08-10 14:51:40 -04:00
describe ( 'module' , ( ) = > {
beforeEach ( ( ) = > {
TestBed . overrideModule ( SomeModule , { set : { declarations : [ SomeOtherComponent ] } } ) ;
} ) ;
it ( 'should work' , ( ) = > {
expect ( TestBed . createComponent ( SomeOtherComponent ) . componentInstance )
. toBeAnInstanceOf ( SomeOtherComponent ) ;
} ) ;
2016-07-28 07:54:49 -04:00
} ) ;
2016-08-10 14:51:40 -04:00
describe ( 'component' , ( ) = > {
beforeEach ( ( ) = > {
TestBed . overrideComponent (
SomeComponent , { set : { selector : 'comp' , template : 'newText' } } ) ;
} ) ;
it ( 'should work' , ( ) = > {
expect ( TestBed . createComponent ( SomeComponent ) . nativeElement ) . toHaveText ( 'newText' ) ;
} ) ;
2016-07-28 07:54:49 -04:00
} ) ;
2016-08-10 14:51:40 -04:00
describe ( 'directive' , ( ) = > {
beforeEach ( ( ) = > {
TestBed
. overrideComponent (
SomeComponent , { set : { selector : 'comp' , template : ` <div someDir></div> ` } } )
. overrideDirective (
SomeDirective , { set : { selector : '[someDir]' , host : { '[title]' : 'someProp' } } } ) ;
} ) ;
2019-01-22 18:02:52 -05:00
it ( 'should work' , ( ) = > {
const compFixture = TestBed . createComponent ( SomeComponent ) ;
compFixture . detectChanges ( ) ;
expect ( compFixture . debugElement . children [ 0 ] . properties [ 'title' ] ) . toEqual ( 'hello' ) ;
} ) ;
2016-07-28 07:54:49 -04:00
} ) ;
2016-08-10 14:51:40 -04:00
describe ( 'pipe' , ( ) = > {
beforeEach ( ( ) = > {
TestBed
. overrideComponent (
SomeComponent , { set : { selector : 'comp' , template : ` {{'hello' | somePipe}} ` } } )
2018-05-24 09:49:07 -04:00
. overridePipe ( SomePipe , { set : { name : 'somePipe' } } )
. overridePipe ( SomePipe , { add : { pure : false } } ) ;
2016-08-10 14:51:40 -04:00
} ) ;
2018-12-18 13:30:14 -05:00
it ( 'should work' , ( ) = > {
const compFixture = TestBed . createComponent ( SomeComponent ) ;
compFixture . detectChanges ( ) ;
expect ( compFixture . nativeElement ) . toHaveText ( 'transformed hello' ) ;
} ) ;
2016-07-28 07:54:49 -04:00
} ) ;
2016-12-14 18:05:17 -05:00
describe ( 'template' , ( ) = > {
let testBedSpy : any ;
beforeEach ( ( ) = > {
testBedSpy = spyOn ( getTestBed ( ) , 'overrideComponent' ) . and . callThrough ( ) ;
TestBed . overrideTemplate ( SomeComponent , 'newText' ) ;
} ) ;
it ( ` should override component's template ` , ( ) = > {
const fixture = TestBed . createComponent ( SomeComponent ) ;
expect ( fixture . nativeElement ) . toHaveText ( 'newText' ) ;
expect ( testBedSpy ) . toHaveBeenCalledWith ( SomeComponent , {
set : { template : 'newText' , templateUrl : null }
} ) ;
} ) ;
} ) ;
2016-07-28 07:54:49 -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
2017-05-15 16:12:10 -04:00
describe ( 'overriding providers' , ( ) = > {
2019-09-06 12:26:17 -04:00
describe ( 'in core' , ( ) = > {
it ( 'ComponentFactoryResolver' , ( ) = > {
const componentFactoryMock =
jasmine . createSpyObj ( 'componentFactory' , [ 'resolveComponentFactory' ] ) ;
TestBed . overrideProvider ( ComponentFactoryResolver , { useValue : componentFactoryMock } ) ;
expect ( TestBed . get ( ComponentFactoryResolver ) ) . toEqual ( componentFactoryMock ) ;
} ) ;
} ) ;
2017-05-15 16:12:10 -04:00
describe ( 'in NgModules' , ( ) = > {
2019-08-28 19:22:36 -04:00
2017-05-15 16:12:10 -04:00
it ( 'should support useValue' , ( ) = > {
TestBed . configureTestingModule ( {
providers : [
2019-08-28 19:22:36 -04:00
{ provide : aTok , useValue : 'aValue' } ,
2017-05-15 16:12:10 -04:00
]
} ) ;
2019-08-28 19:22:36 -04:00
TestBed . overrideProvider ( aTok , { useValue : 'mockValue' } ) ;
expect ( TestBed . inject ( aTok ) ) . toBe ( 'mockValue' ) ;
2017-05-15 16:12:10 -04:00
} ) ;
it ( 'should support useFactory' , ( ) = > {
TestBed . configureTestingModule ( {
providers : [
{ provide : 'dep' , useValue : 'depValue' } ,
2019-08-28 19:22:36 -04:00
{ provide : aTok , useValue : 'aValue' } ,
2017-05-15 16:12:10 -04:00
]
} ) ;
TestBed . overrideProvider (
2019-08-28 19:22:36 -04:00
aTok , { useFactory : ( dep : any ) = > ` mockA: ${ dep } ` , deps : [ 'dep' ] } ) ;
expect ( TestBed . inject ( aTok ) ) . toBe ( 'mockA: depValue' ) ;
2017-05-15 16:12:10 -04:00
} ) ;
it ( 'should support @Optional without matches' , ( ) = > {
TestBed . configureTestingModule ( {
providers : [
2019-08-28 19:22:36 -04:00
{ provide : aTok , useValue : 'aValue' } ,
2017-05-15 16:12:10 -04:00
]
} ) ;
TestBed . overrideProvider (
2019-08-28 19:22:36 -04:00
aTok , { useFactory : ( dep : any ) = > ` mockA: ${ dep } ` , deps : [ [ new Optional ( ) , 'dep' ] ] } ) ;
expect ( TestBed . inject ( aTok ) ) . toBe ( 'mockA: null' ) ;
2017-05-15 16:12:10 -04:00
} ) ;
it ( 'should support Optional with matches' , ( ) = > {
TestBed . configureTestingModule ( {
providers : [
{ provide : 'dep' , useValue : 'depValue' } ,
2019-08-28 19:22:36 -04:00
{ provide : aTok , useValue : 'aValue' } ,
2017-05-15 16:12:10 -04:00
]
} ) ;
TestBed . overrideProvider (
2019-08-28 19:22:36 -04:00
aTok , { useFactory : ( dep : any ) = > ` mockA: ${ dep } ` , deps : [ [ new Optional ( ) , 'dep' ] ] } ) ;
expect ( TestBed . inject ( aTok ) ) . toBe ( 'mockA: depValue' ) ;
2017-05-15 16:12:10 -04:00
} ) ;
2019-01-12 00:19:55 -05:00
it ( 'should support SkipSelf' , ( ) = > {
@NgModule ( {
providers : [
2019-08-28 19:22:36 -04:00
{ provide : aTok , useValue : 'aValue' } ,
2019-01-12 00:19:55 -05:00
{ provide : 'dep' , useValue : 'depValue' } ,
]
} )
class MyModule {
}
2017-05-15 16:12:10 -04:00
2019-01-12 00:19:55 -05:00
TestBed . overrideProvider (
2019-08-28 19:22:36 -04:00
aTok , { useFactory : ( dep : any ) = > ` mockA: ${ dep } ` , deps : [ [ new SkipSelf ( ) , 'dep' ] ] } ) ;
2019-01-12 00:19:55 -05:00
TestBed . configureTestingModule (
{ providers : [ { provide : 'dep' , useValue : 'parentDepValue' } ] } ) ;
2017-05-15 16:12:10 -04:00
2019-08-28 19:22:36 -04:00
const compiler = TestBed . inject ( Compiler ) ;
2019-01-12 00:19:55 -05:00
const modFactory = compiler . compileModuleSync ( MyModule ) ;
2019-08-28 19:22:36 -04:00
expect ( modFactory . create ( getTestBed ( ) ) . injector . get ( aTok ) )
. toBe ( 'mockA: parentDepValue' ) ;
2019-01-12 00:19:55 -05:00
} ) ;
2017-05-15 16:12:10 -04:00
2017-10-04 12:13:22 -04:00
it ( 'should keep imported NgModules eager' , ( ) = > {
let someModule : SomeModule | undefined ;
@NgModule ( )
class SomeModule {
constructor ( ) { someModule = this ; }
}
TestBed . configureTestingModule ( {
providers : [
2019-08-28 19:22:36 -04:00
{ provide : aTok , useValue : 'aValue' } ,
2017-10-04 12:13:22 -04:00
] ,
imports : [ SomeModule ]
} ) ;
2019-08-28 19:22:36 -04:00
TestBed . overrideProvider ( aTok , { useValue : 'mockValue' } ) ;
2017-10-04 12:13:22 -04:00
2019-08-28 19:22:36 -04:00
expect ( TestBed . inject ( aTok ) ) . toBe ( 'mockValue' ) ;
2017-10-04 12:13:22 -04:00
expect ( someModule ) . toBeAnInstanceOf ( SomeModule ) ;
} ) ;
2017-05-15 16:12:10 -04:00
describe ( 'injecting eager providers into an eager overwritten provider' , ( ) = > {
@NgModule ( {
providers : [
2019-08-28 19:22:36 -04:00
{ provide : aTok , useFactory : ( ) = > 'aValue' } ,
{ provide : bTok , useFactory : ( ) = > 'bValue' } ,
2017-05-15 16:12:10 -04:00
]
} )
class MyModule {
// NgModule is eager, which makes all of its deps eager
2019-08-28 19:22:36 -04:00
constructor ( @Inject ( aTok ) a : any , @Inject ( bTok ) b : any ) { }
2017-05-15 16:12:10 -04:00
}
it ( 'should inject providers that were declared before' , ( ) = > {
TestBed . configureTestingModule ( { imports : [ MyModule ] } ) ;
TestBed . overrideProvider (
2019-08-28 19:22:36 -04:00
bTok , { useFactory : ( a : string ) = > ` mockB: ${ a } ` , deps : [ aTok ] } ) ;
2017-05-15 16:12:10 -04:00
2019-08-28 19:22:36 -04:00
expect ( TestBed . inject ( bTok ) ) . toBe ( 'mockB: aValue' ) ;
2017-05-15 16:12:10 -04:00
} ) ;
it ( 'should inject providers that were declared afterwards' , ( ) = > {
TestBed . configureTestingModule ( { imports : [ MyModule ] } ) ;
TestBed . overrideProvider (
2019-08-28 19:22:36 -04:00
aTok , { useFactory : ( b : string ) = > ` mockA: ${ b } ` , deps : [ bTok ] } ) ;
2017-05-15 16:12:10 -04:00
2019-08-28 19:22:36 -04:00
expect ( TestBed . inject ( aTok ) ) . toBe ( 'mockA: bValue' ) ;
2017-05-15 16:12:10 -04:00
} ) ;
} ) ;
} ) ;
describe ( 'in Components' , ( ) = > {
2018-12-15 17:46:47 -05:00
it ( 'should support useValue' , ( ) = > {
@Component ( {
template : '' ,
providers : [
2019-08-28 19:22:36 -04:00
{ provide : aTok , useValue : 'aValue' } ,
2018-12-15 17:46:47 -05:00
]
} )
class MComp {
}
2017-05-15 16:12:10 -04:00
2019-08-28 19:22:36 -04:00
TestBed . overrideProvider ( aTok , { useValue : 'mockValue' } ) ;
2018-12-15 17:46:47 -05:00
const ctx =
TestBed . configureTestingModule ( { declarations : [ MComp ] } ) . createComponent ( MComp ) ;
2017-05-15 16:12:10 -04:00
2019-08-28 19:22:36 -04:00
expect ( ctx . debugElement . injector . get ( aTok ) ) . toBe ( 'mockValue' ) ;
2018-12-15 17:46:47 -05:00
} ) ;
2017-05-15 16:12:10 -04:00
2018-12-15 17:46:47 -05:00
it ( 'should support useFactory' , ( ) = > {
@Component ( {
template : '' ,
providers : [
{ provide : 'dep' , useValue : 'depValue' } ,
2019-08-28 19:22:36 -04:00
{ provide : aTok , useValue : 'aValue' } ,
2018-12-15 17:46:47 -05:00
]
} )
class MyComp {
}
2017-05-15 16:12:10 -04:00
2018-12-15 17:46:47 -05:00
TestBed . overrideProvider (
2019-08-28 19:22:36 -04:00
aTok , { useFactory : ( dep : any ) = > ` mockA: ${ dep } ` , deps : [ 'dep' ] } ) ;
2018-12-15 17:46:47 -05:00
const ctx =
TestBed . configureTestingModule ( { declarations : [ MyComp ] } ) . createComponent ( MyComp ) ;
2017-05-15 16:12:10 -04:00
2019-08-28 19:22:36 -04:00
expect ( ctx . debugElement . injector . get ( aTok ) ) . toBe ( 'mockA: depValue' ) ;
2018-12-15 17:46:47 -05:00
} ) ;
2017-05-15 16:12:10 -04:00
2018-12-15 17:46:47 -05:00
it ( 'should support @Optional without matches' , ( ) = > {
@Component ( {
template : '' ,
providers : [
2019-08-28 19:22:36 -04:00
{ provide : aTok , useValue : 'aValue' } ,
2018-12-15 17:46:47 -05:00
]
} )
class MyComp {
}
2017-05-15 16:12:10 -04:00
2018-12-15 17:46:47 -05:00
TestBed . overrideProvider (
2019-08-28 19:22:36 -04:00
aTok , { useFactory : ( dep : any ) = > ` mockA: ${ dep } ` , deps : [ [ new Optional ( ) , 'dep' ] ] } ) ;
2018-12-15 17:46:47 -05:00
const ctx =
TestBed . configureTestingModule ( { declarations : [ MyComp ] } ) . createComponent ( MyComp ) ;
2017-05-15 16:12:10 -04:00
2019-08-28 19:22:36 -04:00
expect ( ctx . debugElement . injector . get ( aTok ) ) . toBe ( 'mockA: null' ) ;
2018-12-15 17:46:47 -05:00
} ) ;
2017-05-15 16:12:10 -04:00
2018-12-15 17:46:47 -05:00
it ( 'should support Optional with matches' , ( ) = > {
@Component ( {
template : '' ,
providers : [
{ provide : 'dep' , useValue : 'depValue' } ,
2019-08-28 19:22:36 -04:00
{ provide : aTok , useValue : 'aValue' } ,
2018-12-15 17:46:47 -05:00
]
} )
class MyComp {
}
2017-05-15 16:12:10 -04:00
2018-12-15 17:46:47 -05:00
TestBed . overrideProvider (
2019-08-28 19:22:36 -04:00
aTok , { useFactory : ( dep : any ) = > ` mockA: ${ dep } ` , deps : [ [ new Optional ( ) , 'dep' ] ] } ) ;
2018-12-15 17:46:47 -05:00
const ctx =
TestBed . configureTestingModule ( { declarations : [ MyComp ] } ) . createComponent ( MyComp ) ;
2017-05-15 16:12:10 -04:00
2019-08-28 19:22:36 -04:00
expect ( ctx . debugElement . injector . get ( aTok ) ) . toBe ( 'mockA: depValue' ) ;
2018-12-15 17:46:47 -05:00
} ) ;
2017-05-15 16:12:10 -04:00
2018-12-15 17:46:47 -05:00
it ( 'should support SkipSelf' , ( ) = > {
@Directive ( {
selector : '[myDir]' ,
providers : [
2019-08-28 19:22:36 -04:00
{ provide : aTok , useValue : 'aValue' } ,
2018-12-15 17:46:47 -05:00
{ provide : 'dep' , useValue : 'depValue' } ,
]
} )
class MyDir {
}
2017-05-15 16:12:10 -04:00
2018-12-15 17:46:47 -05:00
@Component ( {
template : '<div myDir></div>' ,
providers : [
{ provide : 'dep' , useValue : 'parentDepValue' } ,
]
} )
class MyComp {
}
2017-05-15 16:12:10 -04:00
2018-12-15 17:46:47 -05:00
TestBed . overrideProvider (
2019-08-28 19:22:36 -04:00
aTok , { useFactory : ( dep : any ) = > ` mockA: ${ dep } ` , deps : [ [ new SkipSelf ( ) , 'dep' ] ] } ) ;
2018-12-15 17:46:47 -05:00
const ctx = TestBed . configureTestingModule ( { declarations : [ MyComp , MyDir ] } )
. createComponent ( MyComp ) ;
2019-08-28 19:22:36 -04:00
expect ( ctx . debugElement . children [ 0 ] . injector . get ( aTok ) ) . toBe ( 'mockA: parentDepValue' ) ;
2018-12-15 17:46:47 -05:00
} ) ;
2017-05-15 16:12:10 -04:00
2018-12-15 17:46:47 -05:00
it ( 'should support multiple providers in a template' , ( ) = > {
@Directive ( {
selector : '[myDir1]' ,
providers : [
2019-08-28 19:22:36 -04:00
{ provide : aTok , useValue : 'aValue1' } ,
2018-12-15 17:46:47 -05:00
]
} )
class MyDir1 {
}
2017-05-15 16:12:10 -04:00
2018-12-15 17:46:47 -05:00
@Directive ( {
selector : '[myDir2]' ,
providers : [
2019-08-28 19:22:36 -04:00
{ provide : aTok , useValue : 'aValue2' } ,
2018-12-15 17:46:47 -05:00
]
} )
class MyDir2 {
}
2017-05-15 16:12:10 -04:00
2018-12-15 17:46:47 -05:00
@Component ( {
template : '<div myDir1></div><div myDir2></div>' ,
} )
class MyComp {
}
2017-05-15 16:12:10 -04:00
2019-08-28 19:22:36 -04:00
TestBed . overrideProvider ( aTok , { useValue : 'mockA' } ) ;
2018-12-15 17:46:47 -05:00
const ctx = TestBed . configureTestingModule ( { declarations : [ MyComp , MyDir1 , MyDir2 ] } )
. createComponent ( MyComp ) ;
2019-08-28 19:22:36 -04:00
expect ( ctx . debugElement . children [ 0 ] . injector . get ( aTok ) ) . toBe ( 'mockA' ) ;
expect ( ctx . debugElement . children [ 1 ] . injector . get ( aTok ) ) . toBe ( 'mockA' ) ;
2018-12-15 17:46:47 -05:00
} ) ;
2017-05-15 16:12:10 -04:00
describe ( 'injecting eager providers into an eager overwritten provider' , ( ) = > {
@Component ( {
template : '' ,
providers : [
2019-08-28 19:22:36 -04:00
{ provide : aTok , useFactory : ( ) = > 'aValue' } ,
{ provide : bTok , useFactory : ( ) = > 'bValue' } ,
2017-05-15 16:12:10 -04:00
]
} )
class MyComp {
// Component is eager, which makes all of its deps eager
2019-08-28 19:22:36 -04:00
constructor ( @Inject ( aTok ) a : any , @Inject ( bTok ) b : any ) { }
2017-05-15 16:12:10 -04:00
}
2018-12-15 17:46:47 -05:00
it ( 'should inject providers that were declared before it' , ( ) = > {
TestBed . overrideProvider (
2019-08-28 19:22:36 -04:00
bTok , { useFactory : ( a : string ) = > ` mockB: ${ a } ` , deps : [ aTok ] } ) ;
2018-12-15 17:46:47 -05:00
const ctx =
TestBed . configureTestingModule ( { declarations : [ MyComp ] } ) . createComponent ( MyComp ) ;
2018-12-04 09:13:10 -05:00
2019-08-28 19:22:36 -04:00
expect ( ctx . debugElement . injector . get ( bTok ) ) . toBe ( 'mockB: aValue' ) ;
2018-12-15 17:46:47 -05:00
} ) ;
2018-12-04 09:13:10 -05:00
2018-12-15 17:46:47 -05:00
it ( 'should inject providers that were declared after it' , ( ) = > {
TestBed . overrideProvider (
2019-08-28 19:22:36 -04:00
aTok , { useFactory : ( b : string ) = > ` mockA: ${ b } ` , deps : [ bTok ] } ) ;
2018-12-15 17:46:47 -05:00
const ctx =
TestBed . configureTestingModule ( { declarations : [ MyComp ] } ) . createComponent ( MyComp ) ;
2018-12-04 09:13:10 -05:00
2019-08-28 19:22:36 -04:00
expect ( ctx . debugElement . injector . get ( aTok ) ) . toBe ( 'mockA: bValue' ) ;
2018-12-15 17:46:47 -05:00
} ) ;
2018-12-04 11:51:34 -05:00
} ) ;
2017-05-15 16:12:10 -04:00
} ) ;
it ( 'should reset overrides when the testing modules is resetted' , ( ) = > {
2019-08-28 19:22:36 -04:00
TestBed . overrideProvider ( aTok , { useValue : 'mockValue' } ) ;
2017-05-15 16:12:10 -04:00
TestBed . resetTestingModule ( ) ;
2019-08-28 19:22:36 -04:00
TestBed . configureTestingModule ( { providers : [ { provide : aTok , useValue : 'aValue' } ] } ) ;
expect ( TestBed . inject ( aTok ) ) . toBe ( 'aValue' ) ;
2017-05-15 16:12:10 -04:00
} ) ;
} ) ;
2017-10-27 20:04:11 -04:00
describe ( 'overrideTemplateUsingTestingModule' , ( ) = > {
2018-12-17 17:47:51 -05:00
it ( 'should compile the template in the context of the testing module' , ( ) = > {
@Component ( { selector : 'comp' , template : 'a' } )
class MyComponent {
prop = 'some prop' ;
}
2017-10-27 20:04:11 -04:00
2018-12-17 17:47:51 -05:00
let testDir : TestDir | undefined ;
2017-10-27 20:04:11 -04:00
2018-12-17 17:47:51 -05:00
@Directive ( { selector : '[test]' } )
class TestDir {
constructor ( ) { testDir = this ; }
2017-10-27 20:04:11 -04:00
2018-12-17 17:47:51 -05:00
// TODO(issue/24571): remove '!'.
@Input ( 'test' )
test ! : string ;
}
2017-10-27 20:04:11 -04:00
2018-12-17 17:47:51 -05:00
TestBed . overrideTemplateUsingTestingModule (
MyComponent , '<div [test]="prop">Hello world!</div>' ) ;
2017-10-27 20:04:11 -04:00
2018-12-17 17:47:51 -05:00
const fixture = TestBed . configureTestingModule ( { declarations : [ MyComponent , TestDir ] } )
. createComponent ( MyComponent ) ;
fixture . detectChanges ( ) ;
expect ( fixture . nativeElement ) . toHaveText ( 'Hello world!' ) ;
expect ( testDir ) . toBeAnInstanceOf ( TestDir ) ;
expect ( testDir ! . test ) . toBe ( 'some prop' ) ;
} ) ;
2017-10-27 20:04:11 -04:00
2018-12-17 17:47:51 -05:00
it ( 'should throw if the TestBed is already created' , ( ) = > {
@Component ( { selector : 'comp' , template : 'a' } )
class MyComponent {
}
2019-08-28 19:22:36 -04:00
TestBed . inject ( Injector ) ;
2018-12-17 17:47:51 -05:00
expect ( ( ) = > TestBed . overrideTemplateUsingTestingModule ( MyComponent , 'b' ) )
. toThrowError (
/Cannot override template when the test module has already been instantiated/ ) ;
} ) ;
it ( 'should reset overrides when the testing module is resetted' , ( ) = > {
@Component ( { selector : 'comp' , template : 'a' } )
class MyComponent {
}
TestBed . overrideTemplateUsingTestingModule ( MyComponent , 'b' ) ;
const fixture = TestBed . resetTestingModule ( )
. configureTestingModule ( { declarations : [ MyComponent ] } )
. createComponent ( MyComponent ) ;
expect ( fixture . nativeElement ) . toHaveText ( 'a' ) ;
} ) ;
2017-10-27 20:04:11 -04:00
} ) ;
2016-08-10 14:51:40 -04:00
describe ( 'setting up the compiler' , ( ) = > {
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
2016-08-10 14:51:40 -04:00
describe ( 'providers' , ( ) = > {
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
2019-01-14 08:46:21 -05:00
it ( 'should use set up providers' , fakeAsync ( ( ) = > {
2019-03-20 20:58:20 -04:00
// Keeping this component inside the test is needed to make sure it's not resolved
2019-10-10 17:57:15 -04:00
// prior to this test, thus having ɵcmp and a reference in resource
2019-03-20 20:58:20 -04:00
// resolution queue. This is done to check external resoution logic in isolation by
// configuring TestBed with the necessary ResourceLoader instance.
@Component ( {
selector : 'comp' ,
templateUrl : '/base/angular/packages/platform-browser/test/static_assets/test.html'
} )
class InternalCompWithUrlTemplate {
}
const resourceLoaderGet = jasmine . createSpy ( 'resourceLoaderGet' )
. and . returnValue ( Promise . resolve ( 'Hello world!' ) ) ;
TestBed . configureTestingModule ( { declarations : [ InternalCompWithUrlTemplate ] } ) ;
TestBed . configureCompiler (
{ providers : [ { provide : ResourceLoader , useValue : { get : resourceLoaderGet } } ] } ) ;
2019-01-14 08:46:21 -05:00
TestBed . compileComponents ( ) ;
tick ( ) ;
2019-03-20 20:58:20 -04:00
const compFixture = TestBed . createComponent ( InternalCompWithUrlTemplate ) ;
2019-01-14 08:46:21 -05:00
expect ( compFixture . nativeElement ) . toHaveText ( 'Hello world!' ) ;
} ) ) ;
2016-08-10 14:51:40 -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
2016-08-10 14:51:40 -04:00
describe ( 'useJit true' , ( ) = > {
2016-10-30 15:39:53 -04:00
beforeEach ( ( ) = > TestBed . configureCompiler ( { useJit : true } ) ) ;
2018-12-03 20:57:07 -05:00
obsoleteInIvy ( 'the Render3 compiler JiT mode is not configurable' )
. it ( 'should set the value into CompilerConfig' ,
inject ( [ CompilerConfig ] , ( config : CompilerConfig ) = > {
expect ( config . useJit ) . toBe ( true ) ;
} ) ) ;
2016-08-10 14:51:40 -04:00
} ) ;
describe ( 'useJit false' , ( ) = > {
2016-10-30 15:39:53 -04:00
beforeEach ( ( ) = > TestBed . configureCompiler ( { useJit : false } ) ) ;
2018-12-03 20:57:07 -05:00
obsoleteInIvy ( 'the Render3 compiler JiT mode is not configurable' )
. it ( 'should set the value into CompilerConfig' ,
inject ( [ CompilerConfig ] , ( config : CompilerConfig ) = > {
expect ( config . useJit ) . toBe ( false ) ;
} ) ) ;
2016-08-10 14:51:40 -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
} ) ;
} ) ;
2016-08-10 14:51:40 -04:00
describe ( 'errors' , ( ) = > {
2016-10-30 15:39:53 -04:00
let originalJasmineIt : ( description : string , func : ( ) = > void ) = > jasmine . Spec ;
2017-10-24 07:54:08 -04:00
let originalJasmineBeforeEach : ( beforeEachFunction : ( done : DoneFn ) = > void ) = > void ;
2015-11-18 21:46:24 -05:00
2016-10-30 15:39:53 -04:00
const patchJasmineIt = ( ) = > {
let resolve : ( result : any ) = > void ;
let reject : ( error : any ) = > void ;
const promise = new Promise ( ( res , rej ) = > {
2016-08-10 14:51:40 -04:00
resolve = res ;
reject = rej ;
} ) ;
originalJasmineIt = jasmine . getEnv ( ) . it ;
2017-03-24 12:59:41 -04:00
jasmine . getEnv ( ) . it = ( description : string , fn : ( done : DoneFn ) = > void ) : any = > {
2016-10-30 15:39:53 -04:00
const done = < DoneFn > ( ( ) = > resolve ( null ) ) ;
done . fail = ( err ) = > reject ( err ) ;
2016-08-10 14:51:40 -04:00
fn ( done ) ;
return null ;
} ;
return promise ;
2015-11-18 21:46:24 -05:00
} ;
2015-10-08 18:33:17 -04:00
2016-10-30 15:39:53 -04:00
const restoreJasmineIt = ( ) = > jasmine . getEnv ( ) . it = originalJasmineIt ;
2015-10-08 18:33:17 -04:00
2016-10-30 15:39:53 -04:00
const patchJasmineBeforeEach = ( ) = > {
let resolve : ( result : any ) = > void ;
let reject : ( error : any ) = > void ;
const promise = new Promise ( ( res , rej ) = > {
2016-08-10 14:51:40 -04:00
resolve = res ;
reject = rej ;
} ) ;
originalJasmineBeforeEach = jasmine . getEnv ( ) . beforeEach ;
2016-10-30 15:39:53 -04:00
jasmine . getEnv ( ) . beforeEach = ( fn : ( done : DoneFn ) = > void ) = > {
const done = < DoneFn > ( ( ) = > resolve ( null ) ) ;
done . fail = ( err ) = > reject ( err ) ;
2016-08-10 14:51:40 -04:00
fn ( done ) ;
} ;
return promise ;
2015-12-08 19:44:04 -05:00
} ;
2015-10-08 18:33:17 -04:00
2016-10-30 15:39:53 -04:00
const restoreJasmineBeforeEach = ( ) = > jasmine . getEnv ( ) . beforeEach =
originalJasmineBeforeEach ;
2015-10-08 18:33:17 -04:00
2016-10-30 15:39:53 -04:00
it ( 'should fail when an asynchronous error is thrown' , ( done ) = > {
const itPromise = patchJasmineIt ( ) ;
const barError = new Error ( 'bar' ) ;
2015-11-18 21:46:24 -05:00
2016-08-10 14:51:40 -04:00
it ( 'throws an async error' ,
2016-10-30 15:39:53 -04:00
async ( inject ( [ ] , ( ) = > setTimeout ( ( ) = > { throw barError ; } , 0 ) ) ) ) ;
itPromise . then ( ( ) = > done . fail ( 'Expected test to fail, but it did not' ) , ( err ) = > {
expect ( err ) . toEqual ( barError ) ;
done ( ) ;
} ) ;
2016-08-10 14:51:40 -04:00
restoreJasmineIt ( ) ;
} ) ;
2015-11-18 21:46:24 -05:00
2016-10-30 15:39:53 -04:00
it ( 'should fail when a returned promise is rejected' , ( done ) = > {
const itPromise = patchJasmineIt ( ) ;
2015-10-08 18:33:17 -04:00
2016-08-10 14:51:40 -04:00
it ( 'should fail with an error from a promise' , async ( inject ( [ ] , ( ) = > {
2017-03-24 12:59:41 -04:00
let reject : ( error : any ) = > void = undefined ! ;
2016-10-30 15:39:53 -04:00
const promise = new Promise ( ( _ , rej ) = > reject = rej ) ;
const p = promise . then ( ( ) = > expect ( 1 ) . toEqual ( 2 ) ) ;
2015-10-08 18:33:17 -04:00
2016-08-10 14:51:40 -04:00
reject ( 'baz' ) ;
return p ;
} ) ) ) ;
2015-10-08 18:33:17 -04:00
2016-10-30 15:39:53 -04:00
itPromise . then ( ( ) = > done . fail ( 'Expected test to fail, but it did not' ) , ( err ) = > {
expect ( err . message ) . toEqual ( 'Uncaught (in promise): baz' ) ;
done ( ) ;
} ) ;
2016-08-10 14:51:40 -04:00
restoreJasmineIt ( ) ;
} ) ;
2015-10-08 18:33:17 -04:00
2016-08-10 14:51:40 -04:00
describe ( 'components' , ( ) = > {
2016-08-17 12:24:44 -04:00
let resourceLoaderGet : jasmine.Spy ;
2016-08-10 14:51:40 -04:00
beforeEach ( ( ) = > {
2016-08-17 12:24:44 -04:00
resourceLoaderGet = jasmine . createSpy ( 'resourceLoaderGet' )
. and . returnValue ( Promise . resolve ( 'Hello world!' ) ) ;
TestBed . configureCompiler (
{ providers : [ { provide : ResourceLoader , useValue : { get : resourceLoaderGet } } ] } ) ;
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
2019-01-14 08:46:21 -05:00
it ( 'should report an error for declared components with templateUrl which never call TestBed.compileComponents' ,
( ) = > {
const itPromise = patchJasmineIt ( ) ;
2019-03-11 13:35:25 -04:00
@Component ( {
selector : 'comp' ,
templateUrl : '/base/angular/packages/platform-browser/test/static_assets/test.html'
} )
class InlineCompWithUrlTemplate {
}
2019-01-14 08:46:21 -05:00
expect (
2019-03-11 13:35:25 -04:00
( ) = > it (
'should fail' , withModule (
{ declarations : [ InlineCompWithUrlTemplate ] } ,
( ) = > TestBed . createComponent ( InlineCompWithUrlTemplate ) ) ) )
2019-01-14 08:46:21 -05:00
. toThrowError (
ivyEnabled ?
2019-03-11 13:35:25 -04:00
` Component 'InlineCompWithUrlTemplate' is not resolved:
2019-01-14 08:46:21 -05:00
- templateUrl : / b a s e / a n g u l a r / p a c k a g e s / p l a t f o r m - b r o w s e r / t e s t / s t a t i c _ a s s e t s / t e s t . h t m l
Did you run and wait for 'resolveComponentResources()' ? ` :
2019-03-11 13:35:25 -04:00
` This test module uses the component ${ stringify ( InlineCompWithUrlTemplate ) } which is using a "templateUrl" or "styleUrls", but they were never compiled. ` +
2019-01-14 08:46:21 -05:00
` Please call "TestBed.compileComponents" before your test. ` ) ;
restoreJasmineIt ( ) ;
} ) ;
2016-08-10 14:51:40 -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
} ) ;
2018-12-04 11:51:34 -05:00
2019-09-04 15:48:04 -04:00
modifiedInIvy ( ` Unknown property error thrown instead of logging a warning ` )
2018-12-03 20:57:07 -05:00
. it ( 'should error on unknown bound properties on custom elements by default' , ( ) = > {
2018-12-04 09:13:10 -05:00
@Component ( { template : '<some-element [someUnknownProp]="true"></some-element>' } )
class ComponentUsingInvalidProperty {
}
2016-07-20 13:51:21 -04:00
2018-12-04 09:13:10 -05:00
const itPromise = patchJasmineIt ( ) ;
2016-07-20 13:51:21 -04:00
2018-12-04 09:13:10 -05:00
expect (
( ) = >
it ( 'should fail' ,
withModule (
{ declarations : [ ComponentUsingInvalidProperty ] } ,
( ) = > TestBed . createComponent ( ComponentUsingInvalidProperty ) ) ) )
. toThrowError ( /Can't bind to 'someUnknownProp'/ ) ;
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
2018-12-04 09:13:10 -05:00
restoreJasmineIt ( ) ;
} ) ;
2019-02-07 17:29:28 -05:00
2019-09-04 15:48:04 -04:00
onlyInIvy ( ` Unknown property warning logged instead of an error ` )
2019-02-07 17:29:28 -05:00
. it ( 'should error on unknown bound properties on custom elements by default' , ( ) = > {
@Component ( { template : '<some-element [someUnknownProp]="true"></some-element>' } )
class ComponentUsingInvalidProperty {
}
2019-09-04 15:48:04 -04:00
const spy = spyOn ( console , 'warn' ) ;
withModule ( { declarations : [ ComponentUsingInvalidProperty ] } , ( ) = > {
const fixture = TestBed . createComponent ( ComponentUsingInvalidProperty ) ;
fixture . detectChanges ( ) ;
} ) ( ) ;
expect ( spy . calls . mostRecent ( ) . args [ 0 ] ) . toMatch ( /Can't bind to 'someUnknownProp'/ ) ;
2019-02-07 17:29:28 -05: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
} ) ;
2016-07-28 07:54:49 -04:00
2016-10-30 15:39:53 -04:00
describe ( 'creating components' , ( ) = > {
2016-07-28 07:54:49 -04:00
2016-08-10 14:51:40 -04:00
beforeEach ( ( ) = > {
TestBed . configureTestingModule ( {
declarations : [
ChildComp ,
MyIfComp ,
ChildChildComp ,
ParentComp ,
TestProvidersComp ,
TestViewProvidersComp ,
]
} ) ;
} ) ;
2015-10-08 18:33:17 -04:00
2016-08-10 14:51:40 -04:00
it ( 'should instantiate a component with valid DOM' , async ( ( ) = > {
2016-10-30 15:39:53 -04:00
const fixture = TestBed . createComponent ( ChildComp ) ;
2016-08-10 14:51:40 -04:00
fixture . detectChanges ( ) ;
2015-10-08 18:33:17 -04:00
2016-09-09 15:04:38 -04:00
expect ( fixture . nativeElement ) . toHaveText ( 'Original Child' ) ;
2016-08-10 14:51:40 -04:00
} ) ) ;
2015-10-08 18:33:17 -04:00
2016-08-10 14:51:40 -04:00
it ( 'should allow changing members of the component' , async ( ( ) = > {
2016-10-30 15:39:53 -04:00
const componentFixture = TestBed . createComponent ( MyIfComp ) ;
2015-10-31 12:50:19 -04:00
componentFixture . detectChanges ( ) ;
2016-09-09 15:04:38 -04:00
expect ( componentFixture . nativeElement ) . toHaveText ( 'MyIf()' ) ;
2015-10-08 18:33:17 -04:00
2016-09-09 15:04:38 -04:00
componentFixture . componentInstance . showMore = true ;
2015-10-31 12:50:19 -04:00
componentFixture . detectChanges ( ) ;
2016-09-09 15:04:38 -04:00
expect ( componentFixture . nativeElement ) . toHaveText ( 'MyIf(More)' ) ;
2016-08-10 14:51:40 -04:00
} ) ) ;
2015-10-08 18:33:17 -04:00
2016-08-10 14:51:40 -04:00
it ( 'should override a template' , async ( ( ) = > {
TestBed . overrideComponent ( ChildComp , { set : { template : '<span>Mock</span>' } } ) ;
2016-11-12 08:08:58 -05:00
const componentFixture = TestBed . createComponent ( ChildComp ) ;
2016-08-10 14:51:40 -04:00
componentFixture . detectChanges ( ) ;
2016-09-09 15:04:38 -04:00
expect ( componentFixture . nativeElement ) . toHaveText ( 'Mock' ) ;
2015-10-08 18:33:17 -04:00
2016-08-10 14:51:40 -04:00
} ) ) ;
2015-10-08 18:33:17 -04:00
2016-08-10 14:51:40 -04:00
it ( 'should override a provider' , async ( ( ) = > {
TestBed . overrideComponent (
TestProvidersComp ,
{ set : { providers : [ { provide : FancyService , useClass : MockFancyService } ] } } ) ;
2016-10-30 15:39:53 -04:00
const componentFixture = TestBed . createComponent ( TestProvidersComp ) ;
2016-08-10 14:51:40 -04:00
componentFixture . detectChanges ( ) ;
2016-09-09 15:04:38 -04:00
expect ( componentFixture . nativeElement ) . toHaveText ( 'injected value: mocked out value' ) ;
2016-08-10 14:51:40 -04:00
} ) ) ;
2015-10-08 18:33:17 -04:00
2016-08-10 14:51:40 -04:00
it ( 'should override a viewProvider' , async ( ( ) = > {
TestBed . overrideComponent (
TestViewProvidersComp ,
{ set : { viewProviders : [ { provide : FancyService , useClass : MockFancyService } ] } } ) ;
2015-10-08 18:33:17 -04:00
2016-10-30 15:39:53 -04:00
const componentFixture = TestBed . createComponent ( TestViewProvidersComp ) ;
2016-08-10 14:51:40 -04:00
componentFixture . detectChanges ( ) ;
2016-09-09 15:04:38 -04:00
expect ( componentFixture . nativeElement ) . toHaveText ( 'injected value: mocked out value' ) ;
2016-08-10 14:51:40 -04:00
} ) ) ;
} ) ;
describe ( 'using alternate components' , ( ) = > {
beforeEach ( ( ) = > {
TestBed . configureTestingModule ( {
declarations : [
MockChildComp ,
ParentComp ,
]
} ) ;
} ) ;
2015-10-08 18:33:17 -04:00
2016-08-10 14:51:40 -04:00
it ( 'should override component dependencies' , async ( ( ) = > {
2016-11-12 08:08:58 -05:00
const componentFixture = TestBed . createComponent ( ParentComp ) ;
2016-08-10 14:51:40 -04:00
componentFixture . detectChanges ( ) ;
2016-09-09 15:04:38 -04:00
expect ( componentFixture . nativeElement ) . toHaveText ( 'Parent(Mock)' ) ;
2016-08-10 14:51:40 -04:00
} ) ) ;
} ) ;
2015-10-08 18:33:17 -04:00
} ) ;
}