Skip to content

Conversation

@fossamagna
Copy link
Contributor

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)

@nodejs-github-botnodejs-github-bot added dont-land-on-v14.x needs-ci PRs that need a full CI run. test_runner Issues and PRs related to the test runner subsystem. labels Nov 18, 2022
@fossamagnafossamagnaforce-pushed the add-mock-getter-setter branch from 04a27b4 to 6dca9b9CompareNovember 18, 2022 14:06
Comment on lines 212 to 213
getter: true,
...options
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you swap these two lines so that getter cannot be set to a different value. Same comment below for setter.

functionmockMethod(){
returnthis.prop-1;
}
constgetter=t.mock.getter(obj,'method',mockMethod);
Copy link
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

@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() ?

Copy link
Contributor

Choose a reason for hiding this comment

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

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`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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`.

@fossamagnafossamagnaforce-pushed the add-mock-getter-setter branch 2 times, most recently from f16927b to e240af4CompareNovember 18, 2022 16:00
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])`
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I just realized that this is not sorted. mock.setter() should come after mock.reset().

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

I will sort them in alphabetical order.


if(!getter){
thrownewERR_INVALID_ARG_VALUE(
'options.getter',getter,"cannot set to 'false'"
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment for setter:

Suggested change
'options.getter',getter,"cannot set to 'false'"
'options.getter',getter,"cannot be false"

Comment on lines 216 to 220
const{ getter =true}=options;

validateBoolean(getter,'options.getter');

if(!getter){
Copy link
Contributor

Choose a reason for hiding this comment

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

We can simplify this and let method() handle the validation (same for setter).

Suggested change
const{ getter =true}=options;
validateBoolean(getter,'options.getter');
if(!getter){
if(options?.getter===false){

Copy link
Member

Choose a reason for hiding this comment

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

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?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

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.


validateBoolean(setter,'options.setter');

if(!setter){
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if(!setter){
if(setter===false){

Comment on lines 216 to 220
const{ getter =true}=options;

validateBoolean(getter,'options.getter');

if(!getter){
Copy link
Member

Choose a reason for hiding this comment

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

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?

@fossamagnafossamagnaforce-pushed the add-mock-getter-setter branch 2 times, most recently from cde2736 to 8e13ae4CompareNovember 19, 2022 15:04
Copy link
Member

@MoLowMoLow left a comment

Choose a reason for hiding this comment

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

LGTM

doc/api/test.md Outdated
});
```

they are sorted in alphabetical order.
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove this.

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
they are sorted in alphabetical order.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Sorry. I remove this.


if(getter===false){
thrownewERR_INVALID_ARG_VALUE(
'options.getter',getter,"cannot be 'false'"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
'options.getter',getter,"cannot be 'false'"
'options.getter',getter,"cannot be false"


if(setter===false){
thrownewERR_INVALID_ARG_VALUE(
'options.setter',setter,"cannot be 'false'"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
'options.setter',setter,"cannot be 'false'"
'options.setter',setter,"cannot be false"


returnthis.method(object,methodName,implementation,{
...options,
getter: true
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
getter: true
getter,

Comment on lines 214 to 216
}

validateObject(options,'options');
Copy link
Contributor

Choose a reason for hiding this comment

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

We can skip the validation if we know it's an object already.

Suggested change
}
validateObject(options,'options');
}else{
validateObject(options,'options');
}

Comment on lines 243 to 245
}

validateObject(options,'options');
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
}
validateObject(options,'options');
}else{
validateObject(options,'options');
}


