-
Notifications
You must be signed in to change notification settings - Fork 72
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
Option for enhanced debugging output during testing #561
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ tsconfig.tsbuildinfo | |
tmp/ | ||
dist/ | ||
.vscode/ | ||
.yarn/cache | ||
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -217,6 +217,7 @@ | |||||||||||||||||||||||||||||||||
"@babel/register": "^7.24.6", | ||||||||||||||||||||||||||||||||||
"@changesets/cli": "^2.27.8", | ||||||||||||||||||||||||||||||||||
"@chiragrupani/karma-chromium-edge-launcher": "^2.4.1", | ||||||||||||||||||||||||||||||||||
"@types/diff": "^5.2.2", | ||||||||||||||||||||||||||||||||||
"@types/jest": "^29.5.12", | ||||||||||||||||||||||||||||||||||
"@types/karma": "^6.3.8", | ||||||||||||||||||||||||||||||||||
"@types/node": "^22.5.4", | ||||||||||||||||||||||||||||||||||
|
@@ -267,5 +268,9 @@ | |||||||||||||||||||||||||||||||||
"webpack": "^5.94.0", | ||||||||||||||||||||||||||||||||||
"whatwg-fetch": "^3.6.20" | ||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||
"packageManager": "[email protected]" | ||||||||||||||||||||||||||||||||||
"packageManager": "[email protected]", | ||||||||||||||||||||||||||||||||||
"dependencies": { | ||||||||||||||||||||||||||||||||||
"diff": "^7.0.0", | ||||||||||||||||||||||||||||||||||
"tty-table": "^4.2.3" | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
Comment on lines
+272
to
+275
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How would this work? https://docs.npmjs.com/cli/v7/configuring-npm/package-json#peerdependenciesmeta
Suggested change
I guess since this PR aims to be in "experimentation" mode we could consider have it like this for now even though it is a wonkier install experience for the user. I am not proposing this change, just asking if you have considered it and how you feel about it. |
||||||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,22 +2,29 @@ import MockAssert from './mock-assert' | |||||
import Response from '../response' | ||||||
import { isPlainObject } from '../utils/index' | ||||||
import { clone } from '../utils/clone' | ||||||
import { sortedUrl, toSortedQueryString, isSubset } from './mock-utils' | ||||||
import { sortedUrl, toSortedQueryString, filterKeys } from './mock-utils' | ||||||
|
||||||
/** | ||||||
* @param {number} id | ||||||
* @param {object} props | ||||||
* @param {string} props.method | ||||||
* @param {string|function} props.url | ||||||
* @param {string|function} props.body - request body | ||||||
* @param {string} props.mockName | ||||||
* @param {object} props.response | ||||||
* @param {string} props.response.body | ||||||
* @param {object} props.response.headers | ||||||
* @param {integer} props.response.status | ||||||
*/ | ||||||
|
||||||
const MATCHED_AS_UNDEFINED_IN_MOCK = 'MATCHED_AS_UNDEFINED_IN_MOCK' | ||||||
const MATCHED_BY_FUNCTION = 'MATCHED_BY_FUNCTION' | ||||||
const MISMATCH_BY_FUNCTION = 'MISMATCHED_BY_FUNCTION' | ||||||
|
||||||
function MockRequest(id, props) { | ||||||
this.id = id | ||||||
|
||||||
this.mockName = props.mockName ? props.mockName : this.id | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe interpolate a name from the id, something like:
Suggested change
|
||||||
this.method = props.method || 'get' | ||||||
this.urlFunction = typeof props.url === 'function' | ||||||
this.url = props.url | ||||||
|
@@ -80,33 +87,100 @@ MockRequest.prototype = { | |||||
return new MockAssert(this.calls) | ||||||
}, | ||||||
|
||||||
/** | ||||||
* Checks if the request matches with the mock HTTP method, URL, headers and body | ||||||
* | ||||||
* @return {boolean} | ||||||
*/ | ||||||
isExactMatch(request) { | ||||||
const bodyMatch = () => { | ||||||
if (this.body === undefined) { | ||||||
return true | ||||||
bodyMatchRequest(request) { | ||||||
if (this.body === undefined) { | ||||||
return { | ||||||
match: true, | ||||||
mockValue: MATCHED_AS_UNDEFINED_IN_MOCK, | ||||||
requestValue: MATCHED_AS_UNDEFINED_IN_MOCK, | ||||||
} | ||||||
} | ||||||
if (this.bodyFunction) { | ||||||
const match = this.body(request.body()) | ||||||
const value = match ? MATCHED_BY_FUNCTION : MISMATCH_BY_FUNCTION | ||||||
return { match, mockValue: value, requestValue: value } | ||||||
} | ||||||
const requestBodyAsString = toSortedQueryString(request.body()) | ||||||
const match = this.body === requestBodyAsString | ||||||
return { | ||||||
match, | ||||||
mockValue: decodeURIComponent(this.body), | ||||||
requestValue: decodeURIComponent(requestBodyAsString), | ||||||
} | ||||||
}, | ||||||
|
||||||
return this.bodyFunction | ||||||
? this.body(request.body()) | ||||||
: this.body === toSortedQueryString(request.body()) | ||||||
urlMatchRequest(request) { | ||||||
if (this.urlFunction) { | ||||||
const match = Boolean(this.url(request.url(), request.params())) | ||||||
const value = match ? MATCHED_BY_FUNCTION : MISMATCH_BY_FUNCTION | ||||||
return { match, mockValue: value, requestValue: value } | ||||||
} | ||||||
const requestUrlAsSortedString = sortedUrl(request.url()) | ||||||
const mockRequestUrlAsSortedString = sortedUrl(this.url) | ||||||
const match = mockRequestUrlAsSortedString === requestUrlAsSortedString | ||||||
return { | ||||||
match, | ||||||
mockValue: decodeURIComponent(mockRequestUrlAsSortedString), | ||||||
requestValue: decodeURIComponent(requestUrlAsSortedString), | ||||||
} | ||||||
}, | ||||||
|
||||||
const urlMatch = this.urlFunction | ||||||
? this.url(request.url(), request.params()) | ||||||
: sortedUrl(this.url) === sortedUrl(request.url()) | ||||||
headersMatchRequest(request) { | ||||||
if (!this.headers) | ||||||
return { | ||||||
match: true, | ||||||
mockValue: MATCHED_AS_UNDEFINED_IN_MOCK, | ||||||
requestValue: MATCHED_AS_UNDEFINED_IN_MOCK, | ||||||
} | ||||||
if (this.headersFunction) { | ||||||
const match = this.headers(request.headers()) | ||||||
const value = match ? MATCHED_BY_FUNCTION : MISMATCH_BY_FUNCTION | ||||||
return { match, mockValue: value, requestValue: value } | ||||||
} | ||||||
const filteredRequestHeaders = filterKeys(this.headersObject, request.headers()) | ||||||
const requestHeadersAsSortedString = toSortedQueryString(filteredRequestHeaders) | ||||||
const mockRequestHeadersAsSortedString = toSortedQueryString(this.headersObject) | ||||||
const match = requestHeadersAsSortedString === mockRequestHeadersAsSortedString | ||||||
|
||||||
return { | ||||||
match, | ||||||
mockValue: mockRequestHeadersAsSortedString, | ||||||
requestValue: requestHeadersAsSortedString, | ||||||
} | ||||||
}, | ||||||
|
||||||
const headerMatch = | ||||||
!this.headers || | ||||||
(this.headersFunction | ||||||
? this.headers(request.headers()) | ||||||
: isSubset(this.headersObject, request.headers())) | ||||||
methodMatchRequest(request) { | ||||||
const requestMethod = request.method() | ||||||
const match = this.method === requestMethod | ||||||
return { | ||||||
match, | ||||||
mockValue: this.method, | ||||||
requestValue: requestMethod, | ||||||
} | ||||||
}, | ||||||
|
||||||
return this.method === request.method() && urlMatch && bodyMatch() && headerMatch | ||||||
getRequestMatching(request) { | ||||||
const method = this.methodMatchRequest(request) | ||||||
const url = this.urlMatchRequest(request) | ||||||
const body = this.bodyMatchRequest(request) | ||||||
const headers = this.headersMatchRequest(request) | ||||||
return { | ||||||
mockName: this.mockName, | ||||||
isExactMatch: method.match && url.match && body.match && headers.match, | ||||||
isPartialMatch: this.isPartialMatch(request), | ||||||
method, | ||||||
url, | ||||||
body, | ||||||
headers, | ||||||
} | ||||||
}, | ||||||
/** | ||||||
* Checks if the request matches with the mock HTTP method, URL, headers and body | ||||||
* | ||||||
* @return {boolean} | ||||||
*/ | ||||||
isExactMatch(request) { | ||||||
return this.getRequestMatching(request).isExactMatch | ||||||
}, | ||||||
|
||||||
/** | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, we are weird, but revert this. And check in the changes of
yarn/cache
folder :)Otherwise CI breaks, it wants the cached entries (faster CI).