Skip to content

Commit

Permalink
Avoid unnecessart type casting
Browse files Browse the repository at this point in the history
  • Loading branch information
WunderBart committed Sep 21, 2023
1 parent 693caec commit 33a78eb
Showing 1 changed file with 15 additions and 24 deletions.
39 changes: 15 additions & 24 deletions packages/e2e-test-utils-playwright/src/metrics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,6 @@ interface Trace {
traceEvents: TraceEvent[];
}

interface LoadingDurations {
serverResponse: number;
firstPaint: number;
domContentLoaded: number;
loaded: number;
firstContentfulPaint: number;
timeSinceResponseEnd: number;
}

type MetricsConstructorProps = {
page: Page;
};
Expand All @@ -58,7 +49,7 @@ export class Metrics {
* @param fields Optional fields to filter.
*/
async getServerTiming( fields: string[] = [] ) {
return this.page.evaluate< Record< string, number >, string[] >(
return this.page.evaluate(
( f: string[] ) =>
(
performance.getEntriesByType(
Expand All @@ -82,8 +73,8 @@ export class Metrics {
*
* @see https://web.dev/ttfb/#measure-ttfb-in-javascript
*/
async getTimeToFirstByte(): Promise< number > {
return await this.page.evaluate< number >( () => {
async getTimeToFirstByte() {
return await this.page.evaluate( () => {
const { responseStart, startTime } = (
performance.getEntriesByType(
'navigation'
Expand All @@ -99,8 +90,8 @@ export class Metrics {
* @see https://w3c.github.io/largest-contentful-paint/
* @see https://web.dev/lcp/#measure-lcp-in-javascript
*/
async getLargestContentfulPaint(): Promise< number > {
return await this.page.evaluate< number >(
async getLargestContentfulPaint() {
return await this.page.evaluate(
() =>
new Promise( ( resolve ) => {
new PerformanceObserver( ( entryList ) => {
Expand All @@ -123,8 +114,8 @@ export class Metrics {
* @see https://github.com/WICG/layout-instability
* @see https://web.dev/cls/#measure-layout-shifts-in-javascript
*/
async getCumulativeLayoutShift(): Promise< number > {
return await this.page.evaluate< number >(
async getCumulativeLayoutShift() {
return await this.page.evaluate(
() =>
new Promise( ( resolve ) => {
let CLS = 0;
Expand All @@ -151,7 +142,7 @@ export class Metrics {
* Returns the loading durations using the Navigation Timing API. All the
* durations exclude the server response time.
*/
async getLoadingDurations(): Promise< LoadingDurations > {
async getLoadingDurations() {
return await this.page.evaluate( () => {
const [
{
Expand Down Expand Up @@ -196,7 +187,7 @@ export class Metrics {
*
* @param options Options to pass to `browser.startTracing()`.
*/
async startTracing( options = {} ): Promise< void > {
async startTracing( options = {} ) {
return await this.browser.startTracing( this.page, {
screenshots: false,
categories: [ 'devtools.timeline' ],
Expand All @@ -207,7 +198,7 @@ export class Metrics {
/**
* Stops Chromium tracing and saves the trace.
*/
async stopTracing(): Promise< void > {
async stopTracing() {
const traceBuffer = await this.browser.stopTracing();
const traceJSON = JSON.parse( traceBuffer.toString() );

Expand All @@ -217,7 +208,7 @@ export class Metrics {
/**
* Returns the durations of all typing events.
*/
getTypingEventDurations(): number[][] {
getTypingEventDurations() {
return [
this.getEventDurations( 'keydown' ),
this.getEventDurations( 'keypress' ),
Expand All @@ -228,7 +219,7 @@ export class Metrics {
/**
* Returns the durations of all selection events.
*/
getSelectionEventDurations(): number[][] {
getSelectionEventDurations() {
return [
this.getEventDurations( 'focus' ),
this.getEventDurations( 'focusin' ),
Expand All @@ -238,14 +229,14 @@ export class Metrics {
/**
* Returns the durations of all click events.
*/
getClickEventDurations(): number[][] {
getClickEventDurations() {
return [ this.getEventDurations( 'click' ) ];
}

/**
* Returns the durations of all hover events.
*/
getHoverEventDurations(): number[][] {
getHoverEventDurations() {
return [
this.getEventDurations( 'mouseover' ),
this.getEventDurations( 'mouseout' ),
Expand All @@ -257,7 +248,7 @@ export class Metrics {
*
* @param eventType The type of event to filter.
*/
getEventDurations( eventType: EventType ): number[] {
getEventDurations( eventType: EventType ) {
if ( this.trace.traceEvents.length === 0 ) {
throw new Error(
'No trace events found. Did you forget to call stopTracing()?'
Expand Down

0 comments on commit 33a78eb

Please sign in to comment.