returnthis.method(object,methodName,implementation,{
...options,
setter: true
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
setter: true
setter,

doc/api/test.md Outdated
});
```

they are sorted in alphabetical order.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
they are sorted in alphabetical order.

Comment on lines 220 to 221
validateBoolean(getter,'options.getter');

Copy link
Contributor

@aduh95aduh95Nov 20, 2022

Choose a reason for hiding this comment

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

The validation is already done in this.method (if you implement my suggestion in #45506 (comment)), I suggest removing it.

Suggested change
validateBoolean(getter, 'options.getter');

Comment on lines 249 to 250
validateBoolean(setter,'options.setter');

Copy link
Contributor

Choose a reason for hiding this comment

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

Same here.

Suggested change
validateBoolean(setter, 'options.setter');

@fossamagnafossamagnaforce-pushed the add-mock-getter-setter branch 2 times, most recently from bae6aa6 to dd8d5d3CompareNovember 20, 2022 14:21

returnthis.method(object,methodName,implementation,{
...options,
getter
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
getter
getter,


returnthis.method(object,methodName,implementation,{
...options,
setter
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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)
@fossamagnafossamagnaforce-pushed the add-mock-getter-setter branch from dd8d5d3 to 6da996aCompareNovember 20, 2022 23:59
@MoLowMoLow added the request-ci Add this label to start a Jenkins CI on a PR. label Nov 21, 2022
@github-actionsgithub-actionsbot removed the request-ci Add this label to start a Jenkins CI on a PR. label Nov 21, 2022
@nodejs-github-bot
Copy link
Collaborator

@ljharb
Copy link
Member

cc @nodejs/testing

Copy link
Contributor

@aduh95aduh95 left a comment

Choose a reason for hiding this comment

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

Ideally we would have tests to ensure that passing non-boolean values to getter or setter would trigger an error. Otherwise LGTM.

@nodejs-github-bot
Copy link
Collaborator

@cjihrigcjihrig added commit-queue Add this label to land a pull request using GitHub Actions. commit-queue-rebase Add this label to allow the Commit Queue to land a PR in several commits. and removed needs-ci PRs that need a full CI run. labels Nov 27, 2022
@nodejs-github-botnodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Nov 27, 2022
@nodejs-github-bot
Copy link
Collaborator

Landed in 147d810...afed1af

nodejs-github-bot pushed a commit that referenced this pull request Nov 27, 2022
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 <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
ErickWendel pushed a commit to ErickWendel/node that referenced this pull request Nov 30, 2022
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 <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
targos pushed a commit that referenced this pull request Dec 12, 2022
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 <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
@targostargos mentioned this pull request Dec 12, 2022
@targos
Copy link
Member

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

@richardlau
Copy link
Member

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 mock(.getter/.setter) in https://nodejs.org/docs/v16.19.0/api/test.html.

@richardlau
Copy link
Member

Mocking itself is one of the commits in the open backport #45602 but that hasn't landed on v16.x-staging yet.

@targos
Copy link
Member

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

@targos
Copy link
Member

At least that's the case on the main branch

@richardlau
Copy link
Member

Ah it's possible the release commit merged incorrectly.

richardlau added a commit to richardlau/node-1 that referenced this pull request Dec 14, 2022
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
@richardlau
Copy link
Member

Yes, it was indeed. Another example of nodejs/Release#771 😞. Thanks for catching it.

Opened #45863 to correct.

richardlau added a commit to richardlau/node-1 that referenced this pull request Dec 15, 2022
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
nodejs-github-bot pushed a commit that referenced this pull request Dec 16, 2022
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 <[email protected]> Reviewed-By: Tierney Cyren <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
danielleadams pushed a commit that referenced this pull request Dec 30, 2022
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 <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
danielleadams pushed a commit that referenced this pull request Dec 30, 2022
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 <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
danielleadams pushed a commit that referenced this pull request Jan 3, 2023
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 <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
danielleadams pushed a commit that referenced this pull request Jan 4, 2023
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 <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
danielleadams pushed a commit that referenced this pull request Jan 5, 2023
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 <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
MoLow pushed a commit to MoLow/node-core-test that referenced this pull request Feb 2, 2023
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 <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> (cherry picked from commit afed1afa55962211b6b56c2068d520b4d8a08888)
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commit-queue-rebaseAdd this label to allow the Commit Queue to land a PR in several commits.test_runnerIssues and PRs related to the test runner subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants

@fossamagna@nodejs-github-bot@ljharb@targos@richardlau@anonrig@cjihrig@MoLow@aduh95