@vitest/browser-playwright 5.0.0-beta.1 → 5.0.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +38 -20
  2. package/package.json +5 -5
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { asLocator, parseKeyDef, resolveScreenshotPath, defineBrowserProvider } from '@vitest/browser';
1
+ import { parseKeyDef, resolveScreenshotPath, defineBrowserProvider } from '@vitest/browser';
2
2
  export { defineBrowserCommand } from '@vitest/browser';
3
3
  import { createManualModuleSource } from '@vitest/mocker/node';
4
4
  import c from 'tinyrainbow';
@@ -173,9 +173,9 @@ const basename = function(p, extension) {
173
173
  // - locator('[data-vitest="true"]').contentFrame().getByRole('button')
174
174
  // ⇓
175
175
  // - getByRole('button')
176
- function getDescribedLocator(context, selector) {
177
- const locator = context.iframe.locator(selector);
178
- return typeof locator.describe === "function" ? locator.describe(asLocator("javascript", selector)) : locator;
176
+ function getDescribedLocator(context, { locator, selector }) {
177
+ const iframeLocator = context.iframe.locator(selector);
178
+ return typeof iframeLocator.describe === "function" ? iframeLocator.describe(locator) : iframeLocator;
179
179
  }
180
180
 
181
181
  const clear = async (context, selector) => {
@@ -198,7 +198,7 @@ const tripleClick = async (context, selector, options = {}) => {
198
198
 
199
199
  const dragAndDrop = async (context, source, target, options_) => {
200
200
  const frame = await context.frame();
201
- await frame.dragAndDrop(source, target, options_);
201
+ await frame.dragAndDrop(source.selector, target.selector, options_);
202
202
  };
203
203
 
204
204
  const fill = async (context, selector, text, options = {}) => {
@@ -527,7 +527,7 @@ async function takeScreenshot(context, name, options) {
527
527
  const mask = options.mask?.map((selector) => getDescribedLocator(context, selector));
528
528
  const style = context.project.config.browser.ui ? options.style === undefined ? SCREENSHOT_STYLES : SCREENSHOT_STYLES + options.style : options.style;
529
529
  if (options.element) {
530
- const { element: selector, ...config } = options;
530
+ const { element: selector, target: _target, ...config } = options;
531
531
  const element = getDescribedLocator(context, selector);
532
532
  const buffer = await element.screenshot({
533
533
  ...config,
@@ -540,8 +540,17 @@ async function takeScreenshot(context, name, options) {
540
540
  path
541
541
  };
542
542
  }
543
- const buffer = await getDescribedLocator(context, "body").screenshot({
544
- ...options,
543
+ const { target, ...config } = options;
544
+ const buffer = target === "page" ? await context.page.screenshot({
545
+ ...config,
546
+ mask,
547
+ path: savePath,
548
+ style
549
+ }) : await getDescribedLocator(context, {
550
+ selector: "body",
551
+ locator: "locator('body')"
552
+ }).screenshot({
553
+ ...config,
545
554
  mask,
546
555
  path: savePath,
547
556
  style
@@ -627,14 +636,14 @@ const markTrace = async (context, payload) => {
627
636
  if (!context.provider.tracingContexts.has(context.sessionId)) {
628
637
  return;
629
638
  }
630
- const { name, selector, stack } = payload;
639
+ const { name, element, stack } = payload;
631
640
  const location = parseLocation(context, stack);
632
641
  // mark trace via group/groupEnd with dummy calls to force snapshot.
633
642
  // https://github.com/microsoft/playwright/issues/39308
634
643
  await context.context.tracing.group(name, { location });
635
644
  try {
636
- if (selector) {
637
- const locator = getDescribedLocator(context, selector);
645
+ if (element) {
646
+ const locator = getDescribedLocator(context, element);
638
647
  if (typeof locator._expect === "function") {
639
648
  await locator._expect("to.be.attached", {
640
649
  isNot: false,
@@ -952,7 +961,7 @@ class PlaywrightBrowserProvider {
952
961
  createMocker() {
953
962
  const idPredicates = new Map();
954
963
  const sessionIds = new Map();
955
- function createPredicate(sessionId, url) {
964
+ function createPredicate(url) {
956
965
  const moduleUrl = new URL(url, "http://localhost");
957
966
  const predicate = (url) => {
958
967
  if (url.searchParams.has("_vitest_original")) {
@@ -977,11 +986,10 @@ class PlaywrightBrowserProvider {
977
986
  }
978
987
  return true;
979
988
  };
980
- const ids = sessionIds.get(sessionId) || [];
981
- ids.push(moduleUrl.href);
982
- sessionIds.set(sessionId, ids);
983
- idPredicates.set(predicateKey(sessionId, moduleUrl.href), predicate);
984
- return predicate;
989
+ return {
990
+ url: moduleUrl.href,
991
+ predicate
992
+ };
985
993
  }
986
994
  function predicateKey(sessionId, url) {
987
995
  return `${sessionId}:${url}`;
@@ -989,7 +997,17 @@ class PlaywrightBrowserProvider {
989
997
  return {
990
998
  register: async (sessionId, module) => {
991
999
  const page = this.getPage(sessionId);
992
- await page.context().route(createPredicate(sessionId, module.url), async (route) => {
1000
+ const { url: moduleUrl, predicate } = createPredicate(module.url);
1001
+ const key = predicateKey(sessionId, moduleUrl);
1002
+ const existingPredicate = idPredicates.get(key);
1003
+ if (existingPredicate) {
1004
+ await page.context().unroute(existingPredicate);
1005
+ }
1006
+ const ids = sessionIds.get(sessionId) ?? new Set();
1007
+ ids.add(moduleUrl);
1008
+ sessionIds.set(sessionId, ids);
1009
+ idPredicates.set(key, predicate);
1010
+ await page.context().route(predicate, async (route) => {
993
1011
  if (module.type === "manual") {
994
1012
  const exports$1 = Object.keys(await module.resolve());
995
1013
  const body = createManualModuleSource(module.url, exports$1);
@@ -1050,8 +1068,8 @@ class PlaywrightBrowserProvider {
1050
1068
  },
1051
1069
  clear: async (sessionId) => {
1052
1070
  const page = this.getPage(sessionId);
1053
- const ids = sessionIds.get(sessionId) || [];
1054
- const promises = ids.map((id) => {
1071
+ const ids = sessionIds.get(sessionId) ?? new Set();
1072
+ const promises = [...ids].map((id) => {
1055
1073
  const key = predicateKey(sessionId, id);
1056
1074
  const predicate = idPredicates.get(key);
1057
1075
  if (predicate) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitest/browser-playwright",
3
3
  "type": "module",
4
- "version": "5.0.0-beta.1",
4
+ "version": "5.0.0-beta.3",
5
5
  "description": "Browser running for Vitest using playwright",
6
6
  "license": "MIT",
7
7
  "funding": "https://opencollective.com/vitest",
@@ -42,7 +42,7 @@
42
42
  ],
43
43
  "peerDependencies": {
44
44
  "playwright": "*",
45
- "vitest": "5.0.0-beta.1"
45
+ "vitest": "5.0.0-beta.3"
46
46
  },
47
47
  "peerDependenciesMeta": {
48
48
  "playwright": {
@@ -51,12 +51,12 @@
51
51
  },
52
52
  "dependencies": {
53
53
  "tinyrainbow": "^3.1.0",
54
- "@vitest/mocker": "5.0.0-beta.1",
55
- "@vitest/browser": "5.0.0-beta.1"
54
+ "@vitest/browser": "5.0.0-beta.3",
55
+ "@vitest/mocker": "5.0.0-beta.3"
56
56
  },
57
57
  "devDependencies": {
58
58
  "playwright": "^1.59.0",
59
- "vitest": "5.0.0-beta.1"
59
+ "vitest": "5.0.0-beta.3"
60
60
  },
61
61
  "scripts": {
62
62
  "build": "premove dist && pnpm rollup -c",