-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
chore: enable eslint for __tests__ for adapter-nextjs aws-amplify and core #13108
Conversation
7cb4d22
to
857f60a
Compare
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.
I am curious for others' opinions on the promise param naming rules that seem to be on by default. I commented on one example, but there are a few others.
The rule potentially hurts readability by preventing the use of more semantically meaningful and/or "scoped" names in favor of the vague/generic naming. In some cases, I seem to recall renaming promise params to visually and mentally disentangle them and clearly communicate intent when promises are nested and/or "near" each other.
I don't want to block on this. But, if others feel the same, I would support disabling that rule.
manager.add(async () => new Promise(unsleep => setTimeout(unsleep, 10))); | ||
manager.add(async () => new Promise(resolve => setTimeout(resolve, 10))); |
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.
Strange. This one actually kind of bugs me. I feel like this rule has the potential to hinder readability.
But, I'm curious for others' opinions on this.
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.
Lol i was going to comment. the rule seems to not have any sense of humor
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.
Yea, I'm not holding on this strongly, but I can see the downside of renaming the executor functions for a type of use case. For example, in some places, e.g., in api-graphql and pubsub packages, we have quite a few promise executor implementations have a number of lines more than one page, and a sudden appearance of unblock
, unsleep
, etc. may actually hurt the readability which resolve
and reject
are the default names, which are related to a promise naturally.
For readability around promise... I think can also be achieved by better variable names, or creating a meaningful wrapper. For this particular case for example
const sleep = (seconds: number) => new Promise(resolve => setTimeout(resolve, seconds));
manager.add(sleep(10));
should provide even better readability. Wdyt?
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.
Just to add my 2 cents here. I think I would find it more confusing that a promise has anything other than resolve
/reject
than it would help to try and contextualize them in place. With resolve
- I understand immediately what it is doing within a promise body (especially a long one, as @HuiSF pointed out), whereas with unsleep
I have to understand ahead of time that that it's resolving the promise and not some magic function defined elsewhere.
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.
Agree. I sometimes tend to name something like in ResolvesWith
or ResolvesSomething
along those lines. I was more curious along those lines. Since a strict resolve/reject might enforce that right?
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.
It's actually the lengthier promises that benefit from a semantically named resolver, IMHO. But, I won't stomp my feet for this unless it bites me. Perhaps it'll be a good forcing function to refactor and reduce inline nesting.
packages/core/__tests__/Hub.test.ts
Outdated
@@ -67,7 +73,9 @@ describe('Hub', () => { | |||
); | |||
}); | |||
test('Remove listener', () => { | |||
const listener = jest.fn(() => {}); | |||
const listener = jest.fn(() => { | |||
// no-op |
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.
I dont think this rule would work for tests. This seems affect readability honestly. I dont see value with this.
packages/core/__tests__/utils/queuedStorage/queuedStorage.native.test.ts
Outdated
Show resolved
Hide resolved
@HuiSF Do u think for |
My own answer would be no to this question, we should treat |
857f60a
to
45c0c34
Compare
packages/adapter-nextjs/__tests__/api/generateServerClient.test.ts
Outdated
Show resolved
Hide resolved
return () => { | ||
// no op | ||
}; |
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.
Nit: Not sure but is it possible to use jest.fn
for some of these as well?
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.
Using jest.fn()
as the return type Jest.Mock
would change the parameter type on the setter function. As setter and getter are always a pair...
jest.spyOn(console, 'log').mockImplementation(noop); | ||
jest.spyOn(console, 'error').mockImplementation(noop); | ||
jest.spyOn(console, 'warn').mockImplementation(noop); | ||
jest.spyOn(console, 'info').mockImplementation(noop); | ||
jest.spyOn(console, 'debug').mockImplementation(noop); |
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.
I wonder if these even need their implementations mocked out since we're already replacing global.console in jest setup
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.
👍 Also updated the jest global config to use noop function instead of jest.fn()
as the latter uses system resource while testing though it's not expected to be asserted.
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.
The above change would break datastore unit tests which directly uses the original global mock... reverted this change and will do it in a separate PR.
@HuiSF Yeah, actually im seeing some of Chris's comment and i think it makes sense to keep hold of standard but actually to rewrite the tests better within the std. |
45c0c34
to
23d98bc
Compare
4fc0331
to
4c7e4af
Compare
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.
LGTM, one nit about no-op functions
4c7e4af
to
ea6b47d
Compare
ea6b47d
to
a009e7f
Compare
set onerror(_) {}, | ||
get onsuccess() { | ||
return () => { | ||
// no op |
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.
i still feel this is no-op is excessive and too verbose on tests
Please enter the commit message for your changes. Lines starting
This reverts commit 913b11e.
a009e7f
to
1a830db
Compare
… core (#13108) * chore(repo): setup the base for lint __tests__ codebase * chore(adapter-nextjs): run yarn lint:fix * chore(adapter-nextjs): manual fix linter reported erros * chore(aws-amplify): run yarn lint:fix Please enter the commit message for your changes. Lines starting * chore(aws-amplify): add camelcase var name exception: phone_number * chore(core): run yarn lint:fix * chore(core): manual fix linter reported errors under __tests__ * apply suggestions * turn off node callback function convention check * do not mock console functions globaly but noopify them * apply suggestions * Revert "do not mock console functions globaly but noopify them" This reverts commit 913b11e. * refactor a no-op function * remove // eslint-disable-next-line no-new
Description of changes
Details please see each commit of this PR. Recommend to focus on the commits that contain manual fixes.
Issue #, if available
Description of how you validated changes
Checklist
yarn test
passesBy submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.