Skip to content

Commit

Permalink
(bugfix): allow failing condition to fail with proper error (#1445)
Browse files Browse the repository at this point in the history
* (chore): update deps

* (bugfix): allow failing condition to fail with proper error
  • Loading branch information
christian-bromann committed Feb 1, 2024
1 parent bde595d commit 95d97bd
Show file tree
Hide file tree
Showing 19 changed files with 274 additions and 143 deletions.
285 changes: 211 additions & 74 deletions package-lock.json

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"compile": "tsc --build tsconfig.build.json",
"test": "run-s test:*",
"test:lint": "eslint . --ext .js,.jsx,.ts,.tsx",
"test:unit": "vitest",
"test:unit": "vitest --run",
"test:types": "node test-types/copy && npm run ts && npm run clean:tests",
"ts": "run-s ts:*",
"ts:default": "cd test-types/default && tsc -p ./tsconfig.json --incremental",
Expand All @@ -61,30 +61,30 @@
"@types/debug": "^4.1.12",
"@types/jest": "^29.5.11",
"@types/lodash.isequal": "^4.5.8",
"@types/node": "^20.10.6",
"@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^6.17.0",
"@vitest/coverage-v8": "^1.1.3",
"c8": "^9.0.0",
"@types/node": "^20.11.14",
"@typescript-eslint/eslint-plugin": "^6.20.0",
"@typescript-eslint/parser": "^6.20.0",
"@vitest/coverage-v8": "^1.2.2",
"c8": "^9.1.0",
"eslint": "^8.56.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-unicorn": "^50.0.1",
"husky": "^9.0.6",
"husky": "^9.0.7",
"npm-run-all": "^4.1.5",
"release-it": "^17.0.1",
"release-it": "^17.0.3",
"rimraf": "^5.0.5",
"shelljs": "^0.8.5",
"typescript": "^5.3.3",
"vitest": "1.2.2",
"webdriverio": "8.29.1"
},
"optionalDependencies": {
"@wdio/globals": "^8.27.0",
"@wdio/logger": "^8.24.12",
"webdriverio": "^8.27.0"
"@wdio/globals": "^8.29.3",
"@wdio/logger": "^8.28.0",
"webdriverio": "^8.29.3"
},
"dependencies": {
"@vitest/snapshot": "^1.2.1",
"@vitest/snapshot": "^1.2.2",
"expect": "^29.7.0",
"jest-matcher-utils": "^29.7.0",
"lodash.isequal": "^4.5.0"
Expand Down
8 changes: 1 addition & 7 deletions src/matchers/element/toBeClickable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ export async function toBeClickable(
options,
})

const result = await executeCommandBe.call(this, received, async el => {
try {
return el.isClickable()
} catch {
return false
}
}, options)
const result = await executeCommandBe.call(this, received, el => el.isClickable(), options)

