-
Notifications
You must be signed in to change notification settings - Fork 248
feat(rules): add no-jasmine-globals
#116
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
feat(rules): add
no-jasmine-globals
- Loading branch information
commit c0a462a934d8db59aac45c2a8ddfae0f205d4985
There are no files selected for viewing
This file contains hidden or 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,59 @@ | ||
| # Disallow Jasmine globals | ||
|
|
||
| `jest` uses `jasmine` as a test runner. A side effect of this is that both a | ||
| `jasmine` object, and some jasmine-specific globals, are exposed to the test | ||
| environment. Most functionality offered by Jasmine has been ported to Jest, and | ||
| the Jasmine globals will stop working in the future. Developers should therefor | ||
| migrate to Jest's documented API instead of relying on the undocumented Jasmine | ||
| API. | ||
|
|
||
| ### Rule details | ||
|
|
||
| This rule reports on any usage of Jasmine globals which is not ported to Jest, | ||
| and suggests alternative from Jest's own API. | ||
|
|
||
| ### Default configuration | ||
|
|
||
| The following patterns are considered warnings: | ||
|
|
||
| ```js | ||
| jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000; | ||
|
|
||
| test('my test', () => { | ||
| pending(); | ||
| }); | ||
|
|
||
| test('my test', () => { | ||
| fail(); | ||
| }); | ||
|
|
||
| test('my test', () => { | ||
| spyOn(some, 'object'); | ||
| }); | ||
|
|
||
| test('my test', () => { | ||
| jasmine.createSpy(); | ||
| }); | ||
|
|
||
| test('my test', () => { | ||
| expect('foo').toEqual(jasmine.anything()); | ||
| }); | ||
| ``` | ||
|
|
||
| The following patterns would not be considered warnings: | ||
|
|
||
| ```js | ||
| jest.setTimeout(5000); | ||
|
|
||
| test('my test', () => { | ||
| spyOn(some, 'object'); | ||
| }); | ||
|
|
||
| test('my test', () => { | ||
| jest.fn(); | ||
| }); | ||
|
|
||
| test('my test', () => { | ||
| expect('foo').toEqual(expect.anything()); | ||
| }); | ||
| ``` | ||
This file contains hidden or 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 hidden or 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,205 @@ | ||
| 'use strict'; | ||
|
|
||
| const RuleTester = require('eslint').RuleTester; | ||
| const rule = require('../no-jasmine-globals'); | ||
|
|
||
| const ruleTester = new RuleTester(); | ||
|
|
||
| ruleTester.run('no-jasmine-globals', rule, { | ||
| valid: [ | ||
| 'jest.spyOn()', | ||
| 'jest.fn()', | ||
| 'expect.extend()', | ||
| 'expect.any()', | ||
| 'it("foo", function () {})', | ||
| 'test("foo", function () {})', | ||
| 'foo()', | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: 'spyOn(some, "object")', | ||
| errors: [ | ||
| { | ||
| message: 'Avoid using global `spyOn`, prefer `jest.spyOn`', | ||
| column: 1, | ||
| line: 1, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: 'spyOnProperty(some, "object")', | ||
| errors: [ | ||
| { | ||
| message: 'Avoid using global `spyOnProperty`, prefer `jest.spyOn`', | ||
| column: 1, | ||
| line: 1, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: 'fail()', | ||
| errors: [ | ||
| { | ||
| message: | ||
| 'Avoid global `fail`, prefer throwing an error, or the `done` callback', | ||
SimenB marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| column: 1, | ||
| line: 1, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: 'pending()', | ||
| errors: [ | ||
| { | ||
| message: 'Avoid global `pending`, prefer explicitly skipping a test', | ||
SimenB marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| column: 1, | ||
| line: 1, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: 'jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000;', | ||
| errors: [ | ||
| { | ||
| message: 'Avoid using global `jasmine`', | ||
| column: 1, | ||
| line: 1, | ||
| }, | ||
| ], | ||
| output: 'jest.setTimeout(5000);', | ||
| }, | ||
| { | ||
| code: 'jasmine.addMatchers(matchers)', | ||
| errors: [ | ||
| { | ||
| message: 'Avoid using `jasmine.addMatchers`, prefer `expect.extend`', | ||
| column: 1, | ||
| line: 1, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: 'jasmine.createSpy()', | ||
| errors: [ | ||
| { | ||
| message: 'Avoid using `jasmine.createSpy`, prefer `jest.fn`', | ||
| column: 1, | ||
| line: 1, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: 'jasmine.any()', | ||
| errors: [ | ||
| { | ||
| message: 'Avoid using `jasmine.any`, prefer `expect.any`', | ||
| column: 1, | ||
| line: 1, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: 'jasmine.anything()', | ||
| errors: [ | ||
| { | ||
| message: 'Avoid using `jasmine.anything`, prefer `expect.anything`', | ||
| column: 1, | ||
| line: 1, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: 'jasmine.arrayContaining()', | ||
| errors: [ | ||
| { | ||
| message: | ||
| 'Avoid using `jasmine.arrayContaining`, prefer `expect.arrayContaining`', | ||
| column: 1, | ||
| line: 1, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: 'jasmine.objectContaining()', | ||
| errors: [ | ||
| { | ||
| message: | ||
| 'Avoid using `jasmine.objectContaining`, prefer `expect.objectContaining`', | ||
| column: 1, | ||
| line: 1, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: 'jasmine.stringMatching()', | ||
| errors: [ | ||
| { | ||
| message: | ||
| 'Avoid using `jasmine.stringMatching`, prefer `expect.stringMatching`', | ||
| column: 1, | ||
| line: 1, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: 'jasmine.getEnv()', | ||
| errors: [ | ||
| { | ||
| message: 'Avoid using jasmine global', | ||
| column: 1, | ||
| line: 1, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: 'jasmine.empty()', | ||
| errors: [ | ||
| { | ||
| message: 'Avoid using jasmine global', | ||
| column: 1, | ||
| line: 1, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: 'jasmine.falsy()', | ||
| errors: [ | ||
| { | ||
| message: 'Avoid using jasmine global', | ||
| column: 1, | ||
| line: 1, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: 'jasmine.truthy()', | ||
| errors: [ | ||
| { | ||
| message: 'Avoid using jasmine global', | ||
| column: 1, | ||
| line: 1, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: 'jasmine.arrayWithExactContents()', | ||
| errors: [ | ||
| { | ||
| message: 'Avoid using jasmine global', | ||
| column: 1, | ||
| line: 1, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: 'jasmine.clock()', | ||
| errors: [ | ||
| { | ||
| message: 'Avoid using jasmine global', | ||
| column: 1, | ||
| line: 1, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.