Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mock): emit 'request', 'overwrite', 'fail', 'match', 'continue' events #10137

Merged

Conversation

KuznetsovRoman
Copy link
Contributor

@KuznetsovRoman KuznetsovRoman commented Apr 9, 2023

Proposed changes

Make a mock EventEmitter, emitting request, overwrite, fail, continue events

request is being emitted on request
overwrite is being emitted if response is being overwrited by respond / respondOnce
fail is being emitted if request is aborted by abort / abortOnce
match is being emitted when request is added to matches array (before continue or overwrite) (it has mockedResponse property if overwrite is provided)
continue is being emitted if mock is a spy of if response was already sent (like in case with two mocks. If two mock patterns match current request, and both of them has some overwrites. Then first mock will trigger overwrite event, and the second one will trigger continue)

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

Checklist

  • I have read the CONTRIBUTING doc
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • I have added proper type definitions for new commands (if appropriate)

Further comments

Will be provided in self-review by lines

Reviewers: @webdriverio/project-committers

@KuznetsovRoman KuznetsovRoman changed the title feat(mock): emit 'request', 'overwrite', 'fail', 'continue' events feat(mock): emit 'request', 'overwrite', 'fail', 'match', 'continue' events Apr 10, 2023
Copy link
Member

@christian-bromann christian-bromann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change looks good to me. Can you provide the motivation of this change and maybe add it to the docs section for people that skim through them can immediately for what this is useful?

@KuznetsovRoman
Copy link
Contributor Author

Can you provide the motivation of this change and maybe add it to the docs section for people that skim through them can immediately for what this is useful?

Sure. Added some examples in Events section of mock page.

In addition, you could also modify props of object, passed to event handler, but for now i could not imagine use-cases for that.

Also, here is the order of emitting events:

  • request
  • match
  • continue / overwrite / fail (strictly one of these per request in each mock)

Copy link
Member

@christian-bromann christian-bromann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great examples! Some last questions.

website/docs/api/Mock.md Outdated Show resolved Hide resolved
website/docs/api/Mock.md Outdated Show resolved Hide resolved
website/docs/api/Mock.md Outdated Show resolved Hide resolved
@KuznetsovRoman KuznetsovRoman force-pushed the feat/mocks_event-emitter branch 3 times, most recently from a23d469 to 1476a8f Compare April 10, 2023 21:46
Throwing an error on 404 network fail:

```js
const loadPage = (browser, url, {selector, predicate}) => new Promsie(async (resolve, reject) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we wrap this into a custom command, e.g.

await browser.addCustomCommand('loadPageWithout404', (url, { selector, predicate }) => { ... })

browser.loadPageWithout404('some/url', { selector: 'main' })

Seems to be more WebdriverIO native.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Member

@christian-bromann christian-bromann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks a lot 👍

website/docs/api/Mock.md Outdated Show resolved Hide resolved
@christian-bromann christian-bromann added the PR: New Feature 🚀 PRs that contain new features label Apr 10, 2023
@christian-bromann christian-bromann merged commit adb4ee6 into webdriverio:main Apr 10, 2023
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: New Feature 🚀 PRs that contain new features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants