-
Notifications
You must be signed in to change notification settings - Fork 309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding serializer to remove internal angular attributes from the snapshots #97
Changes from 9 commits
aa2ce1a
e3c86e3
8710de4
aa334d7
9a27a1a
cfe98e1
d8577b9
d656546
a582354
61ad6a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
InlineHtmlStripStylesTransformer.js | ||
*.log | ||
.idea |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict'; | ||
|
||
const jestDOMElementSerializer = require('pretty-format').plugins.DOMElement; | ||
|
||
const attributesToRemovePatterns = ['ng-reflect', '_nghost', '_ngcontent', 'ng-version']; | ||
|
||
const hasAttributesToRemove = (attribute) => | ||
attributesToRemovePatterns | ||
.some(removePattern => attribute.name.startsWith(removePattern)); | ||
|
||
const serialize = (node, ...rest) => { | ||
const nodeCopy = node.cloneNode(true); | ||
Object.values(nodeCopy.attributes) | ||
.filter(hasAttributesToRemove) | ||
.forEach(attribute => nodeCopy.attributes.removeNamedItem(attribute.name)); | ||
|
||
return jestDOMElementSerializer.serialize(nodeCopy, ...rest); | ||
}; | ||
|
||
const serializeTestFn = (val) => { | ||
return val.attributes !== undefined && Object.values(val.attributes) | ||
.some(hasAttributesToRemove) | ||
}; | ||
const test = val => | ||
jestDOMElementSerializer.test(val) && serializeTestFn(val); | ||
|
||
module.exports = { | ||
serialize: serialize, | ||
test: test | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
"build": "ng build", | ||
"test": "jest", | ||
"test:watch": "jest --watch", | ||
"test:ci": "jest --runInBand", | ||
"test:ci": "jest --runInBand --no-cache", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why no-cache? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @wtho looks like it was your change... do we still need it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It can be removed! |
||
"test:coverage": "jest --coverage", | ||
"lint": "ng lint", | ||
"e2e": "ng e2e" | ||
|
@@ -44,6 +44,11 @@ | |
}, | ||
"jest": { | ||
"preset": "jest-preset-angular", | ||
"snapshotSerializers": [ | ||
"jest-preset-angular/AngularNoNgAttributesSnapshotSerializer.js", | ||
"jest-preset-angular/AngularSnapshotSerializer.js", | ||
"jest-preset-angular/HTMLCommentSerializer.js" | ||
], | ||
"moduleNameMapper": { | ||
"\\.(jpg|jpeg|png)$": "<rootDir>/__mocks__/image.js", | ||
"^@lib/(.*)$": "<rootDir>/src/lib/$1" | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`MediumComponent snapshot test 1`] = ` | ||
<app-medium | ||
anotherVar="false" | ||
someVar={[Function Boolean]} | ||
> | ||
<div | ||
class="some-wrapper-class" | ||
> | ||
diary works! | ||
|
||
<div | ||
class="inner-class" | ||
> | ||
|
||
<div> | ||
another case | ||
</div> | ||
<span> | ||
some html | ||
</span> | ||
</div> | ||
<div> | ||
one more case case | ||
</div> | ||
<div> | ||
<app-child-component | ||
someinput="someVar" | ||
> | ||
stubbedBody | ||
</app-child-component> | ||
</div> | ||
</div> | ||
</app-medium> | ||
`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Component, Input } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'app-child-component', | ||
thymikee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
template: ` | ||
<div class="child"> | ||
{{ someInput }} | ||
<div> | ||
rly | ||
<p>complex</p> | ||
<div>component | ||
<div>oh my god!</div> | ||
</div> | ||
</div> | ||
</div> | ||
` | ||
}) | ||
export class ChildComponent { | ||
@Input() someInput = null; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* tslint:disable:no-unused-variable */ | ||
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { MediumComponent } from './medium.component'; | ||
import { ChildComponent } from './child.component'; | ||
|
||
describe('MediumComponent', () => { | ||
let component: MediumComponent; | ||
let fixture: ComponentFixture<MediumComponent>; | ||
|
||
beforeEach( | ||
async(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [MediumComponent, ChildComponent], | ||
}) | ||
.overrideComponent(ChildComponent, {set: {template: 'stubbedBody'}}) | ||
.compileComponents(); | ||
}), | ||
); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(MediumComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
it('snapshot test', () => { | ||
expect(fixture).toMatchSnapshot(); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Component } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'app-medium', | ||
template: ` | ||
<div class="some-wrapper-class"> | ||
thymikee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
diary works! | ||
<div *ngIf="someVar" class="inner-class"> | ||
<div *ngIf="anotherVar" class="inner-hidden"></div> | ||
<div>another case</div> | ||
<span>some html</span> | ||
</div> | ||
<div>one more case case</div> | ||
<div><app-child-component someInput="someVar"></app-child-component></div> | ||
</div> | ||
` | ||
}) | ||
export class MediumComponent { | ||
someVar = true; | ||
anotherVar = false; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`NgReflectAsTextComponent snapshots 1`] = ` | ||
<app-ng-reflect-as-text> | ||
<p | ||
data-some-attr="ng-reflect-as-attr-arg works" | ||
> | ||
ng-reflect-as-text works! | ||
|
||
</p> | ||
</app-ng-reflect-as-text> | ||
`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<p data-some-attr="ng-reflect-as-attr-arg works"> | ||
ng-reflect-as-text works! | ||
wtho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</p> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { NgReflectAsTextComponent } from './ng-reflect-as-text.component'; | ||
|
||
describe('NgReflectAsTextComponent', () => { | ||
let component: NgReflectAsTextComponent; | ||
let fixture: ComponentFixture<NgReflectAsTextComponent>; | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [ NgReflectAsTextComponent ] | ||
}) | ||
.compileComponents(); | ||
})); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(NgReflectAsTextComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('snapshots', () => { | ||
expect(fixture).toMatchSnapshot(); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Component } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'app-ng-reflect-as-text', | ||
templateUrl: './ng-reflect-as-text.component.html' | ||
}) | ||
export class NgReflectAsTextComponent {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`SimpleWithStylesComponent should generate snapshot without nghost/ngcontent 1`] = ` | ||
<app-simple-with-styles> | ||
<p> | ||
simple-with-styles works! | ||
|
||
</p><div | ||
class="some-class" | ||
> | ||
<app-child-component> | ||
<div | ||
class="child" | ||
> | ||
|
||
<div> | ||
rly | ||
<p> | ||
complex | ||
</p> | ||
<div> | ||
component | ||
<div> | ||
oh my god! | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</app-child-component> | ||
</div> | ||
</app-simple-with-styles> | ||
`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<p> | ||
simple-with-styles works! | ||
</p> | ||
<div class="some-class"> | ||
<app-child-component></app-child-component> | ||
</div> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add a link to the exact lines in package.json?