Skip to content

Commit

Permalink
fix(expect-puppeteer): return proper error stack traces from matchers (
Browse files Browse the repository at this point in the history
  • Loading branch information
OriMarron committed Jun 7, 2022
1 parent d9dc28f commit e9cafb1
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/expect-puppeteer/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function createMatcher(matcher, page) {
try {
return await matcher(page, ...args)
} catch (error) {
Error.captureStackTrace(error, createMatcher)
Error.captureStackTrace(error, throwingMatcher)
throw error
}
}
Expand Down
7 changes: 5 additions & 2 deletions packages/expect-puppeteer/src/matchers/notToMatch.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from "path";
import { setupPage } from './setupPage'

describe('not.toMatch', () => {
Expand All @@ -22,13 +23,14 @@ describe('not.toMatch', () => {
})

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

try {
await expect(page).not.toMatch('home', options)
} catch (error) {
expect(error.message).toMatch('Text found "home"')
expect(error.message).toMatch('waiting for function failed')
expect(error.stack).toMatch(path.resolve(__filename))
}
})

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

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

try {
await expect(dialogBtn).not.toMatch('Open dialog', options)
} catch (error) {
expect(error.message).toMatch('Text found "Open dialog"')
expect(error.message).toMatch('waiting for function failed')
expect(error.stack).toMatch(path.resolve(__filename))
}
})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from "path";
import { setupPage } from './setupPage'

describe('not.toMatchElement', () => {
Expand All @@ -19,13 +20,14 @@ describe('not.toMatchElement', () => {
})

it('should return an error if element is not found', async () => {
expect.assertions(3)
expect.assertions(4)

try {
await expect(page).not.toMatchElement('a', { text: 'Page 2' })
} catch (error) {
expect(error.message).toMatch('Element a (text: "Page 2") found')
expect(error.message).toMatch('waiting for function failed')
expect(error.stack).toMatch(path.resolve(__filename))
}
})
})
Expand All @@ -43,13 +45,14 @@ describe('not.toMatchElement', () => {

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

try {
await expect(main).not.toMatchElement('div', { text: 'main' })
} catch (error) {
expect(error.message).toMatch('Element div (text: "main") found')
expect(error.message).toMatch('waiting for function failed')
expect(error.stack).toMatch(path.resolve(__filename))
}
})
})
Expand Down
13 changes: 9 additions & 4 deletions packages/expect-puppeteer/src/matchers/toClick.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from "path";
import { setupPage } from './setupPage'

describe('toClick', () => {
Expand Down Expand Up @@ -71,17 +72,18 @@ describe('toClick', () => {
})

it('should return an error if element is not found', async () => {
expect.assertions(2)
expect.assertions(3)

try {
await expect(page).toClick('a', { text: 'Nop' })
} catch (error) {
expect(error.message).toMatch('Element a (text: "Nop") not found')
expect(error.stack).toMatch(path.resolve(__filename))
}
})

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

try {
await expect(page).toClick(
Expand All @@ -90,16 +92,18 @@ describe('toClick', () => {
)
} catch (error) {
expect(error.message).toMatch('Element //a (text: "Nop") not found')
expect(error.stack).toMatch(path.resolve(__filename))
}
})

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

try {
await expect(page).toClick({ value: 'a', type: 'css' }, { text: 'Nop' })
} catch (error) {
expect(error.message).toMatch('Element a (text: "Nop") not found')
expect(error.stack).toMatch(path.resolve(__filename))
}
})
})
Expand Down Expand Up @@ -134,12 +138,13 @@ describe('toClick', () => {

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

try {
await expect(body).toClick('a', { text: 'Nop' })
} catch (error) {
expect(error.message).toMatch('Element a (text: "Nop") not found')
expect(error.stack).toMatch(path.resolve(__filename))
}
})
})
Expand Down
7 changes: 5 additions & 2 deletions packages/expect-puppeteer/src/matchers/toFill.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from "path";
import { setupPage } from './setupPage'