await options.afterAssertion?.({
matcherName: 'toBeClickable',
Expand Down
8 changes: 1 addition & 7 deletions src/matchers/element/toBeDisplayed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@ export async function toBeDisplayed(
options,
})

const result = await executeCommandBe.call(this, received, async el => {
try {
return el.isDisplayed()
} catch {
return false
}
}, options)
const result = await executeCommandBe.call(this, received, el => el.isDisplayed(), options)

await options.afterAssertion?.({
matcherName: 'toBeDisplayed',
Expand Down
8 changes: 1 addition & 7 deletions src/matchers/element/toBeDisplayedInViewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ export async function toBeDisplayedInViewport(
options,
})

const result = await executeCommandBe.call(this, received, async el => {
try {
return el.isDisplayedInViewport()
} catch {
return false
}
}, options)
const result = await executeCommandBe.call(this, received, el => el.isDisplayedInViewport(), options)

await options.afterAssertion?.({
matcherName: 'toBeDisplayedInViewport',
Expand Down
8 changes: 1 addition & 7 deletions src/matchers/element/toBeExisting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ export async function toExist(
options,
})

const result = await executeCommandBe.call(this, received, async el => {
try {
return el.isExisting()
} catch {
return false
}
}, options)
const result = await executeCommandBe.call(this, received, el => el.isExisting(), options)

This comment has been minimized.

Copy link
@goosewobbler

goosewobbler Feb 9, 2024

This change has broken the wdio-electron-service E2E for a non-existent element: https://github.com/webdriverio-community/wdio-electron-service/blob/main/example/e2e/dom.spec.ts#L18

Guarding for the element fixes it, might want to add this change for the other matchers above too.

const result = await executeCommandBe.call(this, received, el => el?.isExisting(), options)

This comment has been minimized.

Copy link
@christian-bromann

christian-bromann Feb 12, 2024

Author Member

Thanks for pointing this out, I will make sure to add a patch for this.

This comment has been minimized.

Copy link
@christian-bromann

christian-bromann Feb 12, 2024

Author Member

await options.afterAssertion?.({
matcherName: 'toExist',
Expand Down
8 changes: 7 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ const waitUntil = async (
return await condition()
}

let error: Error | undefined

// wait for condition to be truthy
try {
let error
const start = Date.now()
// eslint-disable-next-line no-constant-condition
while (true) {
Expand All @@ -43,6 +44,7 @@ const waitUntil = async (
error = undefined
try {
const result = isNot !== (await condition())
error = undefined
if (result) {
break
}
Expand All @@ -59,6 +61,10 @@ const waitUntil = async (

return !isNot
} catch (err) {
if (error) {
throw error
}

return isNot
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/matchers/beMatchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ describe('be* matchers', () => {
throw new Error('some error')
}

const result = await fn.call({}, el)
expect(result.pass).toBe(false)
await expect(() => fn.call({}, el, 10, {}))
.rejects.toThrow('some error')
})

test('success on the first attempt', async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/matchers/browserMatchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ describe('browser matchers', () => {
throw new Error('some error')
}

const result = await fn.call({}, browser, validText, { trim: false })
expect(result.pass).toBe(false)
await expect(() => fn.call({}, browser, validText, { trim: false }))
.rejects.toThrow('some error')
})

test('success on the first attempt', async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/matchers/element/toBeDisabled.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ describe('toBeDisabled', () => {
throw new Error('some error')
}

const result = await toBeDisabled.call({}, el)
expect(result.pass).toBe(false)
await expect(() => toBeDisabled.call({}, el))
.rejects.toThrow('some error')
})

test('success on the first attempt', async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/matchers/element/toHaveComputedLabel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ describe('toHaveComputedLabel', () => {
throw new Error('some error')
}

const result = await toHaveComputedLabel.call({}, el, 'WebdriverIO', { ignoreCase: true })
expect(result.pass).toBe(false)
await expect(() => toHaveComputedLabel.call({}, el, 'WebdriverIO', { ignoreCase: true }))
.rejects.toThrow('some error')
})

test('success on the first attempt', async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/matchers/element/toHaveComputedRole.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ describe('toHaveComputedcomputed role', () => {
throw new Error('some error')
}

const result = await toHaveComputedRole.call({}, el, 'WebdriverIO', { ignoreCase: true })
expect(result.pass).toBe(false)
await expect(() => toHaveComputedRole.call({}, el, 'WebdriverIO', { ignoreCase: true }))
.rejects.toThrow('some error')
})

test('success on the first attempt', async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/matchers/element/toHaveHTML.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ describe('toHaveHTML', () => {
throw new Error('some error')
}

const result = await toHaveHTML.call({}, el, '<div>foo</div>', { ignoreCase: true })
expect(result.pass).toBe(false)
await expect(() => toHaveHTML.call({}, el, '<div>foo</div>', { ignoreCase: true }))
.rejects.toThrow('some error')
})

test('success on the first attempt', async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/matchers/element/toHaveHeight.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ describe('toHaveHeight', () => {
throw new Error('some error')
}

const result = await toHaveHeight.call({}, el, 10, {})
expect(result.pass).toBe(false)
await expect(() => toHaveHeight.call({}, el, 10, {}))
.rejects.toThrow('some error')
})

test('success on the first attempt', async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/matchers/element/toHaveSize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ describe('toHaveSize', () => {
throw new Error('some error')
}

const result = await toHaveSize.call({}, el, { width: 32, height: 32 }, {})
expect(result.pass).toBe(false)
await expect(() => toHaveSize.call({}, el, { width: 32, height: 32 }, {}))
.rejects.toThrow('some error')
})

test('success on the first attempt', async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/matchers/element/toHaveStyle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ describe('toHaveStyle', () => {
}
throw new Error('some error');
})
const result = await toHaveStyle.call({}, el, mockStyle, { ignoreCase: true });
expect(result.pass).toBe(false)
await expect(() => toHaveStyle.call({}, el, mockStyle, { ignoreCase: true }))
.rejects.toThrow('some error')
})

test('success on the first attempt', async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/matchers/element/toHaveText.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ describe('toHaveText', () => {
throw new Error('some error')
}

const result = await toHaveText.call({}, el, 'WebdriverIO', { ignoreCase: true })
expect(result.pass).toBe(false)
await expect(() => toHaveText.call({}, el, 'WebdriverIO', { ignoreCase: true }))
.rejects.toThrow('some error')
})

test('success on the first attempt', async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/matchers/element/toHaveWidth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ describe('toHaveWidth', () => {
throw new Error('some error')
}

const result = await toHaveWidth.call({}, el, 10, {})
expect(result.pass).toBe(false)
await expect(() => toHaveWidth.call({}, el, 10, {}))
.rejects.toThrow('some error')
})

test('success on the first attempt', async () => {
Expand Down
24 changes: 18 additions & 6 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,29 @@ export default defineConfig({
* not to ESM ported packages
*/
exclude: [
'dist', '.idea', '.git', '.cache',
'dist', '.idea', '.git', '.cache', 'lib',
'**/node_modules/**'
],
testTimeout: 15 * 1000,
coverage: {
enabled: true,
exclude: ['**/build/**', '**/__fixtures__/**', '**/*.test.ts'],
lines: 92,
functions: 87,
branches: 89,
statements: 92
exclude: [
'**/build/**',
'**/__fixtures__/**',
'**/*.test.ts',
'lib',
'test-types',
'.eslintrc.cjs',
'jasmine.d.ts',
'jest.d.ts',
'types'
],
thresholds: {
lines: 93,
functions: 87,
branches: 91,
statements: 93
}
}
}
})

0 comments on commit 95d97bd

Please sign in to comment.