Skip to content

Commit

Permalink
feat: remove ivy flag for snapshot serializer
Browse files Browse the repository at this point in the history
From Angular 15+, Ivy is the ony mode
  • Loading branch information
ahnpnl committed May 7, 2024
1 parent 7bea07b commit 3433cd8
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`BannerComponent (inline template) should display the component 1`] = `
<div
id="root1"
>
<h1>
Test Tour of Heroes
</h1>
</div>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@ describe('BannerComponent (inline template)', () => {
expect(h1.textContent).toEqual('');
});

it('should display original title', () => {
it('should display the component', () => {
fixture.detectChanges();
expect(h1.textContent).toContain(component.title);
});

it('should display original title after detectChanges()', () => {
fixture.detectChanges();
expect(h1.textContent).toContain(component.title);
expect(fixture.debugElement.nativeElement).toMatchSnapshot();
});

it('should display a different test title', () => {
Expand Down
2 changes: 1 addition & 1 deletion examples/example-app-monorepo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "example-app-monorepo",
"version": "13.0.0",
"version": "17.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
Expand Down
6 changes: 0 additions & 6 deletions examples/example-app-v15/jest-esm.config.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import ngPreset from 'jest-preset-angular/presets/index.js';

globalThis.ngJest = {
skipNgcc: false,
tsconfig: 'tsconfig-esm.spec.json',
};

/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
const jestConfig = {
...ngPreset.defaultsESM,
globalSetup: 'jest-preset-angular/global-setup.mjs',
moduleNameMapper: {
tslib: 'tslib/tslib.es6.js',
rxjs: '<rootDir>/node_modules/rxjs/dist/bundles/rxjs.umd.js',
Expand Down
7 changes: 0 additions & 7 deletions examples/example-app-v15/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
const { pathsToModuleNameMapper } = require('ts-jest');
const { paths } = require('./tsconfig.json').compilerOptions;

// eslint-disable-next-line no-undef
globalThis.ngJest = {
skipNgcc: false,
tsconfig: 'tsconfig.spec.json',
};

/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
module.exports = {
preset: 'jest-preset-angular',
globalSetup: 'jest-preset-angular/global-setup',
moduleNameMapper: pathsToModuleNameMapper(paths, { prefix: '<rootDir>' }),
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`BannerComponent (inline template) should display the component 1`] = `
<div
id="root1"
>
<h1>
Test Tour of Heroes
</h1>
</div>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@ describe('BannerComponent (inline template)', () => {
expect(h1.textContent).toEqual('');
});

