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
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e9385f0
feat(no-export): no export from test file
de6a276
fix(no-export): no module exports?
a1f89d2
feat(no-export): added test
23cc0d2
docs(no-export): add no-export to readme
cdca0db
feat(no-export): only report when tests present
6681040
fix(no-export): changes from review feedback
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# no export from test file (no-export) | ||
|
||
Prevents exports from test files. If a file has at least 1 test in it, then this | ||
rule will prevent exports. | ||
|
||
## Rule Details | ||
|
||
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. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
export function myHelper() {} | ||
|
||
module.exports = function() {}; | ||
|
||
module.exports = { | ||
something: 'that should be moved to a non-test file', | ||
}; | ||
|
||
describe('a test', () => { | ||
expect(1).toBe(1); | ||
}); | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
function myHelper() {} | ||
|
||
const myThing = { | ||
something: 'that can live here', | ||
}; | ||
|
||
describe('a test', () => { | ||
expect(1).toBe(1); | ||
}); | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
Don't use this rule on non-jest test files. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { RuleTester } from 'eslint'; | ||
import rule from '../no-export'; | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2015, | ||
sourceType: 'module', | ||
}, | ||
}); | ||
|
||
ruleTester.run('no-export', rule, { | ||
valid: [ | ||
'describe("a test", () => { expect(1).toBe(1); })', | ||
'window.location = "valid"', | ||
'module.somethingElse = "foo";', | ||
'export const myThing = "valid"', | ||
'export default function () {}', | ||
'module.exports = function(){}', | ||
'module.exports.myThing = "valid";', | ||
], | ||
invalid: [ | ||
{ | ||
code: | ||
'export const myThing = "invalid"; test("a test", () => { expect(1).toBe(1);});', | ||
parserOptions: { sourceType: 'module' }, | ||
errors: [{ endColumn: 34, column: 1, messageId: 'unexpectedExport' }], | ||
}, | ||
{ | ||
code: | ||
'export default function() {}; test("a test", () => { expect(1).toBe(1);});', | ||
parserOptions: { sourceType: 'module' }, | ||
errors: [{ endColumn: 29, column: 1, messageId: 'unexpectedExport' }], | ||
}, | ||
{ | ||
code: | ||
'module.exports["invalid"] = function() {}; test("a test", () => { expect(1).toBe(1);});', | ||
errors: [{ endColumn: 26, column: 1, messageId: 'unexpectedExport' }], | ||
}, | ||
{ | ||
code: | ||
'module.exports = function() {}; ; test("a test", () => { expect(1).toBe(1);});', | ||
errors: [{ endColumn: 15, column: 1, messageId: 'unexpectedExport' }], | ||
}, | ||
{ | ||
code: | ||
'module.export.invalid = function() {}; ; test("a test", () => { expect(1).toBe(1);});', | ||
errors: [{ endColumn: 22, column: 1, messageId: 'unexpectedExport' }], | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { getDocsUrl, isTestCase } from './util'; | ||
|
||
let exportNodes = []; | ||
benmonro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let hasTestCase = false; | ||
const messageId = 'unexpectedExport'; | ||
export default { | ||
meta: { | ||
docs: { | ||
url: getDocsUrl(__filename), | ||
}, | ||
messages: { | ||
[messageId]: `Do not export from a test file.`, | ||
benmonro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
schema: [], | ||
}, | ||
create(context) { | ||
return { | ||
'Program:exit'() { | ||
if (hasTestCase && exportNodes.length > 0) { | ||
for (let node of exportNodes) { | ||
context.report({ node, messageId }); | ||
} | ||
} | ||
exportNodes = []; | ||
hasTestCase = false; | ||
}, | ||
|
||
CallExpression(node) { | ||
if (isTestCase(node)) { | ||
hasTestCase = true; | ||
} | ||
}, | ||
'ExportNamedDeclaration, ExportDefaultDeclaration'(node) { | ||
exportNodes.push(node); | ||
}, | ||
'AssignmentExpression > MemberExpression'(node) { | ||
let { object, property } = node; | ||
|
||
if (object.type === 'MemberExpression') { | ||
({ object, property } = object); | ||
} | ||
|
||
if (object.name === 'module' && !!property.name.match(/^exports?$/)) { | ||
benmonro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
exportNodes.push(node); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Not recommended (changing recommended rules is a breaking change)
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.
what's the process of getting it to be a recommended change? This rule prevents very hard to track down problems....
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 remembering when we release a new major :)
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'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.
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.
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