Skip to content

Commit

Permalink
chore: local fix for jest ng-snapshot serializer and re-enable snapsh…
Browse files Browse the repository at this point in the history
…ot tests
  • Loading branch information
brandonroberts committed Nov 8, 2021
1 parent 63e56d6 commit 2f2ef13
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 4 deletions.
2 changes: 1 addition & 1 deletion jest.preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
coverageReporters: ['html'],
snapshotSerializers: [
'jest-preset-angular/build/serializers/no-ng-attributes',
'jest-preset-angular/build/serializers/ng-snapshot',
'<rootDir>/../../ng-snapshot.js',
'jest-preset-angular/build/serializers/html-comment',
],
testRunner: 'jest-jasmine2'
Expand Down
66 changes: 66 additions & 0 deletions ng-snapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"use strict";
const ivyEnabled = () => {
const { ɵivyEnabled } = require('@angular/core');
return ɵivyEnabled;
};
const print = (fixture, print, indent, opts, colors) => {
var _a, _b;
let nodes = '';
let componentAttrs = '';
let componentName = '';
const componentRef = fixture.componentRef;
const componentInstance = fixture.componentInstance;
if (ivyEnabled()) {
const componentDef = componentRef.componentType.ɵcmp;
componentName = componentDef.selectors[0][0];
nodes = Array.from(componentRef.location.nativeElement.childNodes).map(print).join('');
}
else {
componentName = (_a = componentRef._elDef.element) === null || _a === void 0 ? void 0 : _a.name;
nodes = ((_b = componentRef._view.nodes) !== null && _b !== void 0 ? _b : [])
.filter((node) => node === null || node === void 0 ? void 0 : node.hasOwnProperty('renderElement'))
.map((node) => Array.from(node.renderElement.childNodes).map(print).join(''))
.join(opts.edgeSpacing);
}
const attributes = Object.keys(componentInstance);
if (attributes.length) {
componentAttrs += attributes
.sort()
.map((attribute) => {
const compAttrVal = componentInstance[attribute];
return (opts.spacing +
indent(`${colors.prop.open}${attribute}${colors.prop.close}=`) +
colors.value.open +
(compAttrVal && compAttrVal.constructor
? `{[Function ${compAttrVal.constructor.name}]}`
: `"${compAttrVal}"`) +
colors.value.close);
})
.join('');
}
return ('<' +
componentName +
componentAttrs +
(componentAttrs.length ? '\n' : '') +
'>\n' +
indent(nodes) +
'\n</' +
componentName +
'>').replace(/\n^\s*\n/gm, '\n');
};
/**
* This change is from https://github.com/thymikee/jest-preset-angular/commit/59ef5ba0e4617ad7cb0ca8b60d119efea107fddc
* which causes toMatchSnapshot tests to break for reducers
* Reverted locally as temp fix
*/
// const test = (val) => typeof val === 'object' && Object.prototype.hasOwnProperty.call(val, 'componentRef');
const test = (val) =>
val !== undefined &&
val !== null &&
typeof val === 'object' &&
Object.prototype.hasOwnProperty.call(val, 'componentRef');
typeof val === 'object' && Object.prototype.hasOwnProperty.call(val, 'componentRef');
module.exports = {
print,
test,
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AuthApiActions, AuthActions } from '@example-app/auth/actions';

import { User } from '@example-app/auth/models';

xdescribe('AuthReducer', () => {
describe('AuthReducer', () => {
describe('undefined action', () => {
it('should return the default state', () => {
const action = {} as any;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AuthApiActions, LoginPageActions } from '@example-app/auth/actions';

import { Credentials, User } from '@example-app/auth/models';

xdescribe('LoginPageReducer', () => {
describe('LoginPageReducer', () => {
describe('undefined action', () => {
it('should return the default state', () => {
const action = {} as any;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@example-app/books/actions';
import { Book, generateMockBook } from '@example-app/books/models';

xdescribe('BooksReducer', () => {
describe('BooksReducer', () => {
const book1 = generateMockBook();
const book2 = { ...book1, id: '222' };
const book3 = { ...book1, id: '333' };
Expand Down

0 comments on commit 2f2ef13

Please sign in to comment.