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

feat(no-export): new rule for no-export #307

Merged
merged 6 commits into from Jul 18, 2019

Conversation

benmonro
Copy link
Contributor

I ran into this issue when we had a set of helper functions in 1 test file that
got exported directly from a -test.js file. The test that was importing this
file then executes both it's own tests as well as the tests in the imported
file. We started seeing phantom snapshots show up that we weren't able to find
the origin of until we finally tracked it down and realized that a test file was
exporting shared functions. We fixed this by moving those functions out into
seperate files, but that was the inspiration of this rule.

This rule aims to eliminate duplicate runs of tests by exporting things from
test files. If you import from a test file, then all the tests in that file will
be run in each imported instance, so bottom line, don't export from a test, but
instead move helper functions into a seperate file when they need to be shared
across tests.

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

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

This is an awesome rule, thanks for working on it! 🎉

I'm a bit worried about it forcing consumers to only apply it to test files, as it'll report false positives on production code. While not a huge problem, and it keeps the complexity of the rule implementation down, would it be a lot of work to make sure there's actually tests in the file? Might not be worth it, but if so I think it should be noted in its docs :)

(I've been thinking about adding some way of accessing jest config in rules, then we could check if the test would be run as such by Jest. Nothing concrete, though)

docs/rules/no-export.md Outdated Show resolved Hide resolved
@benmonro
Copy link
Contributor Author

@SimenB actually not hard at all to check for a test and a great idea! I'll add that

README.md Outdated
@@ -114,6 +114,7 @@ installations requiring long-term consistency.
| [no-commented-out-tests][] | Disallow commented out tests | | |
| [no-duplicate-hooks][] | Disallow duplicate hooks within a `describe` block | | |
| [no-empty-title][] | Disallow empty titles | | |
| [no-export][] | Disallow export from test files | ![recommended][] | |
Copy link
Member

Choose a reason for hiding this comment

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

Not recommended (changing recommended rules is a breaking change)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

what's the process of getting it to be a recommended change? This rule prevents very hard to track down problems....

Copy link
Member

Choose a reason for hiding this comment

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

Just remembering when we release a new major :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i'll remove it for now, but I would like to move this to recommended in another PR, if you don't mind providing guidance on that process.

Copy link
Member

Choose a reason for hiding this comment

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

Sure! Just open up a PR updating the readme and add it to the config exported from index and I'll make sure to merge it before making a major release

src/rules/no-export.js Outdated Show resolved Hide resolved
src/rules/no-export.js Outdated Show resolved Hide resolved
src/rules/no-export.js Outdated Show resolved Hide resolved
@chrisblossom
Copy link
Contributor

Sometimes you need to add an empty export {} in a typescript file to workaround getting typescript error error TS1208: Cannot compile namespaces when the '--isolatedModules' flag is provided microsoft/TypeScript#15230

Because of that, I think it would be helpful to let .test.ts or .test.tsx files have an empty export function.

@SimenB
Copy link
Member

SimenB commented Jul 18, 2019

Because of that, I think it would be helpful to let .test.ts or .test.tsx files have an empty export function.

I'm down with an option (allowEmpty, or something), but I don't think it should be the default behaviour. People can use overrides in their config if they want 🙂

@benmonro
Copy link
Contributor Author

can you merge @SimenB ? or give me permission to merge? :)

@benmonro
Copy link
Contributor Author

benmonro commented Jul 18, 2019

I can do the allowEmpty option in another PR

@SimenB SimenB merged commit b3a360d into jest-community:master Jul 18, 2019
@benmonro benmonro deleted the feature/no-exports branch July 18, 2019 17:58
@benmonro
Copy link
Contributor Author

@SimenB any guidance you can provide on what it takes to get a rule to be 'recommended'? The bugs that come about are quite nefarious and time consuming to track down, especially for junior devs...

@benmonro
Copy link
Contributor Author

@SimenB looks like the build failed on master. :( https://travis-ci.org/jest-community/eslint-plugin-jest/jobs/560608618

@SimenB
Copy link
Member

SimenB commented Jul 18, 2019

🎉 This PR is included in version 22.11.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@SimenB
Copy link
Member

SimenB commented Jul 18, 2019

@SimenB any guidance you can provide on what it takes to get a rule to be 'recommended'?

Not much guidance needed, just add the badge to the readme and add the rule here:

rules: {
'jest/no-alias-methods': 'warn',
'jest/no-disabled-tests': 'warn',
'jest/no-focused-tests': 'error',
'jest/no-identical-title': 'error',
'jest/no-jest-import': 'error',
// 'jest/no-mocks-import': 'error',
'jest/no-jasmine-globals': 'warn',
'jest/no-test-prefixes': 'error',
'jest/valid-describe': 'error',
'jest/valid-expect': 'error',
'jest/valid-expect-in-promise': 'error',
},

I'll make sure to merge the PR for the major 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants