|
1 | 1 | import type {Locator, PlaywrightTestArgs, TestFixture} from '@playwright/test'
|
2 | 2 | import {Page, selectors} from '@playwright/test'
|
3 | 3 |
|
| 4 | +import type {TestingLibraryDeserializedFunction as DeserializedFunction} from '../helpers' |
4 | 5 | import type {
|
5 | 6 | Config,
|
6 | 7 | LocatorQueries as Queries,
|
@@ -52,38 +53,71 @@ const withinFixture: TestFixture<Within, TestArguments> = async (
|
52 | 53 | : (queriesFor(root, {asyncUtilExpectedState, asyncUtilTimeout}) as WithinReturn<Root>),
|
53 | 54 | )
|
54 | 55 |
|
55 |
| -declare const queryName: SynchronousQuery |
56 |
| - |
57 |
| -const engine: () => SelectorEngine = () => ({ |
58 |
| - query(root, selector) { |
59 |
| - const args = JSON.parse(selector, window.__testingLibraryReviver) as unknown as Parameters< |
60 |
| - Queries[typeof queryName] |
61 |
| - > |
| 56 | +type SynchronousQueryParameters = Parameters<Queries[SynchronousQuery]> |
62 | 57 |
|
63 |
| - if (isAllQuery(queryName)) |
64 |
| - throw new Error( |
65 |
| - `PlaywrightTestingLibrary: the plural '${queryName}' was used to create this Locator`, |
| 58 | +declare const queryName: SynchronousQuery |
| 59 | +declare class TestingLibraryDeserializedFunction extends DeserializedFunction {} |
| 60 | + |
| 61 | +const engine: () => SelectorEngine = () => { |
| 62 | + const getError = (error: unknown, matcher: SynchronousQueryParameters[0]) => { |
| 63 | + if (typeof matcher === 'function' && error instanceof ReferenceError) { |
| 64 | + return new ReferenceError( |
| 65 | + [ |
| 66 | + error.message, |
| 67 | + '\n⚠️ A ReferenceError was thrown when using a function TextMatch, did you reference external scope in your matcher function?', |
| 68 | + '\nProvided matcher function:', |
| 69 | + matcher instanceof TestingLibraryDeserializedFunction |
| 70 | + ? matcher.original |
| 71 | + : matcher.toString(), |
| 72 | + '\n', |
| 73 | + ].join('\n'), |
66 | 74 | )
|
| 75 | + } |
67 | 76 |
|
68 |
| - // @ts-expect-error |
69 |
| - const result = window.TestingLibraryDom[queryName](root, ...args) |
70 |
| - |
71 |
| - return result |
72 |
| - }, |
73 |
| - queryAll(root, selector) { |
74 |
| - const testingLibrary = window.TestingLibraryDom |
75 |
| - const args = JSON.parse(selector, window.__testingLibraryReviver) as unknown as Parameters< |
76 |
| - Queries[typeof queryName] |
77 |
| - > |
78 |
| - |
79 |
| - // @ts-expect-error |
80 |
| - const result = testingLibrary[queryName](root, ...args) |
81 |
| - |
82 |
| - if (!result) return [] |
83 |
| - |
84 |
| - return Array.isArray(result) ? result : [result] |
85 |
| - }, |
86 |
| -}) |
| 77 | + return error |
| 78 | + } |
| 79 | + |
| 80 | + return { |
| 81 | + query(root, selector) { |
| 82 | + const args = JSON.parse( |
| 83 | + selector, |
| 84 | + window.__testingLibraryReviver, |
| 85 | + ) as unknown as SynchronousQueryParameters |
| 86 | + |
| 87 | + if (isAllQuery(queryName)) |
| 88 | + throw new Error( |
| 89 | + `PlaywrightTestingLibrary: the plural '${queryName}' was used to create this Locator`, |
| 90 | + ) |
| 91 | + |
| 92 | + try { |
| 93 | + // @ts-expect-error |
| 94 | + const result = window.TestingLibraryDom[queryName](root, ...args) |
| 95 | + |
| 96 | + return result |
| 97 | + } catch (error) { |
| 98 | + throw getError(error, args[0]) |
| 99 | + } |
| 100 | + }, |
| 101 | + queryAll(root, selector) { |
| 102 | + const testingLibrary = window.TestingLibraryDom |
| 103 | + const args = JSON.parse( |
| 104 | + selector, |
| 105 | + window.__testingLibraryReviver, |
| 106 | + ) as unknown as SynchronousQueryParameters |
| 107 | + |
| 108 | + try { |
| 109 | + // @ts-expect-error |
| 110 | + const result = testingLibrary[queryName](root, ...args) |
| 111 | + |
| 112 | + if (!result) return [] |
| 113 | + |
| 114 | + return Array.isArray(result) ? result : [result] |
| 115 | + } catch (error) { |
| 116 | + throw getError(error, args[0]) |
| 117 | + } |
| 118 | + }, |
| 119 | + } |
| 120 | +} |
87 | 121 |
|
88 | 122 | const registerSelectorsFixture: [
|
89 | 123 | TestFixture<void, PlaywrightTestArgs>,
|
|
0 commit comments