it('should display original title', () => {
it('should display the component', () => {
fixture.detectChanges();
expect(h1.textContent).toContain(component.title);
});

it('should display original title after detectChanges()', () => {
fixture.detectChanges();
expect(h1.textContent).toContain(component.title);
expect(fixture.debugElement.nativeElement).toMatchSnapshot();
});

it('should display a different test title', () => {
Expand Down
4 changes: 0 additions & 4 deletions examples/example-app-v17/jest-esm.config.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import ngPreset from 'jest-preset-angular/presets/index.js';

globalThis.ngJest = {
tsconfig: 'tsconfig-esm.spec.json',
};

/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
const jestConfig = {
...ngPreset.defaultsESM,
Expand Down
5 changes: 0 additions & 5 deletions examples/example-app-v17/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
const { pathsToModuleNameMapper } = require('ts-jest');
const { paths } = require('./tsconfig.json').compilerOptions;

// eslint-disable-next-line no-undef
globalThis.ngJest = {
tsconfig: 'tsconfig.spec.json',
};

/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
module.exports = {
preset: 'jest-preset-angular',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`BannerComponent (inline template) should display the component 1`] = `
<div
id="root1"
>
<h1>
Test Tour of Heroes
</h1>
</div>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@ describe('BannerComponent (inline template)', () => {
expect(h1.textContent).toEqual('');
});

it('should display original title', () => {
it('should display the component', () => {
fixture.detectChanges();
expect(h1.textContent).toContain(component.title);
});

it('should display original title after detectChanges()', () => {
fixture.detectChanges();
expect(h1.textContent).toContain(component.title);
expect(fixture.debugElement.nativeElement).toMatchSnapshot();
});

it('should display a different test title', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
const { pathsToModuleNameMapper } = require('ts-jest');
const { paths } = require('./tsconfig.json').compilerOptions;

// eslint-disable-next-line no-undef
globalThis.ngJest = {
tsconfig: 'tsconfig.spec.json',
};

/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
module.exports = {
preset: 'jest-preset-angular',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`BannerComponent (inline template) should display the component 1`] = `
<div
id="root1"
>
<h1>
Test Tour of Heroes
</h1>
</div>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@ describe('BannerComponent (inline template)', () => {
expect(h1.textContent).toEqual('');
});

it('should display original title', () => {
it('should display the component', () => {
fixture.detectChanges();
expect(h1.textContent).toContain(component.title);
});

it('should display original title after detectChanges()', () => {
fixture.detectChanges();
expect(h1.textContent).toContain(component.title);
expect(fixture.debugElement.nativeElement).toMatchSnapshot();
});

it('should display a different test title', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
const { pathsToModuleNameMapper } = require('ts-jest');
const { paths } = require('./tsconfig.json').compilerOptions;

// eslint-disable-next-line no-undef
globalThis.ngJest = {
tsconfig: 'tsconfig.spec.json',
};

/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
module.exports = {
preset: 'jest-preset-angular',
Expand Down
46 changes: 12 additions & 34 deletions src/serializers/ng-snapshot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ComponentRef, DebugNode, Type, ɵCssSelectorList, ɵNgModuleType } from '@angular/core';
import type { ComponentRef, Type, ɵCssSelectorList } from '@angular/core';
import type { ComponentFixture } from '@angular/core/testing';
import type { Colors } from 'pretty-format';

Expand All @@ -21,11 +21,6 @@ interface NgComponentFixture extends ComponentFixture<unknown> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
componentInstance: Record<string, any>;
}
interface VEDebugNode {
renderElement: {
childNodes: DebugNode[];
};
}

/**
* The following types haven't been exported by jest so temporarily we copy typings from 'pretty-format'
Expand All @@ -39,36 +34,19 @@ type Indent = (indentSpaces: string) => string;
type Printer = (elementToSerialize: unknown) => string;

const attributesToRemovePatterns = ['__ngContext__'];
const ivyEnabled = (): boolean => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { NgModule }: typeof import('@angular/core') = require('@angular/core');

class IvyModule {}
NgModule()(IvyModule);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
return !!(IvyModule as ɵNgModuleType<unknown>).ɵmod;
};

const print = (fixture: unknown, print: Printer, indent: Indent, opts: PluginOptions, colors: Colors): string => {
let nodes = '';
const print = (
fixture: NgComponentFixture,
print: Printer,
indent: Indent,
opts: PluginOptions,
colors: Colors,
): string => {
let componentAttrs = '';
let componentName = '';
const componentRef = (fixture as NgComponentFixture).componentRef;
const componentInstance = (fixture as NgComponentFixture).componentInstance;

if (ivyEnabled()) {
const componentDef = componentRef.componentType.ɵcmp;
componentName = componentDef.selectors[0][0] as string;
nodes = Array.from(componentRef.location.nativeElement.childNodes).map(print).join('');
} else {
componentName = componentRef._elDef.element?.name;
nodes = (componentRef._view.nodes ?? [])
// eslint-disable-next-line no-prototype-builtins
.filter((node: VEDebugNode) => node?.hasOwnProperty('renderElement'))
.map((node: VEDebugNode) => Array.from(node.renderElement.childNodes).map(print).join(''))
.join(opts.edgeSpacing);
}
const { componentRef, componentInstance } = fixture;
const componentDef = componentRef.componentType.ɵcmp;
const componentName = componentDef.selectors[0][0] as string;
const nodes = Array.from(componentRef.location.nativeElement.childNodes).map(print).join('');
const attributes = Object.keys(componentInstance).filter((key) => !attributesToRemovePatterns.includes(key));
if (attributes.length) {
componentAttrs += attributes
Expand Down

0 comments on commit 3433cd8

Please sign in to comment.