describe('toFill', () => {
Expand Down Expand Up @@ -68,12 +69,13 @@ describe('toFill', () => {
})

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

try {
await expect(page).toFill('[name="notFound"]', 'James')
} catch (error) {
expect(error.message).toMatch('Element [name="notFound"] not found')
expect(error.stack).toMatch(path.resolve(__filename))
}
})
})
Expand Down Expand Up @@ -101,12 +103,13 @@ describe('toFill', () => {

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

try {
await expect(body).toFill('[name="notFound"]', 'James')
} catch (error) {
expect(error.message).toMatch('Element [name="notFound"] not found')
expect(error.stack).toMatch(path.resolve(__filename))
}
})
})
Expand Down
7 changes: 5 additions & 2 deletions packages/expect-puppeteer/src/matchers/toFillForm.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from "path";
import { setupPage } from './setupPage'

describe('toFillForm', () => {
Expand Down Expand Up @@ -26,7 +27,7 @@ describe('toFillForm', () => {
})

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

try {
await expect(page).toFillForm('form[name="notFound"]', {
Expand All @@ -35,6 +36,7 @@ describe('toFillForm', () => {
})
} catch (error) {
expect(error.message).toMatch('Element form[name="notFound"] not found')
expect(error.stack).toMatch(path.resolve(__filename))
}
})
})
Expand All @@ -58,7 +60,7 @@ describe('toFillForm', () => {

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

try {
await expect(body).toFillForm('form[name="notFound"]', {
Expand All @@ -67,6 +69,7 @@ describe('toFillForm', () => {
})
} catch (error) {
expect(error.message).toMatch('Element form[name="notFound"] not found')
expect(error.stack).toMatch(path.resolve(__filename))
}
})
})
Expand Down
7 changes: 5 additions & 2 deletions packages/expect-puppeteer/src/matchers/toMatch.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from "path";
import { setupPage } from './setupPage'

describe('toMatch', () => {
Expand Down Expand Up @@ -26,13 +27,14 @@ describe('toMatch', () => {
})

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

try {
await expect(page).toMatch('Nop', options)
} catch (error) {
expect(error.message).toMatch('Text not found "Nop"')
expect(error.message).toMatch('waiting for function failed')
expect(error.stack).toMatch(path.resolve(__filename))
}
})

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

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

try {
await expect(dialogBtn).toMatch('This is home!', options)
} catch (error) {
expect(error.message).toMatch('Text not found "This is home!"')
expect(error.message).toMatch('waiting for function failed')
expect(error.stack).toMatch(path.resolve(__filename))
}
})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from "path";
import { setupPage } from './setupPage'

describe('toMatchElement', () => {
Expand Down Expand Up @@ -36,13 +37,14 @@ describe('toMatchElement', () => {
})

it('should return an error if element is not found', async () => {
expect.assertions(3)
expect.assertions(4)

try {
await expect(page).toMatchElement('a', { text: 'Nop' })
} catch (error) {
expect(error.message).toMatch('Element a (text: "Nop") not found')
expect(error.message).toMatch('waiting for function failed')
expect(error.stack).toMatch(path.resolve(__filename))
}
})

Expand Down
7 changes: 5 additions & 2 deletions packages/expect-puppeteer/src/matchers/toSelect.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from "path";
import { setupPage } from './setupPage'

describe('toSelect', () => {
Expand Down Expand Up @@ -27,14 +28,15 @@ describe('toSelect', () => {
})

it('should return an error if option is not found', async () => {
expect.assertions(2)
expect.assertions(3)

try {
await expect(page).toSelect('select[name="my-select"]', 'Another world')
} catch (error) {
expect(error.message).toMatch(
'Option not found "select[name="my-select"]" ("Another world")',
)
expect(error.stack).toMatch(path.resolve(__filename))
}
})
})
Expand All @@ -60,14 +62,15 @@ describe('toSelect', () => {

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

try {
await expect(body).toSelect('select[name="my-select"]', 'Another world')
} catch (error) {
expect(error.message).toMatch(
'Option not found "select[name="my-select"]" ("Another world")',
)
expect(error.stack).toMatch(path.resolve(__filename))
}
})
})
Expand Down

0 comments on commit e9cafb1

Please sign in to comment.