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

chore: enable eslint for __tests__ for adapter-nextjs aws-amplify and core #13108

Merged
merged 14 commits into from
Mar 28, 2024

Conversation

HuiSF
Copy link
Member

@HuiSF HuiSF commented Mar 11, 2024

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

  • PR description included
  • yarn test passes
  • Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@HuiSF HuiSF requested review from a team as code owners March 11, 2024 22:13
@HuiSF HuiSF force-pushed the hui/chore/lint-test-suite-1 branch from 7cb4d22 to 857f60a Compare March 11, 2024 22:17
svidgen
svidgen previously approved these changes Mar 12, 2024
Copy link
Member

@svidgen svidgen left a 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.

Comment on lines -148 to +149
manager.add(async () => new Promise(unsleep => setTimeout(unsleep, 10)));
manager.add(async () => new Promise(resolve => setTimeout(resolve, 10)));
Copy link
Member

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.

Copy link
Member

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

Copy link
Member Author

@HuiSF HuiSF Mar 15, 2024

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?

Copy link
Member

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.

Copy link
Member

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?

Copy link
Member

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.

@@ -67,7 +73,9 @@ describe('Hub', () => {
);
});
test('Remove listener', () => {
const listener = jest.fn(() => {});
const listener = jest.fn(() => {
// no-op
Copy link
Member

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.

@ashika112
Copy link
Member

@HuiSF Do u think for __tests__ maybe remove some rules that wouldn't make sense on it?

@HuiSF
Copy link
Member Author

HuiSF commented Mar 15, 2024

@HuiSF Do u think for __tests__ maybe remove some rules that wouldn't make sense on it?

My own answer would be no to this question, we should treat __tests__ codebase with the same standard to ensure the same quality. I believe this helps us writing robust test suites. Open for further discussion.

@HuiSF HuiSF force-pushed the hui/chore/lint-test-suite-1 branch from 857f60a to 45c0c34 Compare March 18, 2024 22:42
@HuiSF HuiSF requested review from cshfang, svidgen and ashika112 March 18, 2024 22:42
Comment on lines +170 to +172
return () => {
// no op
};
Copy link
Member

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?

Copy link
Member Author

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...

packages/core/__tests__/utils.test.ts Outdated Show resolved Hide resolved
packages/core/__tests__/storage/CookieStorage.test.ts Outdated Show resolved Hide resolved
packages/core/__tests__/storage/DefaultStorage.test.ts Outdated Show resolved Hide resolved
packages/core/__tests__/Platform/customUserAgent.test.ts Outdated Show resolved Hide resolved
packages/core/__tests__/Hub.test.ts Outdated Show resolved Hide resolved
Comment on lines 11 to 15
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);
Copy link
Member

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

Copy link
Member Author

@HuiSF HuiSF Mar 19, 2024

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.

Copy link
Member Author

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.

packages/core/__tests__/BackgroundProcessManager.test.ts Outdated Show resolved Hide resolved
packages/core/__tests__/BackgroundProcessManager.test.ts Outdated Show resolved Hide resolved
@ashika112
Copy link
Member

@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.

@HuiSF HuiSF force-pushed the hui/chore/lint-test-suite-1 branch from 45c0c34 to 23d98bc Compare March 19, 2024 21:45
@HuiSF HuiSF requested a review from cshfang March 19, 2024 21:49
@HuiSF HuiSF force-pushed the hui/chore/lint-test-suite-1 branch 2 times, most recently from 4fc0331 to 4c7e4af Compare March 20, 2024 23:40
jimblanc
jimblanc previously approved these changes Mar 21, 2024
Copy link
Contributor

@jimblanc jimblanc left a 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

@HuiSF HuiSF force-pushed the hui/chore/lint-test-suite-1 branch from 4c7e4af to ea6b47d Compare March 26, 2024 22:53
cshfang
cshfang previously approved these changes Mar 26, 2024
@HuiSF HuiSF dismissed stale reviews from cshfang and jimblanc via a009e7f March 26, 2024 23:48
@HuiSF HuiSF force-pushed the hui/chore/lint-test-suite-1 branch from ea6b47d to a009e7f Compare March 26, 2024 23:48
set onerror(_) {},
get onsuccess() {
return () => {
// no op
Copy link
Member

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

@HuiSF HuiSF force-pushed the hui/chore/lint-test-suite-1 branch from a009e7f to 1a830db Compare March 28, 2024 17:26
@HuiSF HuiSF merged commit c4f1432 into main Mar 28, 2024
30 checks passed
@HuiSF HuiSF deleted the hui/chore/lint-test-suite-1 branch March 28, 2024 17:58
Samaritan1011001 pushed a commit that referenced this pull request Apr 1, 2024
… 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants