How to use unstable_trace method in Playwright Internal

Best JavaScript code snippet using playwright-internal

Run Playwright Internal automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Tracing-test.internal.js

Source: Tracing-test.internal.js Github

copy
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @jest-environment node
8 */
9'use strict';
10
11describe('Tracing', () => {
12  let SchedulerTracing;
13  let ReactFeatureFlags;
14
15  let advanceTimeBy;
16  let currentTime;
17
18  function loadModules({enableSchedulerTracing}) {
19    jest.resetModules();
20    jest.useFakeTimers();
21
22    currentTime = 0;
23    Date.now = jest.fn().mockImplementation(() => currentTime);
24
25    advanceTimeBy = amount => {
26      currentTime += amount;
27    };
28
29    ReactFeatureFlags = require('shared/ReactFeatureFlags');
30    ReactFeatureFlags.enableSchedulerTracing = enableSchedulerTracing;
31
32    SchedulerTracing = require('scheduler/tracing');
33  }
34
35  describe('enableSchedulerTracing enabled', () => {
36    beforeEach(() => loadModules({enableSchedulerTracing: true}));
37
38    it('should return the value of a traced function', () => {
39      expect(
40        SchedulerTracing.unstable_trace('arbitrary', currentTime, () => 123),
41      ).toBe(123);
42    });
43
44    it('should return the value of a clear function', () => {
45      expect(SchedulerTracing.unstable_clear(() => 123)).toBe(123);
46    });
47
48    it('should return the value of a wrapped function', () => {
49      let wrapped;
50      SchedulerTracing.unstable_trace('arbitrary', currentTime, () => {
51        wrapped = SchedulerTracing.unstable_wrap(() => 123);
52      });
53      expect(wrapped()).toBe(123);
54    });
55
56    it('should pass arguments through to a wrapped function', done => {
57      let wrapped;
58      SchedulerTracing.unstable_trace('arbitrary', currentTime, () => {
59        wrapped = SchedulerTracing.unstable_wrap((param1, param2) => {
60          expect(param1).toBe('foo');
61          expect(param2).toBe('bar');
62          done();
63        });
64      });
65      wrapped('foo', 'bar');
66    });
67
68    it('should return an empty set when outside of a traced event', () => {
69      expect(SchedulerTracing.unstable_getCurrent()).toContainNoInteractions();
70    });
71
72    it('should report the traced interaction from within the trace callback', done => {
73      advanceTimeBy(100);
74
75      SchedulerTracing.unstable_trace('some event', currentTime, () => {
76        const interactions = SchedulerTracing.unstable_getCurrent();
77        expect(interactions).toMatchInteractions([
78          {name: 'some event', timestamp: 100},
79        ]);
80
81        done();
82      });
83    });
84
85    it('should report the traced interaction from within wrapped callbacks', done => {
86      let wrappedIndirection;
87
88      function indirection() {
89        const interactions = SchedulerTracing.unstable_getCurrent();
90        expect(interactions).toMatchInteractions([
91          {name: 'some event', timestamp: 100},
92        ]);
93
94        done();
95      }
96
97      advanceTimeBy(100);
98
99      SchedulerTracing.unstable_trace('some event', currentTime, () => {
100        wrappedIndirection = SchedulerTracing.unstable_wrap(indirection);
101      });
102
103      advanceTimeBy(50);
104
105      wrappedIndirection();
106    });
107
108    it('should clear the interaction stack for traced callbacks', () => {
109      let innerTestReached = false;
110
111      SchedulerTracing.unstable_trace('outer event', currentTime, () => {
112        expect(SchedulerTracing.unstable_getCurrent()).toMatchInteractions([
113          {name: 'outer event'},
114        ]);
115
116        SchedulerTracing.unstable_clear(() => {
117          expect(SchedulerTracing.unstable_getCurrent()).toMatchInteractions(
118            [],
119          );
120
121          SchedulerTracing.unstable_trace('inner event', currentTime, () => {
122            expect(SchedulerTracing.unstable_getCurrent()).toMatchInteractions([
123              {name: 'inner event'},
124            ]);
125
126            innerTestReached = true;
127          });
128        });
129
130        expect(SchedulerTracing.unstable_getCurrent()).toMatchInteractions([
131          {name: 'outer event'},
132        ]);
133      });
134
135      expect(innerTestReached).toBe(true);
136    });
137
138    it('should clear the interaction stack for wrapped callbacks', () => {
139      let innerTestReached = false;
140      let wrappedIndirection;
141
142      const indirection = jest.fn(() => {
143        expect(SchedulerTracing.unstable_getCurrent()).toMatchInteractions([
144          {name: 'outer event'},
145        ]);
146
147        SchedulerTracing.unstable_clear(() => {
148          expect(SchedulerTracing.unstable_getCurrent()).toMatchInteractions(
149            [],
150          );
151
152          SchedulerTracing.unstable_trace('inner event', currentTime, () => {
153            expect(SchedulerTracing.unstable_getCurrent()).toMatchInteractions([
154              {name: 'inner event'},
155            ]);
156
157            innerTestReached = true;
158          });
159        });
160
161        expect(SchedulerTracing.unstable_getCurrent()).toMatchInteractions([
162          {name: 'outer event'},
163        ]);
164      });
165
166      SchedulerTracing.unstable_trace('outer event', currentTime, () => {
167        wrappedIndirection = SchedulerTracing.unstable_wrap(indirection);
168      });
169
170      wrappedIndirection();
171
172      expect(innerTestReached).toBe(true);
173    });
174
175    it('should support nested traced events', done => {
176      advanceTimeBy(100);
177
178      let innerIndirectionTraced = false;
179      let outerIndirectionTraced = false;
180
181      function innerIndirection() {
182        const interactions = SchedulerTracing.unstable_getCurrent();
183        expect(interactions).toMatchInteractions([
184          {name: 'outer event', timestamp: 100},
185          {name: 'inner event', timestamp: 150},
186        ]);
187
188        innerIndirectionTraced = true;
189      }
190
191      function outerIndirection() {
192        const interactions = SchedulerTracing.unstable_getCurrent();
193        expect(interactions).toMatchInteractions([
194          {name: 'outer event', timestamp: 100},
195        ]);
196
197        outerIndirectionTraced = true;
198      }
199
200      SchedulerTracing.unstable_trace('outer event', currentTime, () => {
201        // Verify the current traced event
202        let interactions = SchedulerTracing.unstable_getCurrent();
203        expect(interactions).toMatchInteractions([
204          {name: 'outer event', timestamp: 100},
205        ]);
206
207        advanceTimeBy(50);
208
209        const wrapperOuterIndirection = SchedulerTracing.unstable_wrap(
210          outerIndirection,
211        );
212
213        let wrapperInnerIndirection;
214        let innerEventTraced = false;
215
216        // Verify that a nested event is properly traced
217        SchedulerTracing.unstable_trace('inner event', currentTime, () => {
218          interactions = SchedulerTracing.unstable_getCurrent();
219          expect(interactions).toMatchInteractions([
220            {name: 'outer event', timestamp: 100},
221            {name: 'inner event', timestamp: 150},
222          ]);
223
224          // Verify that a wrapped outer callback is properly traced
225          wrapperOuterIndirection();
226          expect(outerIndirectionTraced).toBe(true);
227
228          wrapperInnerIndirection = SchedulerTracing.unstable_wrap(
229            innerIndirection,
230          );
231
232          innerEventTraced = true;
233        });
234
235        expect(innerEventTraced).toBe(true);
236
237        // Verify that the original event is restored
238        interactions = SchedulerTracing.unstable_getCurrent();
239        expect(interactions).toMatchInteractions([
240          {name: 'outer event', timestamp: 100},
241        ]);
242
243        // Verify that a wrapped nested callback is properly traced
244        wrapperInnerIndirection();
245        expect(innerIndirectionTraced).toBe(true);
246
247        done();
248      });
249    });
250
251    describe('error handling', () => {
252      it('should reset state appropriately when an error occurs in a trace callback', done => {
253        advanceTimeBy(100);
254
255        SchedulerTracing.unstable_trace('outer event', currentTime, () => {
256          expect(() => {
257            SchedulerTracing.unstable_trace('inner event', currentTime, () => {
258              throw Error('intentional');
259            });
260          }).toThrow();
261
262          expect(SchedulerTracing.unstable_getCurrent()).toMatchInteractions([
263            {name: 'outer event', timestamp: 100},
264          ]);
265
266          done();
267        });
268      });
269
270      it('should reset state appropriately when an error occurs in a wrapped callback', done => {
271        advanceTimeBy(100);
272
273        SchedulerTracing.unstable_trace('outer event', currentTime, () => {
274          let wrappedCallback;
275
276          SchedulerTracing.unstable_trace('inner event', currentTime, () => {
277            wrappedCallback = SchedulerTracing.unstable_wrap(() => {
278              throw Error('intentional');
279            });
280          });
281
282          expect(wrappedCallback).toThrow();
283
284          expect(SchedulerTracing.unstable_getCurrent()).toMatchInteractions([
285            {name: 'outer event', timestamp: 100},
286          ]);
287
288          done();
289        });
290      });
291    });
292
293    describe('advanced integration', () => {
294      it('should return a unique threadID per request', () => {
295        expect(SchedulerTracing.unstable_getThreadID()).not.toBe(
296          SchedulerTracing.unstable_getThreadID(),
297        );
298      });
299
300      it('should expose the current set of interactions to be externally manipulated', () => {
301        SchedulerTracing.unstable_trace('outer event', currentTime, () => {
302          expect(SchedulerTracing.__interactionsRef.current).toBe(
303            SchedulerTracing.unstable_getCurrent(),
304          );
305
306          SchedulerTracing.__interactionsRef.current = new Set([
307            {name: 'override event'},
308          ]);
309
310          expect(SchedulerTracing.unstable_getCurrent()).toMatchInteractions([
311            {name: 'override event'},
312          ]);
313        });
314      });
315
316      it('should expose a subscriber ref to be externally manipulated', () => {
317        SchedulerTracing.unstable_trace('outer event', currentTime, () => {
318          expect(SchedulerTracing.__subscriberRef).toEqual({
319            current: null,
320          });
321        });
322      });
323    });
324  });
325
326  describe('enableSchedulerTracing disabled', () => {
327    beforeEach(() => loadModules({enableSchedulerTracing: false}));
328
329    it('should return the value of a traced function', () => {
330      expect(
331        SchedulerTracing.unstable_trace('arbitrary', currentTime, () => 123),
332      ).toBe(123);
333    });
334
335    it('should return the value of a wrapped function', () => {
336      let wrapped;
337      SchedulerTracing.unstable_trace('arbitrary', currentTime, () => {
338        wrapped = SchedulerTracing.unstable_wrap(() => 123);
339      });
340      expect(wrapped()).toBe(123);
341    });
342
343    it('should return null for traced interactions', () => {
344      expect(SchedulerTracing.unstable_getCurrent()).toBe(null);
345    });
346
347    it('should execute traced callbacks', done => {
348      SchedulerTracing.unstable_trace('some event', currentTime, () => {
349        expect(SchedulerTracing.unstable_getCurrent()).toBe(null);
350
351        done();
352      });
353    });
354
355    it('should return the value of a clear function', () => {
356      expect(SchedulerTracing.unstable_clear(() => 123)).toBe(123);
357    });
358
359    it('should execute wrapped callbacks', done => {
360      const wrappedCallback = SchedulerTracing.unstable_wrap(() => {
361        expect(SchedulerTracing.unstable_getCurrent()).toBe(null);
362
363        done();
364      });
365
366      wrappedCallback();
367    });
368
369    describe('advanced integration', () => {
370      it('should not create unnecessary objects', () => {
371        expect(SchedulerTracing.__interactionsRef).toBe(null);
372      });
373    });
374  });
375});
376
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Internal on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)