test_runner: add getter and setter to MockTracker#45506
test_runner: add getter and setter to MockTracker#45506fossamagna wants to merge 1 commit intonodejs:mainfrom
Conversation
04a27b4 to
6dca9b9
Compare
lib/internal/test_runner/mock.js
Outdated
| getter: true, | ||
| ...options |
There was a problem hiding this comment.
Can you swap these two lines so that getter cannot be set to a different value. Same comment below for setter.
| function mockMethod() { | ||
| return this.prop - 1; | ||
| } | ||
| const getter = t.mock.getter(obj, 'method', mockMethod); |
There was a problem hiding this comment.
Can you add tests that try to set getter and setter to false. Tests that throw when trying to set setter: true when calling getter() and vice versa would be nice to.
There was a problem hiding this comment.
@cjihrig Thank you for your suggestions.
If getter cannot be set to a different value, I think that validation do not work for options in method().
Do we implement validation for options in getter() and setter() ?
There was a problem hiding this comment.
If getter cannot be set to a different value, I think that validation do not work for options in method().
There is validation, but if you set the getter option to false, it would pass validation, but the getter() method would not make sense.
Do we implement validation for options in getter() and setter() ?
I would be OK with validating the getter or setter option to ensure that they are not set to any value besides true and deferring all other validation to method().
doc/api/test.md
Outdated
| added: REPLACEME | ||
| --> | ||
|
|
||
| This function is syntax sugar for [`MockTracker.method`][] with `options.getter` is `true`. |
There was a problem hiding this comment.
| This function is syntax sugar for [`MockTracker.method`][] with `options.getter` is `true`. | |
| This function is syntax sugar for [`MockTracker.method`][] with `options.getter` set to `true`. |
doc/api/test.md
Outdated
| added: REPLACEME | ||
| --> | ||
|
|
||
| This function is syntax sugar for [`MockTracker.method`][] with `options.setter` is `true`. |
There was a problem hiding this comment.
| This function is syntax sugar for [`MockTracker.method`][] with `options.setter` is `true`. | |
| This function is syntax sugar for [`MockTracker.method`][] with `options.setter` set to `true`. |
f16927b to
e240af4
Compare
doc/api/test.md
Outdated
| This function is syntax sugar for [`MockTracker.method`][] with `options.getter` | ||
| set to `true`. | ||
|
|
||
| ### `mock.setter(object, methodName[, implementation][, options])` |
There was a problem hiding this comment.
Sorry, I just realized that this is not sorted. mock.setter() should come after mock.reset().
There was a problem hiding this comment.
I will sort them in alphabetical order.
lib/internal/test_runner/mock.js
Outdated
|
|
||
| if (!getter) { | ||
| throw new ERR_INVALID_ARG_VALUE( | ||
| 'options.getter', getter, "cannot set to 'false'" |
There was a problem hiding this comment.
Same comment for setter:
| 'options.getter', getter, "cannot set to 'false'" | |
| 'options.getter', getter, "cannot be false" |
lib/internal/test_runner/mock.js
Outdated
| const { getter = true } = options; | ||
|
|
||
| validateBoolean(getter, 'options.getter'); | ||
|
|
||
| if (!getter) { |
There was a problem hiding this comment.
We can simplify this and let method() handle the validation (same for setter).
| const { getter = true } = options; | |
| validateBoolean(getter, 'options.getter'); | |
| if (!getter) { | |
| if (options?.getter === false) { |
There was a problem hiding this comment.
I didn't understand the reason/logic behind allowing only getter === true while setting it to true as default, but if it's false throw an error? Why do we even check for options?.getter at all?
There was a problem hiding this comment.
It is incorrect to specify options.getter for getter(). If we don't check it, getter() will not throw an error even if we specify false for options.getter. I think that it is better to check options.getter, since getter() will not throw an error if options.getter is specified as false.
lib/internal/test_runner/mock.js
Outdated
|
|
||
| validateBoolean(setter, 'options.setter'); | ||
|
|
||
| if (!setter) { |
There was a problem hiding this comment.
| if (!setter) { | |
| if (setter === false) { |
lib/internal/test_runner/mock.js
Outdated
| const { getter = true } = options; | ||
|
|
||
| validateBoolean(getter, 'options.getter'); | ||
|
|
||
| if (!getter) { |
There was a problem hiding this comment.
I didn't understand the reason/logic behind allowing only getter === true while setting it to true as default, but if it's false throw an error? Why do we even check for options?.getter at all?
cde2736 to
8e13ae4
Compare
doc/api/test.md
Outdated
| }); | ||
| ``` | ||
|
|
||
| they are sorted in alphabetical order. |
There was a problem hiding this comment.
| they are sorted in alphabetical order. |
There was a problem hiding this comment.
Sorry. I remove this.
lib/internal/test_runner/mock.js
Outdated
|
|
||
| if (getter === false) { | ||
| throw new ERR_INVALID_ARG_VALUE( | ||
| 'options.getter', getter, "cannot be 'false'" |
There was a problem hiding this comment.
| 'options.getter', getter, "cannot be 'false'" | |
| 'options.getter', getter, "cannot be false" |
lib/internal/test_runner/mock.js
Outdated
|
|
||
| if (setter === false) { | ||
| throw new ERR_INVALID_ARG_VALUE( | ||
| 'options.setter', setter, "cannot be 'false'" |
There was a problem hiding this comment.
| 'options.setter', setter, "cannot be 'false'" | |
| 'options.setter', setter, "cannot be false" |
lib/internal/test_runner/mock.js
Outdated
|
|
||
| return this.method(object, methodName, implementation, { | ||
| ...options, | ||
| getter: true |
There was a problem hiding this comment.
| getter: true | |
| getter, |
lib/internal/test_runner/mock.js
Outdated
| } | ||
|
|
||
| validateObject(options, 'options'); |
There was a problem hiding this comment.
We can skip the validation if we know it's an object already.
| } | |
| validateObject(options, 'options'); | |
| } else { | |
| validateObject(options, 'options'); | |
| } |
lib/internal/test_runner/mock.js
Outdated
| } | ||
|
|
||
| validateObject(options, 'options'); |
There was a problem hiding this comment.
| } | |
| validateObject(options, 'options'); | |
| } else { | |
| validateObject(options, 'options'); | |
| } |
lib/internal/test_runner/mock.js
Outdated
|
|
||
| return this.method(object, methodName, implementation, { | ||
| ...options, | ||
| setter: true |
There was a problem hiding this comment.
| setter: true | |
| setter, |
doc/api/test.md
Outdated
| }); | ||
| ``` | ||
|
|
||
| they are sorted in alphabetical order. |
There was a problem hiding this comment.
| they are sorted in alphabetical order. |
lib/internal/test_runner/mock.js
Outdated
| validateBoolean(getter, 'options.getter'); | ||
|
|
There was a problem hiding this comment.
The validation is already done in this.method (if you implement my suggestion in #45506 (comment)), I suggest removing it.
| validateBoolean(getter, 'options.getter'); |
lib/internal/test_runner/mock.js
Outdated
| validateBoolean(setter, 'options.setter'); | ||
|
|
There was a problem hiding this comment.
Same here.
| validateBoolean(setter, 'options.setter'); |
bae6aa6 to
dd8d5d3
Compare
lib/internal/test_runner/mock.js
Outdated
|
|
||
| return this.method(object, methodName, implementation, { | ||
| ...options, | ||
| getter |
There was a problem hiding this comment.
| getter | |
| getter, |
lib/internal/test_runner/mock.js
Outdated
|
|
||
| return this.method(object, methodName, implementation, { | ||
| ...options, | ||
| setter |
There was a problem hiding this comment.
| setter | |
| setter, |
This commit allows tests in test runner to use the `getter` and `setter` methods as "syntax sugar" for `MockTracker.method` with the `options.getter` or `options.setter` set to true in the options. Refs: nodejs#45326 (comment)
dd8d5d3 to
6da996a
Compare
|
cc @nodejs/testing |
aduh95
left a comment
There was a problem hiding this comment.
Ideally we would have tests to ensure that passing non-boolean values to getter or setter would trigger an error. Otherwise LGTM.
|
Landed in 147d810...afed1af |
This commit allows tests in test runner to use the `getter` and `setter` methods as "syntax sugar" for `MockTracker.method` with the `options.getter` or `options.setter` set to true in the options. Refs: #45326 (comment) PR-URL: #45506 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit allows tests in test runner to use the `getter` and `setter` methods as "syntax sugar" for `MockTracker.method` with the `options.getter` or `options.setter` set to true in the options. Refs: nodejs#45326 (comment) PR-URL: nodejs#45506 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit allows tests in test runner to use the `getter` and `setter` methods as "syntax sugar" for `MockTracker.method` with the `options.getter` or `options.setter` set to true in the options. Refs: #45326 (comment) PR-URL: #45506 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
|
Apparently the documentation for this landed in v16.x (maintenance LTS). I don't see any other references above so maybe the feature itself wasn't backported. /cc @richardlau |
I'm confused. I don't see any references to |
|
Mocking itself is one of the commits in the open backport #45602 but that hasn't landed on v16.x-staging yet. |
|
I'll dig in tomorrow, but while releasing v19.3.0, I had to update the added yaml field, which had an initial value for v16 |
|
At least that's the case on the main branch |
|
Ah it's possible the release commit merged incorrectly. |
When cherry-picking the release commit for Node.js 16.19.0 to the `main` branch git updated the metadata for the wrong item in `doc/api/test.md`. The incorrectly updated item has been fixed in a subsequent commit (the merge for the 19.3.0 release commit). This commit updates the intended item that should have been updated when the 16.19.0 release commit was merged. Refs: nodejs#45506 (comment) Refs: nodejs/Release#771
|
Yes, it was indeed. Another example of nodejs/Release#771 😞. Thanks for catching it. Opened #45863 to correct. |
When cherry-picking the release commit for Node.js 16.19.0 to the `main` branch git updated the metadata for the wrong item in `doc/api/test.md`. Refs: nodejs#45506 (comment) Refs: nodejs/Release#771
When cherry-picking the release commit for Node.js 16.19.0 to the `main` branch git updated the metadata for the wrong item in `doc/api/test.md`. Refs: #45506 (comment) Refs: nodejs/Release#771 PR-URL: #45863 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Tierney Cyren <hello@bnb.im> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit allows tests in test runner to use the `getter` and `setter` methods as "syntax sugar" for `MockTracker.method` with the `options.getter` or `options.setter` set to true in the options. Refs: #45326 (comment) PR-URL: #45506 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit allows tests in test runner to use the `getter` and `setter` methods as "syntax sugar" for `MockTracker.method` with the `options.getter` or `options.setter` set to true in the options. Refs: #45326 (comment) PR-URL: #45506 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit allows tests in test runner to use the `getter` and `setter` methods as "syntax sugar" for `MockTracker.method` with the `options.getter` or `options.setter` set to true in the options. Refs: #45326 (comment) PR-URL: #45506 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit allows tests in test runner to use the `getter` and `setter` methods as "syntax sugar" for `MockTracker.method` with the `options.getter` or `options.setter` set to true in the options. Refs: #45326 (comment) PR-URL: #45506 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit allows tests in test runner to use the `getter` and `setter` methods as "syntax sugar" for `MockTracker.method` with the `options.getter` or `options.setter` set to true in the options. Refs: #45326 (comment) PR-URL: #45506 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit allows tests in test runner to use the `getter` and `setter` methods as "syntax sugar" for `MockTracker.method` with the `options.getter` or `options.setter` set to true in the options. Refs: nodejs/node#45326 (comment) PR-URL: nodejs/node#45506 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> (cherry picked from commit afed1afa55962211b6b56c2068d520b4d8a08888)
This commit allows tests in test runner to use the
getterandsettermethods as "syntax sugar" forMockTracker.methodwith theoptions.getteroroptions.setterset to true in the options.Refs: #45326 (comment)