Skip to content

Commit

Permalink
feat(expect-puppeteer): keep original error message (#45)
Browse files Browse the repository at this point in the history
Closes #33
  • Loading branch information
gregberge authored Apr 22, 2018
1 parent 9cc0762 commit 72b60a9
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 16 deletions.
4 changes: 2 additions & 2 deletions packages/expect-puppeteer/src/matchers/notToMatch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defaultOptions, getContext } from '../utils'
import { defaultOptions, getContext, enhanceError } from '../utils'

async function notToMatch(instance, matcher, options) {
options = defaultOptions(options)
Expand All @@ -16,7 +16,7 @@ async function notToMatch(instance, matcher, options) {
matcher,
)
} catch (error) {
throw new Error(`Text found "${matcher}"`)
throw enhanceError(error, `Text found "${matcher}"`)
}
}

Expand Down
6 changes: 4 additions & 2 deletions packages/expect-puppeteer/src/matchers/notToMatch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ describe('not.toMatch', () => {
})

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

try {
await expect(page).not.toMatch('home')
} catch (error) {
expect(error.message).toMatch('Text found "home"')
expect(error.message).toMatch('waiting failed')
}
})
})
Expand All @@ -26,13 +27,14 @@ describe('not.toMatch', () => {
})

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

try {
await expect(dialogBtn).not.toMatch('Open dialog')
} catch (error) {
expect(error.message).toMatch('Text found "Open dialog"')
expect(error.message).toMatch('waiting failed')
}
})
})
Expand Down
5 changes: 3 additions & 2 deletions packages/expect-puppeteer/src/matchers/notToMatchElement.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defaultOptions, getContext } from '../utils'
import { defaultOptions, getContext, enhanceError } from '../utils'

async function notToMatchElement(
instance,
Expand Down Expand Up @@ -27,7 +27,8 @@ async function notToMatchElement(
text,
)
} catch (error) {
throw new Error(
throw enhanceError(
error,
`Element ${selector}${
text !== undefined ? ` (text: "${text}") ` : ' '
}found`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ describe('not.toMatchElement', () => {
})

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

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 failed')
}
})
})
Expand All @@ -36,12 +37,13 @@ describe('not.toMatchElement', () => {

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

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 failed')
}
})
})
Expand Down
4 changes: 2 additions & 2 deletions packages/expect-puppeteer/src/matchers/toMatch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defaultOptions, getContext } from '../utils'
import { defaultOptions, getContext, enhanceError } from '../utils'

async function toMatch(instance, matcher, options) {
options = defaultOptions(options)
Expand All @@ -16,7 +16,7 @@ async function toMatch(instance, matcher, options) {
matcher,
)
} catch (error) {
throw new Error(`Text not found "${matcher}"`)
throw enhanceError(error, `Text not found "${matcher}"`)
}
}

Expand Down
6 changes: 4 additions & 2 deletions packages/expect-puppeteer/src/matchers/toMatch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ describe('toMatch', () => {
})

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

try {
await expect(page).toMatch('Nop')
} catch (error) {
expect(error.message).toMatch('Text not found "Nop"')
expect(error.message).toMatch('waiting failed')
}
})
})
Expand All @@ -26,13 +27,14 @@ describe('toMatch', () => {
})

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

try {
await expect(dialogBtn).toMatch('This is home!')
} catch (error) {
expect(error.message).toMatch('Text not found "This is home!"')
expect(error.message).toMatch('waiting failed')
}
})
})
Expand Down
5 changes: 3 additions & 2 deletions packages/expect-puppeteer/src/matchers/toMatchElement.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defaultOptions, getContext } from '../utils'
import { defaultOptions, getContext, enhanceError } from '../utils'

async function toMatchElement(instance, selector, { text, ...options } = {}) {
options = defaultOptions(options)
Expand All @@ -16,7 +16,8 @@ async function toMatchElement(instance, selector, { text, ...options } = {}) {
try {
await page.waitForFunction(getElement, options, handle, selector, text)
} catch (error) {
throw new Error(
throw enhanceError(
error,
`Element ${selector}${
text !== undefined ? ` (text: "${text}") ` : ' '
}not found`,
Expand Down
6 changes: 4 additions & 2 deletions packages/expect-puppeteer/src/matchers/toMatchElement.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ describe('toMatchElement', () => {
})

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

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 failed')
}
})
})
Expand All @@ -50,12 +51,13 @@ describe('toMatchElement', () => {

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

try {
await expect(main).toMatchElement('a', { text: 'Page 2' })
} catch (error) {
expect(error.message).toMatch('Element a (text: "Page 2") not found')
expect(error.message).toMatch('waiting failed')
}
})
})
Expand Down
5 changes: 5 additions & 0 deletions packages/expect-puppeteer/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,8 @@ export const getContext = async (instance, pageFunction) => {
throw new Error(`${type} is not implemented`)
}
}

export const enhanceError = (error, message) => {
error.message = `${message}\n${error.message}`
return error
}

0 comments on commit 72b60a9

Please sign in to comment.