How to use exceptionToError 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.

Expected.js

Source: Expected.js Github

copy
1import { TO_STRING_TAG } from '../constants/Symbol'
2
3class Expected {
4  constructor({ data, exceptionToError, expectation }) {
5    this.data = data
6    this.expectation = expectation
7    if (!exceptionToError) {
8      throw new Error(
9        'Expected instance must provide a conversion method for converting from Exception to Errors'
10      )
11    }
12    this.exceptionToError = exceptionToError
13  }
14
15  get [TO_STRING_TAG]() {
16    return 'Expected'
17  }
18
19  toError(exception) {
20    return this.exceptionToError(exception, this)
21  }
22}
23
24export default Expected
25
Full Screen

Expected.test.js

Source: Expected.test.js Github

copy
1import Exception from './Exception'
2import Expected from './Expected'
3import anyToStringTag from '../util/anyToStringTag'
4
5describe('js:Expected', () => {
6  describe('constructor', () => {
7    test('correctly constructs the Type instance', () => {
8      const instance = new Expected({
9        data: {},
10        exceptionToError: () => {},
11        expectation: 'foo'
12      })
13      expect(instance).toBeInstanceOf(Expected)
14      expect(instance).toMatchObject({
15        data: {},
16        expectation: 'foo'
17      })
18    })
19  })
20
21  describe('Symbol.toStringTag', () => {
22    test('Returns back a custom StringTag', () => {
23      const instance = new Expected({
24        data: {},
25        exceptionToError: () => {},
26        expectation: 'foo'
27      })
28      expect(anyToStringTag(instance)).toBe('Expected')
29    })
30  })
31
32  describe('toError', () => {
33    test('Returns an Error from the given exception', () => {
34      const error = new Error('foo')
35      const exceptionToError = jest.fn(() => error)
36      const expected = new Expected({
37        data: {},
38        exceptionToError,
39        expectation: 'foo'
40      })
41      const source = function () {}
42      const target = {
43        index: 0,
44        type: 'Argument',
45        value: 'test'
46      }
47      const exception = new Exception({ expected, source, target })
48      const result = expected.toError(exception)
49      expect(result).toBe(error)
50      expect(exceptionToError).toHaveBeenCalledWith(exception, expected)
51    })
52  })
53})
54
Full Screen

buildExpected.js

Source: buildExpected.js Github

