Skip to content
Open
Prev Previous commit
Next Next commit
Add executionContext weakmap bench attempt
  • Loading branch information
andrewiggins committed Apr 20, 2022
commit 71fe2a82f5560c63a87e43b4db2557873fed6a9d
22 changes: 16 additions & 6 deletions bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
const path = require("path");
const Benchmark = require("benchmark");
const puppeteer = require("puppeteer");
const { queries: currentQueries } = require("./current");
const { queries: newQueries, getDocument } = require("./");
const { queries: baseQueries, getDocument } = require("./versions/base");
const { queries: windowQueries } = require("./versions/window");
const { queries: execQueries } = require("./versions/executionContext");

var suite = new Benchmark.Suite("pptr-testing-library");

Expand All @@ -17,19 +18,28 @@ async function main() {
// add tests
suite
.add({
name: "current findByRole",
name: "base",
defer: true,
fn(deferred) {
currentQueries
baseQueries
.getByText(document, "Hello h1")
.then(() => deferred.resolve());
},
})
.add({
name: "new findByRole",
name: "window check",
defer: true,
fn(deferred) {
newQueries
windowQueries
.getByText(document, "Hello h1")
.then(() => deferred.resolve());
},
})
.add({
name: "execution context",
defer: true,
fn(deferred) {
execQueries
.getByText(document, "Hello h1")
.then(() => deferred.resolve());
},
Expand Down
15 changes: 11 additions & 4 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {readFileSync} from 'fs'
import * as path from 'path'
import {ElementHandle, EvaluateFn, JSHandle, Page} from 'puppeteer'
import {ElementHandle, EvaluateFn, ExecutionContext, JSHandle, Page} from 'puppeteer'
import waitForExpect from 'wait-for-expect'

import {IConfigureOptions, IQueryUtils, IScopedQueryUtils} from './typedefs'
Expand Down Expand Up @@ -117,6 +117,8 @@ interface IHandleSet {
argsToForward: any[]
}

const executionContextInit = new WeakMap<ExecutionContext, boolean>();

function createDelegateFor<T = DOMReturnType>(
fnName: keyof IQueryUtils,
contextFn?: ContextFn,
Expand All @@ -129,9 +131,14 @@ function createDelegateFor<T = DOMReturnType>(
// @ts-ignore
const containerHandle: ElementHandle = contextFn ? contextFn.apply(this, args) : this

const shouldIncludeTestingLib = await containerHandle.evaluate(
() => (window as any).__dom_testing_library__ == null
);
// const shouldIncludeTestingLib = await containerHandle.evaluate(
// () => (window as any).__dom_testing_library__ == null
// );
const executionContext = containerHandle.executionContext();
const shouldIncludeTestingLib = !executionContextInit.has(executionContext);
if (shouldIncludeTestingLib) {
executionContextInit.set(executionContext, true);
}

// @ts-ignore
const evaluateFn: EvaluateFn = new Function(
Expand Down
File renamed without changes.
210 changes: 210 additions & 0 deletions versions/executionContext.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading