Skip to content

Commit

Permalink
refactor(compiler): add utility to enable deferred blocks for testing (
Browse files Browse the repository at this point in the history
…#51183)

Adds the `ɵsetEnabledBlockTypes` utility that can be used when writing JIT tests using `defer` blocks. Intended usage:

```ts
import {ɵsetEnabledBlockTypes as setEnabledBlockTypes} from '@angular/compiler/src/jit_compiler_facade';

describe('deferred tests', () => {
  beforeEach(() => setEnabledBlockTypes(['defer']));
  afterEach(() => setEnabledBlockTypes([]));

  it('should work', () => {
    // test goes here
  });
});
```

PR Close #51183
  • Loading branch information
crisbeto authored and alxhub committed Jul 28, 2023
1 parent 09bf327 commit 7225528
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions packages/compiler/src/jit_compiler_facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ import {makeBindingParser, parseTemplate} from './render3/view/template';
import {ResourceLoader} from './resource_loader';
import {DomElementSchemaRegistry} from './schema/dom_element_schema_registry';

let enabledBlockTypes: Set<string>|undefined;

/** Temporary utility that enables specific block types in JIT compilations. */
export function ɵsetEnabledBlockTypes(types: string[]) {
enabledBlockTypes = types.length > 0 ? new Set(types) : undefined;
}

export class CompilerFacadeImpl implements CompilerFacade {
FactoryTarget = FactoryTarget;
ResourceLoader = ResourceLoader;
Expand Down Expand Up @@ -524,11 +531,8 @@ function parseJitTemplate(
const interpolationConfig =
interpolation ? InterpolationConfig.fromArray(interpolation) : DEFAULT_INTERPOLATION_CONFIG;
// Parse the template and check for errors.
const parsed = parseTemplate(template, sourceMapUrl, {
preserveWhitespaces,
interpolationConfig,
enabledBlockTypes: new Set(), // TODO: enable deferred blocks when testing in JIT mode.
});
const parsed = parseTemplate(
template, sourceMapUrl, {preserveWhitespaces, interpolationConfig, enabledBlockTypes});
if (parsed.errors !== null) {
const errors = parsed.errors.map(err => err.toString()).join(', ');
throw new Error(`Errors during JIT compilation of template for ${typeName}: ${errors}`);
Expand Down

0 comments on commit 7225528

Please sign in to comment.