Skip to content

Commit

Permalink
fix(webdriverio): mark mock as being called even without overwrites (#…
Browse files Browse the repository at this point in the history
…13731)

* fix(webdriverio): mark mock as being called even without overwrites

* add e2e test
  • Loading branch information
christian-bromann authored Oct 8, 2024
1 parent fefc2cf commit d2681ce
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
13 changes: 13 additions & 0 deletions e2e/wdio/headless/mocking.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { browser, expect } from '@wdio/globals'

describe('network mocking', () => {
it('marks a request as mocked even without overwrites', async () => {
const baseUrl = 'http://guinea-pig.webdriver.io/'
const mock = await browser.mock(`${baseUrl}components/hammerjs/hammer.js`, {
method: 'get',
statusCode: 200,
})
await browser.url(baseUrl)
expect(mock.calls.length).toBe(1)
})
})
1 change: 1 addition & 0 deletions e2e/wdio/wdio.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const config: WebdriverIO.Config = {
path.join(__dirname, 'headless', 'source-maps.e2e.ts'),
path.join(__dirname, 'headless', 'reloadSession.e2e.ts'),
path.join(__dirname, 'headless', 'test.e2e.ts'),
path.join(__dirname, 'headless', 'mocking.e2e.ts'),
],

/**
Expand Down
29 changes: 17 additions & 12 deletions packages/webdriverio/src/utils/interception/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,22 +149,27 @@ export default class WebDriverInterception {
}

/**
* continue the request without modifications, if:
* continue mock if not matching filter
*/
const continueRequest = (
/**
* - mock has no request/respond overwrites
* - no request modifications are set, e.g. no overwrite is set
*/
!this.#matchesFilterOptions(request) ||
this.#respondOverwrites.length === 0 ||
!this.#respondOverwrites[0].overwrite
)
if (!this.#matchesFilterOptions(request)) {
this.#emitter.emit('continue', request.request.request)
return this.#browser.networkProvideResponse({
request: request.request.request
}).catch(this.#handleNetworkProvideResponseError)
}

/**
* check if request matches the mock url
* mark mock to be "called"
*/
if (continueRequest) {
this.#calls.push(request)

/**
* continue response as mock has no respond overwrites
*/
if (
this.#respondOverwrites.length === 0 ||
!this.#respondOverwrites[0].overwrite
) {
this.#emitter.emit('continue', request.request.request)
return this.#browser.networkProvideResponse({
request: request.request.request
Expand Down

0 comments on commit d2681ce

Please sign in to comment.