[ Feature]: v10 – @wdio/jasmine-framework: keep Jasmine sync matchers synchronous (hybrid expect)
Is your feature request related to a problem?
In @wdio/jasmine-framework, the global expect is Jasmine's expectAsync. The adapter also wraps every Jasmine sync matcher as an async matcher. Because of this, expect(1).toBe(1) returns a Promise, but the types say it returns void.
Code: packages/wdio-jasmine-framework/src/index.ts
setupExpect():const expect = jasmineEnv.expectAsyncand then_setGlobal('expect', expect, …).#setupMatchers():jasmine.addMatchersis replaced with(m) => jasmine.addAsyncMatchers(transform(m)), and Jasmine's built-in matchers are registered as async ({ ...wdioMatchers, ...syncMatchers }).#transformMatchers():compare: async (actual, expected, ...args) => fn(util).compare(actual, expected, ...args).
expect-webdriverio also documents this in jasmine-wdio-expect-async.d.ts: "expectAsync is forced into the expect global ambient, making all Jasmine sync-matchers asynchronous", with a TODO to move that typing into @wdio/jasmine-framework.
Verified on v10 (jasmine-core 6.3.0, expect-webdriverio 8.0.0-alpha.0). I ran real Jasmine through JasmineAdapter with the real expect-webdriverio:
| Spec | Plain Jasmine | @wdio/jasmine-framework |
|---|---|---|
expect(1).toBe(1) return value |
undefined |
Promise (TypeScript says void) |
Called spy: await expect(spy).toHaveBeenCalled() |
pass | fail: Error: <toHaveBeenCalled> : Does not take arguments, use toHaveBeenCalledWith |
Uncalled spy: expect(spy).toHaveBeenCalled() (no await) |
fail | pass, with an unhandledRejection for the same error |
stopSpecOnExpectationFailure: true, a failed sync expect, then more code |
spec stops | spec continues (the failure is a rejected promise, not a throw) |
await expect(elem).toHaveSize({ width, height }) |
n/a | Jasmine's toHaveSize runs: …with size 3 to have size [object Object] |
Effects:
- Sync matchers are async. Types say
void, sono-floating-promisesand similar rules do not find a missingawait. Code after a failed assertion continues to run. - False pass.
#transformMatchersalways passesexpected, even when it isundefined.toHaveBeenCalled,toHaveSpyInteractionsandtoHaveNoOtherSpyInteractionscheckarguments.length > 1, so they always throw. Withoutawait, the throw becomes an unhandled rejection and the spec passes. - Stop on first failure is not possible. The wrapper cannot throw synchronously.
- WDIO
toHaveSizeis hidden. Jasmine's synctoHaveSizeis spread over the WDIO matcher of the same name. Jasmine and WDIO share no other matcher names. - Jasmine deprecation warning. Replacing
jasmine.addMatchersgives: "DEPRECATION: Monkey patching detected… will break in a future release."
Minimal reproduction (Jasmine framework, any config):
describe('sync matchers', () => {
it('should fail but passes', () => {
expect(jasmine.createSpy('s')).toHaveBeenCalled()
})
it('should pass but fails', async () => {
const s = jasmine.createSpy('s'); s()
await expect(s).toHaveBeenCalled()
})
it('returns a Promise although typed as void', () => {
console.log(expect(1).toBe(1) instanceof Promise) // true
})
})Describe the solution you'd like.
For v10, use a hybrid global expect. It sends each matcher call to the correct Jasmine expectation:
- Sync: Jasmine built-in matchers and user
jasmine.addMatchersmatchers useenv.expect(actual). They returnvoid, throw whenstopSpecOnExpectationFailureis set, and use Jasmine's ownnegativeCompareand messages. - Async: WDIO matchers (
Object.keys(wdioCustomMatchers)), Jasmine async matchers (toBeResolved,toBeRejected,toBeResolvedTo,toBeRejectedWith,toBeRejectedWithError,toBePending) and userjasmine.addAsyncMatchersmatchers useenv.expectAsync(actual). They return aPromise. .notand.withContext()return the same hybrid. Asymmetric matchers (expect.any,expect.stringContaining, …) stay on the function, as they do now.- Colliding name (
toHaveSize): use WDIO whenactualis a WDIO element, element array or chainable promise. Otherwise, use Jasmine.
Changes in the adapter:
- Register only the WDIO matchers with
addAsyncMatchers. Remove thesyncMatchersspread. - Remove the
jasmine.addMatchersreplacement and#transformMatchers. This also removes the monkey-patching deprecation warning. - Types: move the typing from
expect-webdriverio/jasmine-wdio-expect-asyncinto@wdio/jasmine-framework(as expect-webdriverio's TODO asks). Jasmine sync matchers returnvoid, and WDIO and Jasmine async matchers returnPromise<void>.
User impact: await expect(1).toBe(1) continues to work, because await undefined is valid. await expect(el).toBeDisplayed() does not change. Tests that use Jasmine's toHaveSize on a WDIO element change behaviour. That is the only breaking case, so this fits v10.
Proof of concept: a Proxy over env.expect and env.expectAsync, applied after setupExpect() and run through the real adapter. Results:
- Sync matchers return
undefined. - Failing sync matchers fail the spec without
await. toHaveBeenCalledpasses and fails correctly..notand.withContextmessages are correct.toBeResolvedTo,toHaveTitle, and.notwith asymmetric matchers work.- User
addMatchersmatchers stay sync. - With
stopSpecOnExpectationFailure: true, the spec stops at the first failure.
Describe alternatives you've considered.
- Only fix
#transformMatchers(forward the exactarguments, await them, and use the nativenegativeCompare). This fixes effect 2 only. Matchers stay async, the types stay wrong, and stop on first failure stays impossible. - Global
expect= Jasmine syncexpect, WDIO matchers only onexpectAsync. This is Jasmine's own model, but everyawait expect(el).toBeDisplayed()must change. The migration cost is too high. - Global
expect= expect-webdriverio (Jest style), as in Mocha. Jasmine matchers (toBeTrue, spy matchers onjasmine.createSpy) would stop working.
Additional context
Related bug found during the analysis (it can have its own issue): v10 removed jasmineOpts.stopSpecOnExpectationFailure and tells users to use jasmineOpts.oneFailurePerSpec (adapter constructor and website/docs/v10Migration.md). jasmine-core 6.3.0 has no oneFailurePerSpec option. Configuration.update() only reads stopSpecOnExpectationFailure, so oneFailurePerSpec: true has no effect. The rename must go in the opposite direction.
Code of Conduct
- I agree to follow this project's Code of Conduct
Source: webdriverio/webdriverio