Skip to content

Commit e9cafb1

Browse files
authored
fix(expect-puppeteer): return proper error stack traces from matchers (#487)
1 parent d9dc28f commit e9cafb1

File tree

9 files changed

+43
-18
lines changed

9 files changed

+43
-18
lines changed

packages/expect-puppeteer/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function createMatcher(matcher, page) {
5151
try {
5252
return await matcher(page, ...args)
5353
} catch (error) {
54-
Error.captureStackTrace(error, createMatcher)
54+
Error.captureStackTrace(error, throwingMatcher)
5555
throw error
5656
}
5757
}

packages/expect-puppeteer/src/matchers/notToMatch.test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from "path";
12
import { setupPage } from './setupPage'
23

34
describe('not.toMatch', () => {
@@ -22,13 +23,14 @@ describe('not.toMatch', () => {
2223
})
2324

2425
it('should return an error if text is in the page', async () => {
25-
expect.assertions(3)
26+
expect.assertions(4)
2627

2728
try {
2829
await expect(page).not.toMatch('home', options)
2930
} catch (error) {
3031
expect(error.message).toMatch('Text found "home"')
3132
expect(error.message).toMatch('waiting for function failed')
33+
expect(error.stack).toMatch(path.resolve(__filename))
3234
}
3335
})
3436

@@ -39,14 +41,15 @@ describe('not.toMatch', () => {
3941
})
4042

4143
it('should return an error if text is not in the page', async () => {
42-
expect.assertions(3)
44+
expect.assertions(4)
4345
const dialogBtn = await page.$('#dialog-btn')
4446

4547
try {
4648
await expect(dialogBtn).not.toMatch('Open dialog', options)
4749
} catch (error) {
4850
expect(error.message).toMatch('Text found "Open dialog"')
4951
expect(error.message).toMatch('waiting for function failed')
52+
expect(error.stack).toMatch(path.resolve(__filename))
5053
}
5154
})
5255
})

packages/expect-puppeteer/src/matchers/notToMatchElement.test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from "path";
12
import { setupPage } from './setupPage'
23

34
describe('not.toMatchElement', () => {
@@ -19,13 +20,14 @@ describe('not.toMatchElement', () => {
1920
})
2021

2122
it('should return an error if element is not found', async () => {
22-
expect.assertions(3)
23+
expect.assertions(4)
2324

2425
try {
2526
await expect(page).not.toMatchElement('a', { text: 'Page 2' })
2627
} catch (error) {
2728
expect(error.message).toMatch('Element a (text: "Page 2") found')
2829
expect(error.message).toMatch('waiting for function failed')
30+
expect(error.stack).toMatch(path.resolve(__filename))
2931
}
3032
})
3133
})
@@ -43,13 +45,14 @@ describe('not.toMatchElement', () => {
4345

4446
it('should return an error if element is not found', async () => {
4547
const main = await page.$('main')
46-
expect.assertions(3)
48+
expect.assertions(4)
4749

4850
try {
4951
await expect(main).not.toMatchElement('div', { text: 'main' })
5052
} catch (error) {
5153
expect(error.message).toMatch('Element div (text: "main") found')
5254
expect(error.message).toMatch('waiting for function failed')
55+
expect(error.stack).toMatch(path.resolve(__filename))
5356
}
5457
})
5558
})

packages/expect-puppeteer/src/matchers/toClick.test.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from "path";
12
import { setupPage } from './setupPage'
23

34
describe('toClick', () => {
@@ -71,17 +72,18 @@ describe('toClick', () => {
7172
})
7273

7374
it('should return an error if element is not found', async () => {
74-
expect.assertions(2)
75+
expect.assertions(3)
7576

7677
try {
7778
await expect(page).toClick('a', { text: 'Nop' })
7879
} catch (error) {
7980
expect(error.message).toMatch('Element a (text: "Nop") not found')
81+
expect(error.stack).toMatch(path.resolve(__filename))
8082
}
8183
})
8284

8385
it('should return an error if element is not found with xpath selector', async () => {
84-
expect.assertions(2)
86+
expect.assertions(3)
8587

8688
try {
8789
await expect(page).toClick(
@@ -90,16 +92,18 @@ describe('toClick', () => {
9092
)
9193
} catch (error) {
9294
expect(error.message).toMatch('Element //a (text: "Nop") not found')
95+
expect(error.stack).toMatch(path.resolve(__filename))
9396
}
9497
})
9598

9699
it('should return an error if element is not found with css selector as object', async () => {
97-
expect.assertions(2)
100+
expect.assertions(3)
98101

99102
try {
100103
await expect(page).toClick({ value: 'a', type: 'css' }, { text: 'Nop' })
101104
} catch (error) {
102105
expect(error.message).toMatch('Element a (text: "Nop") not found')
106+
expect(error.stack).toMatch(path.resolve(__filename))
103107
}
104108
})
105109
})
@@ -134,12 +138,13 @@ describe('toClick', () => {
134138

135139
it('should return an error if element is not found', async () => {
136140
const body = await page.$('body')
137-
expect.assertions(2)
141+
expect.assertions(3)
138142

139143
try {
140144
await expect(body).toClick('a', { text: 'Nop' })
141145
} catch (error) {
142146
expect(error.message).toMatch('Element a (text: "Nop") not found')
147+
expect(error.stack).toMatch(path.resolve(__filename))
143148
}
144149
})
145150
})

packages/expect-puppeteer/src/matchers/toFill.test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from "path";
12
import { setupPage } from './setupPage'
23

34
describe('toFill', () => {
@@ -68,12 +69,13 @@ describe('toFill', () => {
6869
})
6970

7071
it('should return an error if text is not in the page', async () => {
71-
expect.assertions(2)
72+
expect.assertions(3)
7273

7374
try {
7475
await expect(page).toFill('[name="notFound"]', 'James')
7576
} catch (error) {
7677
expect(error.message).toMatch('Element [name="notFound"] not found')
78+
expect(error.stack).toMatch(path.resolve(__filename))
7779
}
7880
})
7981
})
@@ -101,12 +103,13 @@ describe('toFill', () => {
101103

102104
it('should return an error if text is not in the page', async () => {
103105
const body = await page.$('body')
104-
expect.assertions(2)
106+
expect.assertions(3)
105107

106108
try {
107109
await expect(body).toFill('[name="notFound"]', 'James')
108110
} catch (error) {
109111
expect(error.message).toMatch('Element [name="notFound"] not found')
112+
expect(error.stack).toMatch(path.resolve(__filename))
110113
}
111114
})
112115
})

packages/expect-puppeteer/src/matchers/toFillForm.test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from "path";
12
import { setupPage } from './setupPage'
23

34
describe('toFillForm', () => {
@@ -26,7 +27,7 @@ describe('toFillForm', () => {
2627
})
2728

2829
it('should return an error if text is not in the page', async () => {
29-
expect.assertions(2)
30+
expect.assertions(3)
3031

3132
try {
3233
await expect(page).toFillForm('form[name="notFound"]', {
@@ -35,6 +36,7 @@ describe('toFillForm', () => {
3536
})
3637
} catch (error) {
3738
expect(error.message).toMatch('Element form[name="notFound"] not found')
39+
expect(error.stack).toMatch(path.resolve(__filename))
3840
}
3941
})
4042
})
@@ -58,7 +60,7 @@ describe('toFillForm', () => {
5860

5961
it('should return an error if text is not in the page', async () => {
6062
const body = await page.$('body')
61-
expect.assertions(2)
63+
expect.assertions(3)
6264

6365
try {
6466
await expect(body).toFillForm('form[name="notFound"]', {
@@ -67,6 +69,7 @@ describe('toFillForm', () => {
6769
})
6870
} catch (error) {
6971
expect(error.message).toMatch('Element form[name="notFound"] not found')
72+
expect(error.stack).toMatch(path.resolve(__filename))
7073
}
7174
})
7275
})

packages/expect-puppeteer/src/matchers/toMatch.test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from "path";
12
import { setupPage } from './setupPage'
23

34
describe('toMatch', () => {
@@ -26,13 +27,14 @@ describe('toMatch', () => {
2627
})
2728

2829
it('should return an error if text is not in the page', async () => {
29-
expect.assertions(3)
30+
expect.assertions(4)
3031

3132
try {
3233
await expect(page).toMatch('Nop', options)
3334
} catch (error) {
3435
expect(error.message).toMatch('Text not found "Nop"')
3536
expect(error.message).toMatch('waiting for function failed')
37+
expect(error.stack).toMatch(path.resolve(__filename))
3638
}
3739
})
3840

@@ -48,14 +50,15 @@ describe('toMatch', () => {
4850
})
4951

5052
it('should return an error if text is not in the page', async () => {
51-
expect.assertions(3)
53+
expect.assertions(4)
5254
const dialogBtn = await page.$('#dialog-btn')
5355

5456
try {
5557
await expect(dialogBtn).toMatch('This is home!', options)
5658
} catch (error) {
5759
expect(error.message).toMatch('Text not found "This is home!"')
5860
expect(error.message).toMatch('waiting for function failed')
61+
expect(error.stack).toMatch(path.resolve(__filename))
5962
}
6063
})
6164
})

packages/expect-puppeteer/src/matchers/toMatchElement.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from "path";
12
import { setupPage } from './setupPage'
23

34
describe('toMatchElement', () => {
@@ -36,13 +37,14 @@ describe('toMatchElement', () => {
3637
})
3738

3839
it('should return an error if element is not found', async () => {
39-
expect.assertions(3)
40+
expect.assertions(4)
4041

4142
try {
4243
await expect(page).toMatchElement('a', { text: 'Nop' })
4344
} catch (error) {
4445
expect(error.message).toMatch('Element a (text: "Nop") not found')
4546
expect(error.message).toMatch('waiting for function failed')
47+
expect(error.stack).toMatch(path.resolve(__filename))
4648
}
4749
})
4850

packages/expect-puppeteer/src/matchers/toSelect.test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from "path";
12
import { setupPage } from './setupPage'
23

34
describe('toSelect', () => {
@@ -27,14 +28,15 @@ describe('toSelect', () => {
2728
})
2829

2930
it('should return an error if option is not found', async () => {
30-
expect.assertions(2)
31+
expect.assertions(3)
3132

3233
try {
3334
await expect(page).toSelect('select[name="my-select"]', 'Another world')
3435
} catch (error) {
3536
expect(error.message).toMatch(
3637
'Option not found "select[name="my-select"]" ("Another world")',
3738
)
39+
expect(error.stack).toMatch(path.resolve(__filename))
3840
}
3941
})
4042
})
@@ -60,14 +62,15 @@ describe('toSelect', () => {
6062

6163
it('should return an error if option is not found', async () => {
6264
const body = await page.$('body')
63-
expect.assertions(2)
65+
expect.assertions(3)
6466

6567
try {
6668
await expect(body).toSelect('select[name="my-select"]', 'Another world')
6769
} catch (error) {
6870
expect(error.message).toMatch(
6971
'Option not found "select[name="my-select"]" ("Another world")',
7072
)
73+
expect(error.stack).toMatch(path.resolve(__filename))
7174
}
7275
})
7376
})

0 commit comments

Comments
 (0)