MCPcopy
hub / github.com/rrweb-io/rrweb / stringifySnapshots

Function stringifySnapshots

packages/all/test/utils.ts:94–227  ·  view source on GitHub ↗

* Puppeteer may cast random mouse move which make our tests flaky. * So we only do snapshot test with filtered events. * Also remove timestamp from event. * @param snapshots incrementalSnapshotEvent[]

(snapshots: eventWithTime[])

Source from the content-addressed store, hash-verified

92 * @param snapshots incrementalSnapshotEvent[]
93 */
94function stringifySnapshots(snapshots: eventWithTime[]): string {
95 return JSON.stringify(
96 snapshots
97 .filter((s) => {
98 if (
99 s.type === EventType.IncrementalSnapshot &&
100 (s.data.source === IncrementalSource.MouseMove ||
101 s.data.source === IncrementalSource.ViewportResize)
102 ) {
103 return false;
104 }
105 return true;
106 })
107 .map((s) => {
108 if (s.type === EventType.Meta) {
109 s.data.href = 'about:blank';
110 }
111 // FIXME: travis coordinates seems different with my laptop
112 const coordinatesReg =
113 /(bottom|top|left|right|width|height): \d+(\.\d+)?px/g;
114 if (
115 s.type === EventType.IncrementalSnapshot &&
116 s.data.source === IncrementalSource.MouseInteraction
117 ) {
118 delete (s.data as Optional<mouseInteractionData, 'x'>).x;
119 delete (s.data as Optional<mouseInteractionData, 'y'>).y;
120 }
121 if (
122 s.type === EventType.IncrementalSnapshot &&
123 s.data.source === IncrementalSource.Mutation
124 ) {
125 s.data.attributes.forEach((a) => {
126 if ('style' in a.attributes && a.attributes.style) {
127 if (typeof a.attributes.style === 'object') {
128 for (const [k, v] of Object.entries(a.attributes.style)) {
129 if (Array.isArray(v)) {
130 if (coordinatesReg.test(k + ': ' + v[0])) {
131 // TODO: could round the number here instead depending on what's coming out of various test envs
132 a.attributes.style[k] = ['Npx', v[1]];
133 }
134 } else if (typeof v === 'string') {
135 if (coordinatesReg.test(k + ': ' + v)) {
136 a.attributes.style[k] = 'Npx';
137 }
138 }
139 coordinatesReg.lastIndex = 0; // wow, a real wart in ECMAScript
140 }
141 } else if (coordinatesReg.test(a.attributes.style)) {
142 a.attributes.style = a.attributes.style.replace(
143 coordinatesReg,
144 '$1: Npx',
145 );
146 }
147 }
148
149 // strip blob:urls as they are different every time
150 stripBlobURLsFromAttributes(a);
151 });

Callers 2

assertSnapshotFunction · 0.90
assertSnapshotFunction · 0.70

Calls 2

replaceMethod · 0.65

Tested by 1

assertSnapshotFunction · 0.72