copy
1import Expected from '../classes/Expected'
2import TypeError from '../classes/TypeError'
3import anyToName from './anyToName'
4import anyToString from './anyToString'
5import fnToSignatureString from './fnToSignatureString'
6import functionToParameterNames from './functionToParameterNames'
7import sourceToString from './sourceToString'
8
9const prefixNot = (not, expectation) => `${not ? 'not.' : ''}${expectation}`
10
11const targetToString = (target) => `${target.type}:${anyToString(target.value)}`
12
13const toBeEmpty = (next, not = false) => () =>
14  next(
15    new Expected({
16      data: {},
17      exceptionToError: (exception) =>
18        new TypeError(
19          `${sourceToString(exception.source)} expected ${
20            not ? 'at least one Parameter' : 'no Parameters'
21          }.`
22        ),
23      expectation: prefixNot(not, 'toBeEmpty')
24    })
25  )
26
27const toBeInstanceOf = (next, not = false) => (_class) =>
28  next(
29    new Expected({
30      data: {
31        class: _class
32      },
33      exceptionToError: (exception, expected) =>
34        new TypeError(
35          `${sourceToString(exception.source)} expected ${targetToString(
36            exception.target
37          )} to ${not ? 'NOT ' : ''}be an instance of ${anyToName(
38            expected.data.class
39          )}. Instead was given ${targetToString(exception.target)}.`
40        )
41    })
42  )
43
44const toBeOfMinLength = (next, not = false) => (length) =>
45  next(
46    new Expected({
47      data: {
48        length
49      },
50      exceptionToError: (exception, expected) =>
51        new TypeError(
52          `${sourceToString(exception.source)} expected ${targetToString(
53            exception.target
54          )} ${not ? 'NOT ' : ''}to be of minimum length ${
55            expected.data.length
56          }.`
57        ),
58      expectation: prefixNot(not, 'toBeOfMinLength')
59    })
60  )
61
62const toHaveParameters = (next, not = false) => () =>
63  next(
64    new Expected({
65      exceptionToError: (exception) => {
66        let sentence
67        if (exception.source === exception.target.value) {
68          sentence = `${sourceToString(exception.source)} was`
69        } else {
70          sentence = `${sourceToString(
71            exception.source
72          )} expected ${targetToString(exception.target)}`
73        }
74        return new TypeError(
75          `${sentence} ${not ? 'NOT ' : ''}to have Parameters.`
76        )
77      },
78      expectation: prefixNot(not, 'toHaveParameters')
79    })
80  )
81
82const toHaveParametersOrDispatcher = (next, not = false) => () =>
83  next(
84    new Expected({
85      exceptionToError: (exception) => {
86        let sentence
87        if (exception.source === exception.target.value) {
88          sentence = `${sourceToString(exception.source)} was`
89        } else {
90          sentence = `${sourceToString(
91            exception.source
92          )} expected ${targetToString(exception.target)}`
93        }
94        return new TypeError(
95          `${sentence} ${not ? 'NOT ' : ''}to have Parameters or a Dispatcher.`
96        )
97      },
98      expectation: prefixNot(not, 'toHaveParametersOrDispatcher')
99    })
100  )
101
102const toMatchDispatcher = (next, not = false) => (dispatcher) => {
103  if (!dispatcher) {
104    throw new Error('Expected Dispatcher')
105  }
106  return next(
107    new Expected({
108      data: {
109        dispatcher
110      },
111      exceptionToError: (exception, expected) => {
112        const allFns = expected.data.dispatcher.getAllDispatchableFns()
113        if (allFns.isEmpty()) {
114          return new TypeError(
115            `${sourceToString(exception.source)} expected ${targetToString(
116              exception.target
117            )} ${
118              not ? 'NOT ' : ''
119            }to dispatch to at least one Fn. It's possible that no Fns have been defiend for this `
120          )
121        }
122        return new TypeError(
123          `${sourceToString(exception.source)} expected ${targetToString(
124            exception.target
125          )} ${
126            not ? 'NOT ' : ''
127          }to match one of the following method signatures.\n${allFns
128            .map((fn) => `* ${fnToSignatureString(fn)}`)
129            .join('\n')}`
130        )
131      },
132      expectation: prefixNot(not, 'toMatchDispatcher')
133    })
134  )
135}
136
137const toMatchParameter = (next, not = false) => (parameter) =>
138  next(
139    new Expected({
140      data: {
141        parameter
142      },
143      exceptionToError: (exception, expected) =>
144        new TypeError(
145          `${sourceToString(exception.source)} expected ${targetToString(
146            exception.target
147          )} for Parameter ${anyToName(expected.data.parameter)} to ${
148            not ? 'NOT ' : ''
149          }be a ${expected.data.parameter.type}. Instead was given ${
150            exception.target.value
151          }.`
152        ),
153      expectation: prefixNot(not, 'toMatchParameter')
154    })
155  )
156
157const toMatchRegex = (next, not = false) => (regex) =>
158  next(
159    new Expected({
160      data: {
161        regex
162      },
163      exceptionToError: (exception, expected) => {
164        const parameterName = functionToParameterNames(exception.source)[
165          exception.target.index
166        ]
167        return new TypeError(
168          `${sourceToString(exception.source)} expected ${targetToString(
169            exception.target
170          )} for Parameter '${parameterName}' ${
171            not ? 'NOT ' : ''
172          }to match regex ${expected.data.regex}.`
173        )
174      },
175      expectation: prefixNot(not, 'toMatchRegex')
176    })
177  )
178
179const toMatchReturns = (next, not = false) => (returns) =>
180  next(
181    new Expected({
182      data: {
183        returns
184      },
185      exceptionToError: (exception, expected) =>
186        new TypeError(
187          `${sourceToString(exception.source)} expected ${targetToString(
188            exception.target
189          )} value to ${not ? 'NOT ' : ''}return a ${anyToName(
190            expected.data.returns
191          )}. Instead was given ${targetToString(exception.target)}.`
192        ),
193      expectation: prefixNot(not, 'toMatchReturns')
194    })
195  )
196
197const toSatisfyProtocol = (next, not = false) => (protocol) =>
198  next(
199    new Expected({
200      data: {
201        protocol
202      },
203      exceptionToError: (exception, expected) =>
204        new TypeError(
205          `${sourceToString(exception.source)} expected ${targetToString(
206            exception.target
207          )} to ${not ? 'NOT ' : ''}satisfy the Protocol ${anyToName(
208            expected.data.protocol
209          )}. Instead was given ${targetToString(exception.target)}.`
210        )
211    })
212  )
213
214const buildExpected = (type, next) => {
215  switch (type) {
216    case 'Argument':
217      return {
218        not: {
219          toBeInstanceOf: toBeInstanceOf(next, true),
220          toMatchParameter: toMatchParameter(next, true),
221          toMatchRegex: toMatchRegex(next, true),
222          toSatisfyProtocol: toSatisfyProtocol(next, true)
223        },
224        toBeInstanceOf: toBeInstanceOf(next),
225        toMatchParameter: toMatchParameter(next),
226        toMatchRegex: toMatchRegex(next),
227        toSatisfyProtocol: toSatisfyProtocol(next)
228      }
229
230    case 'Arguments':
231      return {
232        not: {
233          toBeEmpty: toBeEmpty(next, true),
234          toBeOfMinLength: toBeOfMinLength(next, true),
235          toMatchDispatcher: toMatchDispatcher(next, true)
236        },
237        toBeEmpty: toBeEmpty(next),
238        toBeOfMinLength: toBeOfMinLength(next),
239        toMatchDispatcher: toMatchDispatcher(next)
240      }
241    case 'Fn':
242      return {
243        not: {
244          toHaveParameters: toHaveParameters(next, true),
245          toHaveParametersOrDispatcher: toHaveParametersOrDispatcher(next, true)
246        },
247        toHaveParameters: toHaveParameters(next),
248        toHaveParametersOrDispatcher: toHaveParametersOrDispatcher(next)
249      }
250    case 'Returned':
251      return {
252        not: {
253          toMatchReturns: toMatchReturns(next, true)
254        },
255        toMatchReturns: toMatchReturns(next)
256      }
257    case 'This':
258      return {
259        not: {
260          toBeInstanceOf: toBeInstanceOf(next, true)
261        },
262        toBeInstanceOf: toBeInstanceOf(next)
263      }
264  }
265  throw new Error(`Unhandled type '${type}'in buildExpected`)
266}
267
268export default buildExpected
269
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)