2016-06-23 16:47:54 +00:00
|
|
|
/**
|
|
|
|
|
* @license
|
2020-05-19 19:08:49 +00:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-06-23 16:47:54 +00:00
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
2024-09-20 15:23:15 +00:00
|
|
|
* found in the LICENSE file at https://angular.dev/license
|
2016-06-23 16:47:54 +00:00
|
|
|
*/
|
|
|
|
|
|
2021-11-09 02:20:56 +00:00
|
|
|
import {ResourceLoader} from '@angular/compiler';
|
2023-02-27 18:36:39 +00:00
|
|
|
import {
|
|
|
|
|
Compiler,
|
|
|
|
|
Component,
|
|
|
|
|
ComponentFactoryResolver,
|
|
|
|
|
CUSTOM_ELEMENTS_SCHEMA,
|
|
|
|
|
Directive,
|
|
|
|
|
Inject,
|
|
|
|
|
Injectable,
|
|
|
|
|
InjectionToken,
|
|
|
|
|
Injector,
|
|
|
|
|
Input,
|
|
|
|
|
NgModule,
|
|
|
|
|
Optional,
|
|
|
|
|
Pipe,
|
2024-04-23 08:17:45 +00:00
|
|
|
TransferState,
|
2023-02-27 18:36:39 +00:00
|
|
|
SkipSelf,
|
|
|
|
|
Type,
|
|
|
|
|
} from '@angular/core';
|
2020-07-31 19:43:18 +00:00
|
|
|
import {
|
|
|
|
|
fakeAsync,
|
|
|
|
|
getTestBed,
|
|
|
|
|
inject,
|
|
|
|
|
TestBed,
|
|
|
|
|
tick,
|
|
|
|
|
waitForAsync,
|
|
|
|
|
withModule,
|
|
|
|
|
} from '@angular/core/testing';
|
2025-05-19 10:54:54 +00:00
|
|
|
import {expect} from '@angular/private/testing/matchers';
|
|
|
|
|
import {isBrowser} 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 16:37:30 +00:00
|
|
|
|
2015-10-08 22:33:17 +00:00
|
|
|
// Services, and components for the tests.
|
|
|
|
|
|
2024-10-11 10:32:52 +00:00
|
|
|
@Component({
|
|
|
|
|
selector: 'child-comp',
|
|
|
|
|
template: `<span>Original {{ childBinding }}</span>`,
|
|
|
|
|
standalone: false,
|
|
|
|
|
})
|
2015-10-08 22:33:17 +00:00
|
|
|
@Injectable()
|
|
|
|
|
class ChildComp {
|
|
|
|
|
childBinding: string;
|
2020-04-13 23:40:21 +00:00
|
|
|
constructor() {
|
|
|
|
|
this.childBinding = 'Child';
|
|
|
|
|
}
|
2015-10-08 22:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-11 10:32:52 +00:00
|
|
|
@Component({
|
|
|
|
|
selector: 'child-comp',
|
|
|
|
|
template: `<span>Mock</span>`,
|
|
|
|
|
standalone: false,
|
|
|
|
|
})
|
2015-10-08 22:33:17 +00:00
|
|
|
@Injectable()
|
|
|
|
|
class MockChildComp {}
|
|
|
|
|
|
2016-03-08 21:36:48 +00:00
|
|
|
@Component({
|
|
|
|
|
selector: 'parent-comp',
|
|
|
|
|
template: `Parent(<child-comp></child-comp>)`,
|
2024-10-11 10:32:52 +00:00
|
|
|
standalone: false,
|
2016-03-08 21:36:48 +00:00
|
|
|
})
|
2015-10-08 22:33:17 +00:00
|
|
|
@Injectable()
|
|
|
|
|
class ParentComp {}
|
|
|
|
|
|
2024-10-11 10:32:52 +00:00
|
|
|
@Component({
|
|
|
|
|
selector: 'my-if-comp',
|
|
|
|
|
template: `MyIf(<span *ngIf="showMore">More</span>)`,
|
|
|
|
|
standalone: false,
|
|
|
|
|
})
|
2015-10-08 22:33:17 +00:00
|
|
|
@Injectable()
|
|
|
|
|
class MyIfComp {
|
|
|
|
|
showMore: boolean = false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-11 10:32:52 +00:00
|
|
|
@Component({
|
|
|
|
|
selector: 'child-child-comp',
|
|
|
|
|
template: `<span>ChildChild</span>`,
|
|
|
|
|
standalone: false,
|
|
|
|
|
})
|
2015-10-08 22:33:17 +00:00
|
|
|
@Injectable()
|
|
|
|
|
class ChildChildComp {}
|
|
|
|
|
|
|
|
|
|
class FancyService {
|
|
|
|
|
value: string = 'real value';
|
2020-04-13 23:40:21 +00:00
|
|
|
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 17:59:38 +00:00
|
|
|
getTimeoutValue() {
|
2016-10-30 19:39:53 +00: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 17:59:38 +00:00
|
|
|
}
|
2015-10-08 22:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class MockFancyService extends FancyService {
|
2021-06-07 19:02:10 +00:00
|
|
|
override value: string = 'mocked out value';
|
2015-10-08 22:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-08 21:36:48 +00:00
|
|
|
@Component({
|
|
|
|
|
selector: 'my-service-comp',
|
|
|
|
|
providers: [FancyService],
|
|
|
|
|
template: `injected value: {{ fancyService.value }}`,
|
2024-10-11 10:32:52 +00:00
|
|
|
standalone: false,
|
2016-03-08 21:36:48 +00:00
|
|
|
})
|
2015-10-08 22:33:17 +00:00
|
|
|
class TestProvidersComp {
|
|
|
|
|
constructor(private fancyService: FancyService) {}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-08 21:36:48 +00:00
|
|
|
@Component({
|
|
|
|
|
selector: 'my-service-comp',
|
|
|
|
|
viewProviders: [FancyService],
|
|
|
|
|
template: `injected value: {{ fancyService.value }}`,
|
2024-10-11 10:32:52 +00:00
|
|
|
standalone: false,
|
2016-03-08 21:36:48 +00:00
|
|
|
})
|
2015-10-08 22:33:17 +00:00
|
|
|
class TestViewProvidersComp {
|
|
|
|
|
constructor(private fancyService: FancyService) {}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-11 10:32:52 +00:00
|
|
|
@Directive({
|
|
|
|
|
selector: '[someDir]',
|
|
|
|
|
host: {'[title]': 'someDir'},
|
|
|
|
|
standalone: false,
|
|
|
|
|
})
|
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 16:37:30 +00:00
|
|
|
class SomeDirective {
|
2020-04-13 23:40:21 +00:00
|
|
|
@Input() 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 16:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-11 10:32:52 +00:00
|
|
|
@Pipe({
|
|
|
|
|
name: 'somePipe',
|
|
|
|
|
standalone: false,
|
|
|
|
|
})
|
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 16:37:30 +00:00
|
|
|
class SomePipe {
|
2020-04-13 23:40:21 +00: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 16:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-11 10:32:52 +00:00
|
|
|
@Component({
|
|
|
|
|
selector: 'comp',
|
|
|
|
|
template: `<div [someDir]="'someValue' | somePipe"></div>`,
|
|
|
|
|
standalone: false,
|
|
|
|
|
})
|
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 16:37:30 +00:00
|
|
|
class CompUsingModuleDirectiveAndPipe {}
|
|
|
|
|
|
2016-07-18 10:50:31 +00: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 16:37:30 +00:00
|
|
|
|
2019-08-28 23:22:36 +00:00
|
|
|
const aTok = new InjectionToken<string>('a');
|
|
|
|
|
const bTok = new InjectionToken<string>('b');
|
|
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
describe('public testing API', () => {
|
|
|
|
|
describe('using the async helper with context passing', () => {
|
|
|
|
|
type TestContext = {actuallyDone: boolean};
|
2015-11-17 20:37:39 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
beforeEach(function (this: TestContext) {
|
|
|
|
|
this.actuallyDone = false;
|
|
|
|
|
});
|
2015-11-17 20:37:39 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
afterEach(function (this: TestContext) {
|
|
|
|
|
expect(this.actuallyDone).toEqual(true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should run normal tests', function (this: TestContext) {
|
|
|
|
|
this.actuallyDone = true;
|
|
|
|
|
});
|
2015-10-08 22:33:17 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should run normal async tests', function (this: TestContext, done) {
|
|
|
|
|
setTimeout(() => {
|
2020-04-13 23:40:21 +00:00
|
|
|
this.actuallyDone = true;
|
2023-10-17 09:45:16 +00:00
|
|
|
done();
|
|
|
|
|
}, 0);
|
|
|
|
|
});
|
2019-06-14 10:19:09 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should run async tests with tasks', waitForAsync(function (this: TestContext) {
|
|
|
|
|
setTimeout(() => (this.actuallyDone = true), 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 17:59:38 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should run async tests with promises', waitForAsync(function (this: TestContext) {
|
|
|
|
|
const p = new Promise((resolve, reject) => setTimeout(resolve, 10));
|
|
|
|
|
p.then(() => (this.actuallyDone = true));
|
|
|
|
|
}));
|
|
|
|
|
});
|
2015-10-08 22:33:17 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
describe('basic context passing to inject, fakeAsync and withModule helpers', () => {
|
|
|
|
|
const moduleConfig = {
|
|
|
|
|
providers: [FancyService],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type TestContext = {contextModified: boolean};
|
|
|
|
|
|
|
|
|
|
beforeEach(function (this: TestContext) {
|
|
|
|
|
this.contextModified = false;
|
2016-12-31 10:50:03 +00:00
|
|
|
});
|
|
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
afterEach(function (this: TestContext) {
|
|
|
|
|
expect(this.contextModified).toEqual(true);
|
|
|
|
|
});
|
2016-12-31 10:50:03 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should pass context to inject helper', inject([], function (this: TestContext) {
|
|
|
|
|
this.contextModified = true;
|
|
|
|
|
}));
|
2024-04-19 16:13:59 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should pass context to fakeAsync helper', fakeAsync(function (this: TestContext) {
|
|
|
|
|
this.contextModified = true;
|
|
|
|
|
}));
|
2024-04-19 16:13:59 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it(
|
|
|
|
|
'should pass context to withModule helper - simple',
|
|
|
|
|
withModule(moduleConfig, function (this: TestContext) {
|
|
|
|
|
this.contextModified = true;
|
|
|
|
|
}),
|
|
|
|
|
);
|
2024-04-19 16:13:59 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it(
|
|
|
|
|
'should pass context to withModule helper - advanced',
|
|
|
|
|
withModule(moduleConfig).inject(
|
|
|
|
|
[FancyService],
|
|
|
|
|
function (this: TestContext, service: FancyService) {
|
|
|
|
|
expect(service.value).toBe('real value');
|
|
|
|
|
this.contextModified = true;
|
|
|
|
|
},
|
2024-04-19 16:13:59 +00:00
|
|
|
),
|
2023-10-17 09:45:16 +00:00
|
|
|
);
|
2024-04-19 16:13:59 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should preserve context when async and inject helpers are combined', waitForAsync(
|
|
|
|
|
inject([], function (this: TestContext) {
|
|
|
|
|
setTimeout(() => (this.contextModified = true), 0);
|
|
|
|
|
}),
|
|
|
|
|
));
|
2024-04-19 16:13:59 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should preserve context when fakeAsync and inject helpers are combined', fakeAsync(
|
|
|
|
|
inject([], function (this: TestContext) {
|
|
|
|
|
setTimeout(() => (this.contextModified = true), 0);
|
|
|
|
|
tick(1);
|
|
|
|
|
}),
|
|
|
|
|
));
|
|
|
|
|
});
|
2016-12-31 10:50:03 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
describe('using the test injector with the inject helper', () => {
|
|
|
|
|
describe('setting up Providers', () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
TestBed.configureTestingModule({
|
|
|
|
|
providers: [{provide: FancyService, useValue: new FancyService()}],
|
|
|
|
|
});
|
|
|
|
|
});
|
2016-12-31 10:50:03 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should use set up providers', inject([FancyService], (service: FancyService) => {
|
|
|
|
|
expect(service.value).toEqual('real value');
|
|
|
|
|
}));
|
2024-04-19 16:13:59 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should wait until returned promises', waitForAsync(
|
|
|
|
|
inject([FancyService], (service: FancyService) => {
|
|
|
|
|
service.getAsyncValue().then((value) => expect(value).toEqual('async value'));
|
|
|
|
|
service.getTimeoutValue().then((value) => expect(value).toEqual('timeout value'));
|
2016-12-31 10:50:03 +00:00
|
|
|
}),
|
|
|
|
|
));
|
2024-04-19 16:13:59 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should allow the use of fakeAsync', fakeAsync(
|
|
|
|
|
inject([FancyService], (service: FancyService) => {
|
|
|
|
|
let value: string = undefined!;
|
|
|
|
|
service.getAsyncValue().then((val) => (value = val));
|
|
|
|
|
tick();
|
|
|
|
|
expect(value).toEqual('async value');
|
|
|
|
|
}),
|
|
|
|
|
));
|
2022-04-08 19:37:30 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should allow use of "done"', (done) => {
|
|
|
|
|
inject([FancyService], (service: FancyService) => {
|
|
|
|
|
let count = 0;
|
|
|
|
|
const id = setInterval(() => {
|
|
|
|
|
count++;
|
|
|
|
|
if (count > 2) {
|
|
|
|
|
clearInterval(id);
|
|
|
|
|
done();
|
|
|
|
|
}
|
|
|
|
|
}, 5);
|
|
|
|
|
})(); // inject needs to be invoked explicitly with ().
|
|
|
|
|
});
|
2022-04-08 19:37:30 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
describe('using beforeEach', () => {
|
|
|
|
|
beforeEach(inject([FancyService], (service: FancyService) => {
|
|
|
|
|
service.value = 'value modified in beforeEach';
|
|
|
|
|
}));
|
2016-04-26 20:06:50 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should use modified providers', inject([FancyService], (service: FancyService) => {
|
|
|
|
|
expect(service.value).toEqual('value modified in beforeEach');
|
|
|
|
|
}));
|
|
|
|
|
});
|
2022-04-08 19:37:30 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
describe('using async beforeEach', () => {
|
|
|
|
|
beforeEach(waitForAsync(
|
|
|
|
|
inject([FancyService], (service: FancyService) => {
|
|
|
|
|
service.getAsyncValue().then((value) => (service.value = value));
|
|
|
|
|
}),
|
|
|
|
|
));
|
2024-04-19 16:13:59 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should use asynchronously modified value', inject(
|
|
|
|
|
[FancyService],
|
|
|
|
|
(service: FancyService) => {
|
|
|
|
|
expect(service.value).toEqual('async value');
|
|
|
|
|
},
|
|
|
|
|
));
|
2016-04-26 20:06:50 +00:00
|
|
|
});
|
2015-12-10 20:00:48 +00:00
|
|
|
});
|
2023-10-17 09:45:16 +00:00
|
|
|
});
|
2015-10-08 22:33:17 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
describe('using the test injector with modules', () => {
|
|
|
|
|
const moduleConfig = {
|
|
|
|
|
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 16:37:30 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
describe('setting up a module', () => {
|
|
|
|
|
beforeEach(() => TestBed.configureTestingModule(moduleConfig));
|
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 16:37:30 +00:00
|
|
|
|
2023-10-17 09:45:16 +00: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 16:37:30 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should be able to create any declared components', () => {
|
|
|
|
|
const compFixture = TestBed.createComponent(CompUsingModuleDirectiveAndPipe);
|
|
|
|
|
expect(compFixture.componentInstance).toBeInstanceOf(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 16:37:30 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should use set up directives and pipes', () => {
|
|
|
|
|
const compFixture = TestBed.createComponent(CompUsingModuleDirectiveAndPipe);
|
|
|
|
|
const el = compFixture.debugElement;
|
2016-07-28 11:54:49 +00:00
|
|
|
|
2023-10-17 09:45:16 +00: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 16:37:30 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should use set up imported modules', inject(
|
|
|
|
|
[SomeLibModule],
|
|
|
|
|
(libModule: SomeLibModule) => {
|
|
|
|
|
expect(libModule).toBeInstanceOf(SomeLibModule);
|
|
|
|
|
},
|
|
|
|
|
));
|
2016-07-25 10:02:57 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
describe('provided schemas', () => {
|
2024-10-11 10:32:52 +00:00
|
|
|
@Component({
|
|
|
|
|
template: '<some-element [someUnknownProp]="true"></some-element>',
|
|
|
|
|
standalone: false,
|
|
|
|
|
})
|
2023-10-17 09:45:16 +00:00
|
|
|
class ComponentUsingInvalidProperty {}
|
2016-07-25 10:02:57 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
|
TestBed.configureTestingModule({
|
|
|
|
|
schemas: [CUSTOM_ELEMENTS_SCHEMA],
|
|
|
|
|
declarations: [ComponentUsingInvalidProperty],
|
|
|
|
|
});
|
2016-08-10 18:51:40 +00:00
|
|
|
});
|
2023-10-17 09:45:16 +00:00
|
|
|
|
|
|
|
|
it('should not error on unknown bound properties on custom elements when using the CUSTOM_ELEMENTS_SCHEMA', () => {
|
|
|
|
|
expect(
|
|
|
|
|
TestBed.createComponent(ComponentUsingInvalidProperty).componentInstance,
|
|
|
|
|
).toBeInstanceOf(ComponentUsingInvalidProperty);
|
|
|
|
|
});
|
2016-07-25 10:02:57 +00:00
|
|
|
});
|
2023-10-17 09:45:16 +00: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 16:37:30 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
describe('per test modules', () => {
|
|
|
|
|
it(
|
|
|
|
|
'should use set up providers',
|
|
|
|
|
withModule(moduleConfig).inject([FancyService], (service: FancyService) => {
|
|
|
|
|
expect(service.value).toEqual('real value');
|
|
|
|
|
}),
|
|
|
|
|
);
|
2024-04-19 16:13:59 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it(
|
|
|
|
|
'should use set up directives and pipes',
|
|
|
|
|
withModule(moduleConfig, () => {
|
|
|
|
|
const compFixture = TestBed.createComponent(CompUsingModuleDirectiveAndPipe);
|
|
|
|
|
const el = compFixture.debugElement;
|
2024-04-19 16:13:59 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
compFixture.detectChanges();
|
|
|
|
|
expect(el.children[0].properties['title']).toBe('transformed someValue');
|
|
|
|
|
}),
|
|
|
|
|
);
|
2024-04-19 16:13:59 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it(
|
|
|
|
|
'should use set up library modules',
|
|
|
|
|
withModule(moduleConfig).inject([SomeLibModule], (libModule: SomeLibModule) => {
|
|
|
|
|
expect(libModule).toBeInstanceOf(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 16:37:30 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
xdescribe('components with template url', () => {
|
|
|
|
|
let TestComponent!: Type<unknown>;
|
2021-11-09 19:02:39 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
beforeEach(waitForAsync(async () => {
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'comp',
|
|
|
|
|
templateUrl: '/base/angular/packages/platform-browser/test/static_assets/test.html',
|
2024-10-11 10:32:52 +00:00
|
|
|
standalone: false,
|
2023-10-17 09:45:16 +00:00
|
|
|
})
|
|
|
|
|
class CompWithUrlTemplate {}
|
2021-11-09 19:02:39 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
TestComponent = CompWithUrlTemplate;
|
2021-11-09 19:02:39 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
TestBed.configureTestingModule({declarations: [CompWithUrlTemplate]});
|
|
|
|
|
await TestBed.compileComponents();
|
|
|
|
|
}));
|
2016-07-28 11:54:49 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
isBrowser &&
|
|
|
|
|
it('should allow to createSync components with templateUrl after explicit async compilation', () => {
|
|
|
|
|
const fixture = TestBed.createComponent(TestComponent);
|
|
|
|
|
expect(fixture.nativeElement).toHaveText('from external template');
|
|
|
|
|
});
|
2024-04-19 16:13:59 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should always pass to satisfy jasmine always wanting an `it` block under a `describe`', () => {});
|
|
|
|
|
});
|
2016-07-28 11:54:49 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
describe('overwriting metadata', () => {
|
2024-10-11 10:32:52 +00:00
|
|
|
@Pipe({
|
|
|
|
|
name: 'undefined',
|
|
|
|
|
standalone: false,
|
|
|
|
|
})
|
2023-10-17 09:45:16 +00:00
|
|
|
class SomePipe {
|
|
|
|
|
transform(value: string): string {
|
|
|
|
|
return `transformed ${value}`;
|
2016-08-10 18:51:40 +00:00
|
|
|
}
|
2023-10-17 09:45:16 +00:00
|
|
|
}
|
2016-07-28 11:54:49 +00:00
|
|
|
|
2024-10-11 10:32:52 +00:00
|
|
|
@Directive({
|
|
|
|
|
selector: '[undefined]',
|
|
|
|
|
standalone: false,
|
|
|
|
|
})
|
2023-10-17 09:45:16 +00:00
|
|
|
class SomeDirective {
|
|
|
|
|
someProp = 'hello';
|
|
|
|
|
}
|
2016-07-28 11:54:49 +00:00
|
|
|
|
2024-10-11 10:32:52 +00:00
|
|
|
@Component({
|
|
|
|
|
selector: 'comp',
|
|
|
|
|
template: 'someText',
|
|
|
|
|
standalone: false,
|
|
|
|
|
})
|
2023-10-17 09:45:16 +00:00
|
|
|
class SomeComponent {}
|
2016-07-28 11:54:49 +00:00
|
|
|
|
2024-10-11 10:32:52 +00:00
|
|
|
@Component({
|
|
|
|
|
selector: 'comp',
|
|
|
|
|
template: 'someOtherText',
|
|
|
|
|
standalone: false,
|
|
|
|
|
})
|
2023-10-17 09:45:16 +00:00
|
|
|
class SomeOtherComponent {}
|
2016-07-28 11:54:49 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
@NgModule({declarations: [SomeComponent, SomeDirective, SomePipe]})
|
|
|
|
|
class SomeModule {}
|
2016-07-28 11:54:49 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
beforeEach(() => TestBed.configureTestingModule({imports: [SomeModule]}));
|
2016-07-28 11:54:49 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
describe('module', () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
TestBed.overrideModule(SomeModule, {set: {declarations: [SomeOtherComponent]}});
|
2016-07-28 11:54:49 +00:00
|
|
|
});
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should work', () => {
|
|
|
|
|
expect(TestBed.createComponent(SomeOtherComponent).componentInstance).toBeInstanceOf(
|
|
|
|
|
SomeOtherComponent,
|
|
|
|
|
);
|
2016-07-28 11:54:49 +00:00
|
|
|
});
|
2023-10-17 09:45:16 +00:00
|
|
|
});
|
2016-07-28 11:54:49 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
describe('component', () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
TestBed.overrideComponent(SomeComponent, {set: {selector: 'comp', template: 'newText'}});
|
2016-07-28 11:54:49 +00:00
|
|
|
});
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should work', () => {
|
|
|
|
|
expect(TestBed.createComponent(SomeComponent).nativeElement).toHaveText('newText');
|
2016-07-28 11:54:49 +00:00
|
|
|
});
|
2023-10-17 09:45:16 +00:00
|
|
|
});
|
2016-12-14 23:05:17 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
describe('directive', () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
TestBed.overrideComponent(SomeComponent, {
|
|
|
|
|
set: {selector: 'comp', template: `<div someDir></div>`},
|
|
|
|
|
}).overrideDirective(SomeDirective, {
|
|
|
|
|
set: {selector: '[someDir]', host: {'[title]': 'someProp'}},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
it('should work', () => {
|
|
|
|
|
const compFixture = TestBed.createComponent(SomeComponent);
|
|
|
|
|
compFixture.detectChanges();
|
|
|
|
|
expect(compFixture.debugElement.children[0].properties['title']).toEqual('hello');
|
2016-12-14 23:05:17 +00:00
|
|
|
});
|
2016-07-28 11:54:49 +00: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 16:37:30 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
describe('pipe', () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
TestBed.overrideComponent(SomeComponent, {
|
|
|
|
|
set: {selector: 'comp', template: `{{'hello' | somePipe}}`},
|
|
|
|
|
})
|
|
|
|
|
.overridePipe(SomePipe, {set: {name: 'somePipe'}})
|
|
|
|
|
.overridePipe(SomePipe, {add: {pure: false}});
|
|
|
|
|
});
|
|
|
|
|
it('should work', () => {
|
|
|
|
|
const compFixture = TestBed.createComponent(SomeComponent);
|
|
|
|
|
compFixture.detectChanges();
|
|
|
|
|
expect(compFixture.nativeElement).toHaveText('transformed hello');
|
2019-09-06 16:26:17 +00:00
|
|
|
});
|
2023-10-17 09:45:16 +00:00
|
|
|
});
|
2019-09-06 16:26:17 +00:00
|
|
|
|
2023-10-17 09:45:16 +00: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},
|
2017-05-15 20:12:10 +00:00
|
|
|
});
|
2023-10-17 09:45:16 +00:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2017-05-15 20:12:10 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
describe('overriding providers', () => {
|
|
|
|
|
describe('in core', () => {
|
|
|
|
|
it('ComponentFactoryResolver', () => {
|
|
|
|
|
const componentFactoryMock = jasmine.createSpyObj('componentFactory', [
|
|
|
|
|
'resolveComponentFactory',
|
|
|
|
|
]);
|
|
|
|
|
TestBed.overrideProvider(ComponentFactoryResolver, {useValue: componentFactoryMock});
|
2025-03-17 08:32:46 +00:00
|
|
|
expect(TestBed.inject(ComponentFactoryResolver)).toEqual(componentFactoryMock);
|
2023-10-17 09:45:16 +00:00
|
|
|
});
|
|
|
|
|
});
|
2017-05-15 20:12:10 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
describe('in NgModules', () => {
|
|
|
|
|
it('should support useValue', () => {
|
|
|
|
|
TestBed.configureTestingModule({
|
|
|
|
|
providers: [{provide: aTok, useValue: 'aValue'}],
|
2017-05-15 20:12:10 +00:00
|
|
|
});
|
2023-10-17 09:45:16 +00:00
|
|
|
TestBed.overrideProvider(aTok, {useValue: 'mockValue'});
|
|
|
|
|
expect(TestBed.inject(aTok)).toBe('mockValue');
|
|
|
|
|
});
|
2017-05-15 20:12:10 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should support useFactory', () => {
|
|
|
|
|
TestBed.configureTestingModule({
|
|
|
|
|
providers: [
|
|
|
|
|
{provide: 'dep', useValue: 'depValue'},
|
|
|
|
|
{provide: aTok, useValue: 'aValue'},
|
|
|
|
|
],
|
2024-04-19 16:13:59 +00:00
|
|
|
});
|
2023-10-17 09:45:16 +00:00
|
|
|
TestBed.overrideProvider(aTok, {
|
|
|
|
|
useFactory: (dep: any) => `mockA: ${dep}`,
|
|
|
|
|
deps: ['dep'],
|
2017-05-15 20:12:10 +00:00
|
|
|
});
|
2023-10-17 09:45:16 +00:00
|
|
|
expect(TestBed.inject(aTok)).toBe('mockA: depValue');
|
|
|
|
|
});
|
2017-05-15 20:12:10 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should support @Optional without matches', () => {
|
|
|
|
|
TestBed.configureTestingModule({
|
|
|
|
|
providers: [{provide: aTok, useValue: 'aValue'}],
|
2024-04-19 16:13:59 +00:00
|
|
|
});
|
2023-10-17 09:45:16 +00:00
|
|
|
TestBed.overrideProvider(aTok, {
|
|
|
|
|
useFactory: (dep: any) => `mockA: ${dep}`,
|
|
|
|
|
deps: [[new Optional(), 'dep']],
|
2019-01-12 05:19:55 +00:00
|
|
|
});
|
2023-10-17 09:45:16 +00:00
|
|
|
expect(TestBed.inject(aTok)).toBe('mockA: null');
|
|
|
|
|
});
|
2017-05-15 20:12:10 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should support Optional with matches', () => {
|
|
|
|
|
TestBed.configureTestingModule({
|
|
|
|
|
providers: [
|
|
|
|
|
{provide: 'dep', useValue: 'depValue'},
|
|
|
|
|
{provide: aTok, useValue: 'aValue'},
|
|
|
|
|
],
|
2024-04-19 16:13:59 +00:00
|
|
|
});
|
2023-10-17 09:45:16 +00:00
|
|
|
TestBed.overrideProvider(aTok, {
|
|
|
|
|
useFactory: (dep: any) => `mockA: ${dep}`,
|
|
|
|
|
deps: [[new Optional(), 'dep']],
|
2017-10-04 16:13:22 +00:00
|
|
|
});
|
2023-10-17 09:45:16 +00:00
|
|
|
expect(TestBed.inject(aTok)).toBe('mockA: depValue');
|
|
|
|
|
});
|
2017-10-04 16:13:22 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should support SkipSelf', () => {
|
|
|
|
|
@NgModule({
|
|
|
|
|
providers: [
|
|
|
|
|
{provide: aTok, useValue: 'aValue'},
|
|
|
|
|
{provide: 'dep', useValue: 'depValue'},
|
|
|
|
|
],
|
|
|
|
|
})
|
|
|
|
|
class MyModule {}
|
2017-05-15 20:12:10 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
TestBed.overrideProvider(aTok, {
|
|
|
|
|
useFactory: (dep: any) => `mockA: ${dep}`,
|
|
|
|
|
deps: [[new SkipSelf(), 'dep']],
|
|
|
|
|
});
|
|
|
|
|
TestBed.configureTestingModule({
|
|
|
|
|
providers: [{provide: 'dep', useValue: 'parentDepValue'}],
|
|
|
|
|
});
|
2017-05-15 20:12:10 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
const compiler = TestBed.inject(Compiler);
|
|
|
|
|
const modFactory = compiler.compileModuleSync(MyModule);
|
2025-03-17 08:32:46 +00:00
|
|
|
expect(modFactory.create(TestBed.inject(Injector)).injector.get(aTok)).toBe(
|
|
|
|
|
'mockA: parentDepValue',
|
|
|
|
|
);
|
2017-05-15 20:12:10 +00:00
|
|
|
});
|
|
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should keep imported NgModules eager', () => {
|
|
|
|
|
let someModule: SomeModule | undefined;
|
2017-05-15 20:12:10 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
@NgModule()
|
|
|
|
|
class SomeModule {
|
|
|
|
|
constructor() {
|
|
|
|
|
someModule = this;
|
2018-12-15 22:46:47 +00:00
|
|
|
}
|
2023-10-17 09:45:16 +00:00
|
|
|
}
|
2017-05-15 20:12:10 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
TestBed.configureTestingModule({
|
|
|
|
|
providers: [{provide: aTok, useValue: 'aValue'}],
|
|
|
|
|
imports: [SomeModule],
|
2018-12-15 22:46:47 +00:00
|
|
|
});
|
2023-10-17 09:45:16 +00:00
|
|
|
TestBed.overrideProvider(aTok, {useValue: 'mockValue'});
|
2017-05-15 20:12:10 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
expect(TestBed.inject(aTok)).toBe('mockValue');
|
|
|
|
|
expect(someModule).toBeInstanceOf(SomeModule);
|
|
|
|
|
});
|
2017-05-15 20:12:10 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
describe('injecting eager providers into an eager overwritten provider', () => {
|
|
|
|
|
@NgModule({
|
|
|
|
|
providers: [
|
|
|
|
|
{provide: aTok, useFactory: () => 'aValue'},
|
|
|
|
|
{provide: bTok, useFactory: () => 'bValue'},
|
|
|
|
|
],
|
|
|
|
|
})
|
|
|
|
|
class MyModule {
|
|
|
|
|
// NgModule is eager, which makes all of its deps eager
|
|
|
|
|
constructor(@Inject(aTok) a: any, @Inject(bTok) b: any) {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
it('should inject providers that were declared before', () => {
|
|
|
|
|
TestBed.configureTestingModule({imports: [MyModule]});
|
2018-12-15 22:46:47 +00:00
|
|
|
TestBed.overrideProvider(bTok, {
|
2023-10-17 09:45:16 +00:00
|
|
|
useFactory: (a: string) => `mockB: ${a}`,
|
|
|
|
|
deps: [aTok],
|
|
|
|
|
});
|
2017-05-15 20:12:10 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
expect(TestBed.inject(bTok)).toBe('mockB: aValue');
|
2018-12-15 22:46:47 +00:00
|
|
|
});
|
2017-05-15 20:12:10 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should inject providers that were declared afterwards', () => {
|
|
|
|
|
TestBed.configureTestingModule({imports: [MyModule]});
|
2018-12-15 22:46:47 +00:00
|
|
|
TestBed.overrideProvider(aTok, {
|
2023-10-17 09:45:16 +00:00
|
|
|
useFactory: (b: string) => `mockA: ${b}`,
|
|
|
|
|
deps: [bTok],
|
|
|
|
|
});
|
2017-05-15 20:12:10 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
expect(TestBed.inject(aTok)).toBe('mockA: bValue');
|
2018-12-15 22:46:47 +00:00
|
|
|
});
|
2023-10-17 09:45:16 +00:00
|
|
|
});
|
|
|
|
|
});
|
2017-05-15 20:12:10 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
describe('in Components', () => {
|
|
|
|
|
it('should support useValue', () => {
|
|
|
|
|
@Component({
|
|
|
|
|
template: '',
|
|
|
|
|
providers: [{provide: aTok, useValue: 'aValue'}],
|
2024-10-11 10:32:52 +00:00
|
|
|
standalone: false,
|
2023-10-17 09:45:16 +00:00
|
|
|
})
|
|
|
|
|
class MComp {}
|
2017-05-15 20:12:10 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
TestBed.overrideProvider(aTok, {useValue: 'mockValue'});
|
|
|
|
|
const ctx = TestBed.configureTestingModule({declarations: [MComp]}).createComponent(
|
|
|
|
|
MComp,
|
|
|
|
|
);
|
2017-05-15 20:12:10 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
expect(ctx.debugElement.injector.get(aTok)).toBe('mockValue');
|
|
|
|
|
});
|
2017-05-15 20:12:10 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should support useFactory', () => {
|
|
|
|
|
@Component({
|
|
|
|
|
template: '',
|
|
|
|
|
providers: [
|
|
|
|
|
{provide: 'dep', useValue: 'depValue'},
|
|
|
|
|
{provide: aTok, useValue: 'aValue'},
|
|
|
|
|
],
|
2024-10-11 10:32:52 +00:00
|
|
|
standalone: false,
|
2023-10-17 09:45:16 +00:00
|
|
|
})
|
|
|
|
|
class MyComp {}
|
2017-05-15 20:12:10 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
TestBed.overrideProvider(aTok, {
|
|
|
|
|
useFactory: (dep: any) => `mockA: ${dep}`,
|
|
|
|
|
deps: ['dep'],
|
|
|
|
|
});
|
|
|
|
|
const ctx = TestBed.configureTestingModule({declarations: [MyComp]}).createComponent(
|
|
|
|
|
MyComp,
|
|
|
|
|
);
|
2017-05-15 20:12:10 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
expect(ctx.debugElement.injector.get(aTok)).toBe('mockA: depValue');
|
|
|
|
|
});
|
2017-05-15 20:12:10 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should support @Optional without matches', () => {
|
|
|
|
|
@Component({
|
|
|
|
|
template: '',
|
|
|
|
|
providers: [{provide: aTok, useValue: 'aValue'}],
|
2024-10-11 10:32:52 +00:00
|
|
|
standalone: false,
|
2023-10-17 09:45:16 +00:00
|
|
|
})
|
|
|
|
|
class MyComp {}
|
2017-05-15 20:12:10 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
TestBed.overrideProvider(aTok, {
|
|
|
|
|
useFactory: (dep: any) => `mockA: ${dep}`,
|
|
|
|
|
deps: [[new Optional(), 'dep']],
|
|
|
|
|
});
|
|
|
|
|
const ctx = TestBed.configureTestingModule({declarations: [MyComp]}).createComponent(
|
|
|
|
|
MyComp,
|
|
|
|
|
);
|
2017-05-15 20:12:10 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
expect(ctx.debugElement.injector.get(aTok)).toBe('mockA: null');
|
|
|
|
|
});
|
2018-12-04 14:13:10 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should support Optional with matches', () => {
|
|
|
|
|
@Component({
|
|
|
|
|
template: '',
|
|
|
|
|
providers: [
|
|
|
|
|
{provide: 'dep', useValue: 'depValue'},
|
|
|
|
|
{provide: aTok, useValue: 'aValue'},
|
|
|
|
|
],
|
2024-10-11 10:32:52 +00:00
|
|
|
standalone: false,
|
2023-10-17 09:45:16 +00:00
|
|
|
})
|
|
|
|
|
class MyComp {}
|
2018-12-04 14:13:10 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
TestBed.overrideProvider(aTok, {
|
|
|
|
|
useFactory: (dep: any) => `mockA: ${dep}`,
|
|
|
|
|
deps: [[new Optional(), 'dep']],
|
|
|
|
|
});
|
|
|
|
|
const ctx = TestBed.configureTestingModule({declarations: [MyComp]}).createComponent(
|
|
|
|
|
MyComp,
|
|
|
|
|
);
|
2018-12-04 14:13:10 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
expect(ctx.debugElement.injector.get(aTok)).toBe('mockA: depValue');
|
2017-05-15 20:12:10 +00:00
|
|
|
});
|
|
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should support SkipSelf', () => {
|
|
|
|
|
@Directive({
|
|
|
|
|
selector: '[myDir]',
|
|
|
|
|
providers: [
|
|
|
|
|
{provide: aTok, useValue: 'aValue'},
|
|
|
|
|
{provide: 'dep', useValue: 'depValue'},
|
|
|
|
|
],
|
2024-10-11 10:32:52 +00:00
|
|
|
standalone: false,
|
2023-10-17 09:45:16 +00:00
|
|
|
})
|
|
|
|
|
class MyDir {}
|
2017-05-15 20:12:10 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
@Component({
|
|
|
|
|
template: '<div myDir></div>',
|
|
|
|
|
providers: [{provide: 'dep', useValue: 'parentDepValue'}],
|
2024-10-11 10:32:52 +00:00
|
|
|
standalone: false,
|
2023-10-17 09:45:16 +00:00
|
|
|
})
|
|
|
|
|
class MyComp {}
|
2017-10-28 00:04:11 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
TestBed.overrideProvider(aTok, {
|
|
|
|
|
useFactory: (dep: any) => `mockA: ${dep}`,
|
|
|
|
|
deps: [[new SkipSelf(), 'dep']],
|
|
|
|
|
});
|
|
|
|
|
const ctx = TestBed.configureTestingModule({
|
|
|
|
|
declarations: [MyComp, MyDir],
|
|
|
|
|
}).createComponent(MyComp);
|
|
|
|
|
expect(ctx.debugElement.children[0].injector.get(aTok)).toBe('mockA: parentDepValue');
|
|
|
|
|
});
|
2017-10-28 00:04:11 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should support multiple providers in a template', () => {
|
|
|
|
|
@Directive({
|
|
|
|
|
selector: '[myDir1]',
|
|
|
|
|
providers: [{provide: aTok, useValue: 'aValue1'}],
|
2024-10-11 10:32:52 +00:00
|
|
|
standalone: false,
|
2023-10-17 09:45:16 +00:00
|
|
|
})
|
|
|
|
|
class MyDir1 {}
|
2017-10-28 00:04:11 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
@Directive({
|
|
|
|
|
selector: '[myDir2]',
|
|
|
|
|
providers: [{provide: aTok, useValue: 'aValue2'}],
|
2024-10-11 10:32:52 +00:00
|
|
|
standalone: false,
|
2023-10-17 09:45:16 +00:00
|
|
|
})
|
|
|
|
|
class MyDir2 {}
|
2017-10-28 00:04:11 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
@Component({
|
|
|
|
|
template: '<div myDir1></div><div myDir2></div>',
|
2024-10-11 10:32:52 +00:00
|
|
|
standalone: false,
|
2023-10-17 09:45:16 +00:00
|
|
|
})
|
|
|
|
|
class MyComp {}
|
2017-10-28 00:04:11 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
TestBed.overrideProvider(aTok, {useValue: 'mockA'});
|
|
|
|
|
const ctx = TestBed.configureTestingModule({
|
|
|
|
|
declarations: [MyComp, MyDir1, MyDir2],
|
|
|
|
|
}).createComponent(MyComp);
|
|
|
|
|
expect(ctx.debugElement.children[0].injector.get(aTok)).toBe('mockA');
|
|
|
|
|
expect(ctx.debugElement.children[1].injector.get(aTok)).toBe('mockA');
|
2018-12-17 22:47:51 +00:00
|
|
|
});
|
2017-10-28 00:04:11 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
describe('injecting eager providers into an eager overwritten provider', () => {
|
|
|
|
|
@Component({
|
|
|
|
|
template: '',
|
|
|
|
|
providers: [
|
|
|
|
|
{provide: aTok, useFactory: () => 'aValue'},
|
|
|
|
|
{provide: bTok, useFactory: () => 'bValue'},
|
|
|
|
|
],
|
2024-10-11 10:32:52 +00:00
|
|
|
standalone: false,
|
2023-10-17 09:45:16 +00:00
|
|
|
})
|
|
|
|
|
class MyComp {
|
|
|
|
|
// Component is eager, which makes all of its deps eager
|
|
|
|
|
constructor(@Inject(aTok) a: any, @Inject(bTok) b: any) {}
|
2018-12-17 22:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should inject providers that were declared before it', () => {
|
|
|
|
|
TestBed.overrideProvider(bTok, {
|
|
|
|
|
useFactory: (a: string) => `mockB: ${a}`,
|
|
|
|
|
deps: [aTok],
|
|
|
|
|
});
|
|
|
|
|
const ctx = TestBed.configureTestingModule({declarations: [MyComp]}).createComponent(
|
|
|
|
|
MyComp,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(ctx.debugElement.injector.get(bTok)).toBe('mockB: aValue');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should inject providers that were declared after it', () => {
|
|
|
|
|
TestBed.overrideProvider(aTok, {
|
|
|
|
|
useFactory: (b: string) => `mockA: ${b}`,
|
|
|
|
|
deps: [bTok],
|
|
|
|
|
});
|
|
|
|
|
const ctx = TestBed.configureTestingModule({declarations: [MyComp]}).createComponent(
|
|
|
|
|
MyComp,
|
|
|
|
|
);
|
2018-12-17 22:47:51 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
expect(ctx.debugElement.injector.get(aTok)).toBe('mockA: bValue');
|
|
|
|
|
});
|
2018-12-17 22:47:51 +00:00
|
|
|
});
|
2017-10-28 00:04:11 +00:00
|
|
|
});
|
|
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should reset overrides when the testing modules is resetted', () => {
|
|
|
|
|
TestBed.overrideProvider(aTok, {useValue: 'mockValue'});
|
|
|
|
|
TestBed.resetTestingModule();
|
|
|
|
|
TestBed.configureTestingModule({providers: [{provide: aTok, useValue: 'aValue'}]});
|
|
|
|
|
expect(TestBed.inject(aTok)).toBe('aValue');
|
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 16:37:30 +00:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
describe('overrideTemplateUsingTestingModule', () => {
|
|
|
|
|
it('should compile the template in the context of the testing module', () => {
|
2024-10-11 10:32:52 +00:00
|
|
|
@Component({
|
|
|
|
|
selector: 'comp',
|
|
|
|
|
template: 'a',
|
|
|
|
|
standalone: false,
|
|
|
|
|
})
|
2023-10-17 09:45:16 +00:00
|
|
|
class MyComponent {
|
|
|
|
|
prop = 'some prop';
|
|
|
|
|
}
|
2015-11-19 02:46:24 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
let testDir: TestDir | undefined;
|
2015-10-08 22:33:17 +00:00
|
|
|
|
2024-10-11 10:32:52 +00:00
|
|
|
@Directive({
|
|
|
|
|
selector: '[test]',
|
|
|
|
|
standalone: false,
|
|
|
|
|
})
|
2023-10-17 09:45:16 +00:00
|
|
|
class TestDir {
|
|
|
|
|
constructor() {
|
|
|
|
|
testDir = this;
|
|
|
|
|
}
|
2015-10-08 22:33:17 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
@Input('test') test!: string;
|
|
|
|
|
}
|
2015-11-19 02:46:24 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
TestBed.overrideTemplateUsingTestingModule(
|
|
|
|
|
MyComponent,
|
|
|
|
|
'<div [test]="prop">Hello world!</div>',
|
|
|
|
|
);
|
2016-10-30 19:39:53 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
const fixture = TestBed.configureTestingModule({
|
|
|
|
|
declarations: [MyComponent, TestDir],
|
|
|
|
|
}).createComponent(MyComponent);
|
|
|
|
|
fixture.detectChanges();
|
|
|
|
|
expect(fixture.nativeElement).toHaveText('Hello world!');
|
|
|
|
|
expect(testDir).toBeInstanceOf(TestDir);
|
|
|
|
|
expect(testDir!.test).toBe('some prop');
|
2016-08-10 18:51:40 +00:00
|
|
|
});
|
2015-11-19 02:46:24 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should reset overrides when the testing module is resetted', () => {
|
2024-10-11 10:32:52 +00:00
|
|
|
@Component({
|
|
|
|
|
selector: 'comp',
|
|
|
|
|
template: 'a',
|
|
|
|
|
standalone: false,
|
|
|
|
|
})
|
2023-10-17 09:45:16 +00:00
|
|
|
class MyComponent {}
|
2015-10-08 22:33:17 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
TestBed.overrideTemplateUsingTestingModule(MyComponent, 'b');
|
2015-10-08 22:33:17 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
const fixture = TestBed.resetTestingModule()
|
|
|
|
|
.configureTestingModule({declarations: [MyComponent]})
|
|
|
|
|
.createComponent(MyComponent);
|
|
|
|
|
expect(fixture.nativeElement).toHaveText('a');
|
2016-08-10 18:51:40 +00:00
|
|
|
});
|
2023-10-17 09:45:16 +00:00
|
|
|
});
|
2015-10-08 22:33:17 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
describe('setting up the compiler', () => {
|
|
|
|
|
describe('providers', () => {
|
2021-11-09 19:02:39 +00:00
|
|
|
// TODO(alxhub): disable while we figure out how this should work
|
2023-10-17 09:45:16 +00:00
|
|
|
xit('should use set up providers', fakeAsync(() => {
|
|
|
|
|
// Keeping this component inside the test is needed to make sure it's not resolved
|
|
|
|
|
// prior to this test, thus having ɵcmp and a reference in resource
|
|
|
|
|
// resolution queue. This is done to check external resoution logic in isolation by
|
|
|
|
|
// configuring TestBed with the necessary ResourceLoader instance.
|
2021-11-09 19:02:39 +00:00
|
|
|
@Component({
|
|
|
|
|
selector: 'comp',
|
2023-10-17 09:45:16 +00:00
|
|
|
templateUrl: '/base/angular/packages/platform-browser/test/static_assets/test.html',
|
2024-10-11 10:32:52 +00:00
|
|
|
standalone: false,
|
2021-11-09 19:02:39 +00:00
|
|
|
})
|
2023-10-17 09:45:16 +00:00
|
|
|
class InternalCompWithUrlTemplate {}
|
2024-04-19 16:13:59 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
const resourceLoaderGet = jasmine
|
|
|
|
|
.createSpy('resourceLoaderGet')
|
|
|
|
|
.and.returnValue(Promise.resolve('Hello world!'));
|
|
|
|
|
TestBed.configureTestingModule({declarations: [InternalCompWithUrlTemplate]});
|
|
|
|
|
TestBed.configureCompiler({
|
|
|
|
|
providers: [{provide: ResourceLoader, useValue: {get: resourceLoaderGet}}],
|
|
|
|
|
});
|
2024-04-19 16:13:59 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
TestBed.compileComponents();
|
|
|
|
|
tick();
|
|
|
|
|
const compFixture = TestBed.createComponent(InternalCompWithUrlTemplate);
|
|
|
|
|
expect(compFixture.nativeElement).toHaveText('Hello world!');
|
|
|
|
|
}));
|
2021-11-09 02:20:56 +00: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 16:37:30 +00:00
|
|
|
});
|
2023-10-17 09:45:16 +00:00
|
|
|
});
|
2016-07-28 11:54:49 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
describe('errors', () => {
|
|
|
|
|
let originalJasmineIt: (description: string, func: () => void) => jasmine.Spec;
|
|
|
|
|
|
|
|
|
|
const patchJasmineIt = () => {
|
|
|
|
|
let resolve: (result: any) => void;
|
|
|
|
|
let reject: (error: any) => void;
|
|
|
|
|
const promise = new Promise((res, rej) => {
|
|
|
|
|
resolve = res;
|
|
|
|
|
reject = rej;
|
2016-08-10 18:51:40 +00:00
|
|
|
});
|
2023-10-17 09:45:16 +00:00
|
|
|
const jasmineEnv = jasmine.getEnv() as any;
|
|
|
|
|
originalJasmineIt = jasmineEnv.it;
|
|
|
|
|
jasmineEnv.it = (description: string, fn: (done: DoneFn) => void): any => {
|
|
|
|
|
const done = <DoneFn>(() => resolve(null));
|
|
|
|
|
done.fail = (err) => reject(err);
|
|
|
|
|
fn(done);
|
|
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
return promise;
|
|
|
|
|
};
|
2015-10-08 22:33:17 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
const restoreJasmineIt = () => ((jasmine.getEnv() as any).it = originalJasmineIt);
|
2015-10-08 22:33:17 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should fail when an asynchronous error is thrown', (done) => {
|
|
|
|
|
const itPromise = patchJasmineIt();
|
|
|
|
|
const barError = new Error('bar');
|
2015-10-08 22:33:17 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('throws an async error', waitForAsync(
|
|
|
|
|
inject([], () =>
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
throw barError;
|
|
|
|
|
}, 0),
|
2024-04-19 16:13:59 +00:00
|
|
|
),
|
2023-10-17 09:45:16 +00:00
|
|
|
));
|
2024-04-19 16:13:59 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
itPromise.then(
|
|
|
|
|
() => done.fail('Expected test to fail, but it did not'),
|
|
|
|
|
(err) => {
|
|
|
|
|
expect(err).toEqual(barError);
|
|
|
|
|
done();
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
restoreJasmineIt();
|
|
|
|
|
});
|
2015-10-08 22:33:17 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should fail when a returned promise is rejected', (done) => {
|
|
|
|
|
const itPromise = patchJasmineIt();
|
2015-10-08 22:33:17 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should fail with an error from a promise', waitForAsync(
|
|
|
|
|
inject([], () => {
|
|
|
|
|
let reject: (error: any) => void = undefined!;
|
|
|
|
|
const promise = new Promise((_, rej) => (reject = rej));
|
|
|
|
|
const p = promise.then(() => expect(1).toEqual(2));
|
2024-04-19 16:13:59 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
reject('baz');
|
|
|
|
|
return p;
|
|
|
|
|
}),
|
|
|
|
|
));
|
2024-04-19 16:13:59 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
itPromise.then(
|
|
|
|
|
() => done.fail('Expected test to fail, but it did not'),
|
|
|
|
|
(err) => {
|
2023-11-02 16:00:52 +00:00
|
|
|
expect(err.message).toEqual('baz');
|
2023-10-17 09:45:16 +00:00
|
|
|
done();
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
restoreJasmineIt();
|
2016-08-10 18:51:40 +00:00
|
|
|
});
|
2023-10-17 09:45:16 +00:00
|
|
|
|
|
|
|
|
describe('components', () => {
|
|
|
|
|
let resourceLoaderGet: jasmine.Spy;
|
2016-08-10 18:51:40 +00:00
|
|
|
beforeEach(() => {
|
2023-10-17 09:45:16 +00:00
|
|
|
resourceLoaderGet = jasmine
|
|
|
|
|
.createSpy('resourceLoaderGet')
|
|
|
|
|
.and.returnValue(Promise.resolve('Hello world!'));
|
|
|
|
|
TestBed.configureCompiler({
|
|
|
|
|
providers: [{provide: ResourceLoader, useValue: {get: resourceLoaderGet}}],
|
|
|
|
|
});
|
2016-08-10 18:51:40 +00:00
|
|
|
});
|
2015-10-08 22:33:17 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
// TODO(alxhub): disable while we figure out how this should work
|
|
|
|
|
xit('should report an error for declared components with templateUrl which never call TestBed.compileComponents', () => {
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'comp',
|
|
|
|
|
templateUrl: '/base/angular/packages/platform-browser/test/static_assets/test.html',
|
2024-10-11 10:32:52 +00:00
|
|
|
standalone: false,
|
2023-10-17 09:45:16 +00:00
|
|
|
})
|
|
|
|
|
class InlineCompWithUrlTemplate {}
|
|
|
|
|
|
|
|
|
|
expect(
|
|
|
|
|
withModule({declarations: [InlineCompWithUrlTemplate]}, () =>
|
|
|
|
|
TestBed.createComponent(InlineCompWithUrlTemplate),
|
2024-04-19 16:13:59 +00:00
|
|
|
),
|
2023-10-17 09:45:16 +00:00
|
|
|
).toThrowError(`Component 'InlineCompWithUrlTemplate' is not resolved:
|
|
|
|
|
- templateUrl: /base/angular/packages/platform-browser/test/static_assets/test.html
|
|
|
|
|
Did you run and wait for 'resolveComponentResources()'?`);
|
|
|
|
|
});
|
2016-08-10 18:51:40 +00:00
|
|
|
});
|
2020-09-04 20:57:21 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should error on unknown bound properties on custom elements by default', () => {
|
2024-10-11 10:32:52 +00:00
|
|
|
@Component({
|
|
|
|
|
template: '<div [someUnknownProp]="true"></div>',
|
|
|
|
|
standalone: false,
|
|
|
|
|
})
|
2023-10-17 09:45:16 +00:00
|
|
|
class ComponentUsingInvalidProperty {}
|
|
|
|
|
|
|
|
|
|
const spy = spyOn(console, 'error');
|
|
|
|
|
withModule({declarations: [ComponentUsingInvalidProperty]}, () => {
|
|
|
|
|
const fixture = TestBed.createComponent(ComponentUsingInvalidProperty);
|
|
|
|
|
fixture.detectChanges();
|
|
|
|
|
})();
|
|
|
|
|
expect(spy.calls.mostRecent().args[0]).toMatch(/Can't bind to 'someUnknownProp'/);
|
|
|
|
|
});
|
|
|
|
|
});
|
2020-09-04 20:57:21 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
describe('creating components', () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
TestBed.configureTestingModule({
|
|
|
|
|
declarations: [
|
|
|
|
|
ChildComp,
|
|
|
|
|
MyIfComp,
|
|
|
|
|
ChildChildComp,
|
|
|
|
|
ParentComp,
|
|
|
|
|
TestProvidersComp,
|
|
|
|
|
TestViewProvidersComp,
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
});
|
2020-09-04 20:57:21 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should instantiate a component with valid DOM', waitForAsync(() => {
|
|
|
|
|
const fixture = TestBed.createComponent(ChildComp);
|
|
|
|
|
fixture.detectChanges();
|
|
|
|
|
|
|
|
|
|
expect(fixture.nativeElement).toHaveText('Original Child');
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
it('should allow changing members of the component', waitForAsync(() => {
|
|
|
|
|
const componentFixture = TestBed.createComponent(MyIfComp);
|
|
|
|
|
componentFixture.detectChanges();
|
|
|
|
|
expect(componentFixture.nativeElement).toHaveText('MyIf()');
|
|
|
|
|
|
|
|
|
|
componentFixture.componentInstance.showMore = true;
|
2025-09-09 19:24:22 +00:00
|
|
|
componentFixture.changeDetectorRef.markForCheck();
|
2023-10-17 09:45:16 +00:00
|
|
|
componentFixture.detectChanges();
|
|
|
|
|
expect(componentFixture.nativeElement).toHaveText('MyIf(More)');
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
it('should override a template', waitForAsync(() => {
|
|
|
|
|
TestBed.overrideComponent(ChildComp, {set: {template: '<span>Mock</span>'}});
|
|
|
|
|
const componentFixture = TestBed.createComponent(ChildComp);
|
|
|
|
|
componentFixture.detectChanges();
|
|
|
|
|
expect(componentFixture.nativeElement).toHaveText('Mock');
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
it('should override a provider', waitForAsync(() => {
|
|
|
|
|
TestBed.overrideComponent(TestProvidersComp, {
|
|
|
|
|
set: {providers: [{provide: FancyService, useClass: MockFancyService}]},
|
|
|
|
|
});
|
|
|
|
|
const componentFixture = TestBed.createComponent(TestProvidersComp);
|
|
|
|
|
componentFixture.detectChanges();
|
|
|
|
|
expect(componentFixture.nativeElement).toHaveText('injected value: mocked out value');
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
it('should override a viewProvider', waitForAsync(() => {
|
|
|
|
|
TestBed.overrideComponent(TestViewProvidersComp, {
|
|
|
|
|
set: {viewProviders: [{provide: FancyService, useClass: MockFancyService}]},
|
|
|
|
|
});
|
2024-04-19 16:13:59 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
const componentFixture = TestBed.createComponent(TestViewProvidersComp);
|
|
|
|
|
componentFixture.detectChanges();
|
|
|
|
|
expect(componentFixture.nativeElement).toHaveText('injected value: mocked out value');
|
|
|
|
|
}));
|
|
|
|
|
});
|
|
|
|
|
describe('using alternate components', () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
TestBed.configureTestingModule({
|
|
|
|
|
declarations: [MockChildComp, ParentComp],
|
2020-09-04 20:57:21 +00:00
|
|
|
});
|
2023-10-17 09:45:16 +00:00
|
|
|
});
|
2020-09-04 20:57:21 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should override component dependencies', waitForAsync(() => {
|
|
|
|
|
const componentFixture = TestBed.createComponent(ParentComp);
|
|
|
|
|
componentFixture.detectChanges();
|
|
|
|
|
expect(componentFixture.nativeElement).toHaveText('Parent(Mock)');
|
|
|
|
|
}));
|
|
|
|
|
});
|
2020-09-04 20:57:21 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
describe('calling override methods after TestBed initialization', () => {
|
|
|
|
|
const getExpectedErrorMessage = (methodName: string, methodDescription: string) =>
|
|
|
|
|
`Cannot ${methodDescription} when the test module has already been instantiated. Make sure you are not using \`inject\` before \`${methodName}\`.`;
|
2020-09-04 20:57:21 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should throw if TestBed.overrideProvider is called after TestBed initialization', () => {
|
|
|
|
|
TestBed.inject(Injector);
|
2020-09-04 20:57:21 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
expect(() =>
|
|
|
|
|
TestBed.overrideProvider(aTok, {
|
|
|
|
|
useValue: 'mockValue',
|
|
|
|
|
}),
|
|
|
|
|
).toThrowError(getExpectedErrorMessage('overrideProvider', 'override provider'));
|
|
|
|
|
});
|
2020-09-04 20:57:21 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should throw if TestBed.overrideModule is called after TestBed initialization', () => {
|
|
|
|
|
@NgModule()
|
|
|
|
|
class MyModule {}
|
2020-09-04 20:57:21 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
TestBed.inject(Injector);
|
2020-09-04 20:57:21 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
expect(() => TestBed.overrideModule(MyModule, {})).toThrowError(
|
|
|
|
|
getExpectedErrorMessage('overrideModule', 'override module metadata'),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw if TestBed.overridePipe is called after TestBed initialization', () => {
|
2024-10-11 10:32:52 +00:00
|
|
|
@Pipe({
|
|
|
|
|
name: 'myPipe',
|
|
|
|
|
standalone: false,
|
|
|
|
|
})
|
2023-10-17 09:45:16 +00:00
|
|
|
class MyPipe {
|
|
|
|
|
transform(value: any) {
|
|
|
|
|
return value;
|
2020-09-04 20:57:21 +00:00
|
|
|
}
|
2023-10-17 09:45:16 +00:00
|
|
|
}
|
2020-09-04 20:57:21 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
TestBed.inject(Injector);
|
2020-09-04 20:57:21 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
expect(() => TestBed.overridePipe(MyPipe, {})).toThrowError(
|
|
|
|
|
getExpectedErrorMessage('overridePipe', 'override pipe metadata'),
|
|
|
|
|
);
|
|
|
|
|
});
|
2020-09-04 20:57:21 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should throw if TestBed.overrideDirective is called after TestBed initialization', () => {
|
|
|
|
|
@Directive()
|
|
|
|
|
class MyDirective {}
|
2020-09-04 20:57:21 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
TestBed.inject(Injector);
|
2020-09-04 20:57:21 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
expect(() => TestBed.overrideDirective(MyDirective, {})).toThrowError(
|
|
|
|
|
getExpectedErrorMessage('overrideDirective', 'override directive metadata'),
|
|
|
|
|
);
|
2020-09-04 20:57:21 +00:00
|
|
|
});
|
2023-04-26 14:59:11 +00:00
|
|
|
|
2023-10-17 09:45:16 +00:00
|
|
|
it('should throw if TestBed.overrideTemplateUsingTestingModule is called after TestBed initialization', () => {
|
2024-10-11 10:32:52 +00:00
|
|
|
@Component({
|
|
|
|
|
selector: 'comp',
|
|
|
|
|
template: 'a',
|
|
|
|
|
standalone: false,
|
|
|
|
|
})
|
2023-10-17 09:45:16 +00:00
|
|
|
class MyComponent {}
|
|
|
|
|
|
|
|
|
|
TestBed.inject(Injector);
|
|
|
|
|
|
|
|
|
|
expect(() => TestBed.overrideTemplateUsingTestingModule(MyComponent, 'b')).toThrowError(
|
|
|
|
|
/Cannot override template when the test module has already been instantiated/,
|
|
|
|
|
);
|
|
|
|
|
});
|
2015-10-08 22:33:17 +00:00
|
|
|
});
|
2023-10-17 09:45:16 +00:00
|
|
|
|
|
|
|
|
it('TransferState re-export can be used as a type and contructor', () => {
|
|
|
|
|
const transferState: TransferState = new TransferState();
|
|
|
|
|
expect(transferState).toBeDefined();
|
|
|
|
|
});
|
|
|
|
|
});
|