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

Eslint check for mocking node core modules #786

Merged
merged 5 commits into from
Jun 11, 2020
Merged

Conversation

VitGottwald
Copy link
Contributor

No description provided.

@VitGottwald VitGottwald force-pushed the eslint-mock-guardian branch 3 times, most recently from 582e277 to 435de15 Compare May 11, 2020 14:11
@VitGottwald VitGottwald marked this pull request as ready for review May 11, 2020 15:17
see https://jestjs.io/docs/en/manual-mocks#mocking-node-modules

  > Warning: If we want to mock Node's core modules (e.g.: fs or path), then explicitly calling e.g. jest.mock('path') is required, because core Node modules are not mocked by default.

Signed-off-by: Vit Gottwald <vit.gottwald@gmail.com>
@VitGottwald VitGottwald force-pushed the eslint-mock-guardian branch from 435de15 to a72ce3f Compare May 12, 2020 08:53
@codecov
Copy link

codecov bot commented May 12, 2020

Codecov Report

Merging #786 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #786   +/-   ##
=======================================
  Coverage   91.23%   91.23%           
=======================================
  Files          47       47           
  Lines        5143     5143           
  Branches     1076     1076           
=======================================
  Hits         4692     4692           
  Misses        447      447           
  Partials        4        4           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3b10640...12db28b. Read the comment docs.

@katelynienaber
Copy link
Contributor

I want to mock these, but in the test PRs I am having trouble mocking node's standard module path.

Sometimes we need to use the standard behavior of path inside the test environment.

Maybe we could use something like this: https://www.npmjs.com/package/mock-fs

Or maybe there is a mock library someone has already made for path, somewhere on the interwebs?

Copy link
Contributor

@jellypuno jellypuno left a comment

Choose a reason for hiding this comment

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

Please see my comments

eslint-plugin/README.md Show resolved Hide resolved
{
"name": "eslint-plugin-zowe-explorer",
"version": "0.0.0",
"description": "Custom ESLint Rules for ZOWE Explorer",
Copy link
Contributor

Choose a reason for hiding this comment

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

I am confused with the word plugin. For me, plugin is optional but somehow I feel like this is not optional.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Eslint has several parts. Its core (the eslint package), plugins (which can provide linting rules) and a user configuration.

If someone wants to create a new rule for ESLint, they need to create an eslint plugin with the rule. That is what the eslint-plugin folder is.

To tell eslint I want it to use the plugin, the plugin has to be specified in eslint config https://github.com/zowe/vscode-extension-for-zowe/pull/786/files#diff-e4403a877d80de653400d88d85e4801aR4 . And to tell eslint to use a rule from the plugin for linting, it also has to be specified in the eslint config https://github.com/zowe/vscode-extension-for-zowe/pull/786/files#diff-e4403a877d80de653400d88d85e4801aR7.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does that answer your questions @jellypuno ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the team was interested in more details, I could do a small presentation and explain how today's javascript/typescript tooling works under the hood.

@VitGottwald
Copy link
Contributor Author

@katelynienaber I believe I answered your question in a standup. Because we are modifying the fs module, we have to make sure it is mocked. This eslint rule will make sure we call jest.mock('fs').

@phaumer
Copy link
Member

phaumer commented Jun 3, 2020

Do we really want to run two linters? I would propose to switch everything over to eslint at once. I did some experimentation with it here: zowe/zowe-explorer-ftp-extension#3

I would also not add any .js files. As we are typescript project we should implement everything strongly typed in .ts files. Configurations such as .eslintrc are easier to maintain as yaml files.

@VitGottwald
Copy link
Contributor Author

Good points @phaumer ! I would switch to ESlint without a blink of an eye. I actually suggested it about a month ago, but it has not been discussed yet and had no priority.

The goal of this PR is to prevent a very particular kind of error we discovered in our tests. I do not think its scope includes switching linters. For a short period of time I do not see a problem with having two of them.

@VitGottwald
Copy link
Contributor Author

I guess I used .eslintrc.js because I am used to it. I have no problem switching to yaml. There might however be some quirks related to running prettier. In the last project I had a conditional logic in eslintrc to only run prettier on CI to avoid the problems I described in zowe/zowe-explorer-ftp-extension#3

@VitGottwald
Copy link
Contributor Author

The .js question is also interesting one. ESlint's preferred way of providing custom linting rules is to provide an npm package with a plugin. They used to have the option to point to a directory with a rule, but they have long wanted to get rid of it and finally dropped it to have a clean architecture.
So we have to have a separate package for it.
If I write the plugin in typescript, I should define it as a typescript project and run a compile for it. Having a typescript project inside a typescript project causes troubles with compiler settings.
So the right thing to do is to create a workspace and make the ZOWE Explorer one package and the eslint plugin another package. Currently npm does not support workspaces (even though they plan it). So we would have to switch from npm to yarn.

I thought about all of this and decided that instead of boiling the ocean and introducing new concepts and additional tooling to he project, I would make it simple and go with javascript for the plugin. It is a lint tool, not production code.

What do you think we should do @phaumer ?

@phaumer
Copy link
Member

phaumer commented Jun 3, 2020

Ok, I did not realize that. If this is just a tool that does not require constant maintenance and not production code then I agree to do it in just the most straightforward way. Sorry for chiming in without fully understanding what is going on.

I definitely like yaml files for listing the rules for our main code, but if that also causes problem then I understand.

@VitGottwald
Copy link
Contributor Author

No worries @phaumer. All feedback is appreciated. It is my fault. I was so happy to have the solution to make sure we do not break tests again by modifying the fs module, that I rushed in with the PR. I should really have diligently described the solution and choices I made to everyone upfront.

@VitGottwald
Copy link
Contributor Author

For this particular PR we can go with .yaml, I will update it. I think the discussion about how to run prettier will determine if we need to switch back to .js or can stay with yaml.

@VitGottwald VitGottwald added this to the 1.7 Release milestone Jun 11, 2020
Copy link
Member

@zFernand0 zFernand0 left a comment

Choose a reason for hiding this comment

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

Thanks @VitGottwald
This will make sure that any new/refactored tests PRs do the right thing! 👍

@zFernand0 zFernand0 merged commit ae9faf7 into master Jun 11, 2020
@zFernand0 zFernand0 deleted the eslint-mock-guardian branch June 11, 2020 15:53
@zFernand0
Copy link
Member

Please keep in mind that the migration from tslint to eslint is still pending. and we will continue further discussion in zowe/zowe-explorer-ftp-extension#3

Also another reminder, let's aim to make eslint rule changes whenever there are a small amount of open PRs 😋

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.

6 participants