Skip to content

Commit

Permalink
Minor fixes and cleanups.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpojer committed Mar 6, 2017
1 parent 2ec914d commit de36add
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 deletions.
2 changes: 1 addition & 1 deletion packages/eslint-plugin-jest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ You can also whitelist the environment variables provided by Jest by doing:
- [no-disabled-tests](/packages/eslint-plugin-jest/docs/rules/no-disabled-tests.md) - disallow disabled tests.
- [no-focused-tests](/packages/eslint-plugin-jest/docs/rules/no-focused-tests.md) - disallow focused tests.
- [no-identical-title](/packages/eslint-plugin-jest/docs/rules/no-identical-title.md) - disallow identical titles.
- [valid-expect](/packages/eslint-plugin-jest/docs/rules/valid-expect.md) - disallow identical titles.
- [valid-expect](/packages/eslint-plugin-jest/docs/rules/valid-expect.md) - ensure expect is called correctly.

## Shareable configurations

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,56 +30,50 @@ ruleTester.run('valid-expect', rules['valid-expect'], {
code: 'expect().toBe(true);',
errors: [
{
message: 'No arguments passed to expect()',
message: 'No arguments were passed to expect().',
},
],
},
{
code: 'expect().toEqual("something");',
errors: [
{
message: 'No arguments passed to expect()',
message: 'No arguments were passed to expect().',
},
],
},
{
code: 'expect("something", "else").toEqual("something");',
errors: [
{
message: 'More than one argument passed to expect()',
message: 'More than one argument was passed to expect().',
},
],
},
{
code: 'expect("something");',
errors: [
{
message: 'Matcher was not called',
},
{
message: 'Nothing called on expect()',
message: 'No assertion was called on expect().',
},
],
},
{
code: 'expect();',
errors: [
{
message: 'No arguments passed to expect()',
},
{
message: 'Matcher was not called',
message: 'No arguments were passed to expect().',
},
{
message: 'Nothing called on expect()',
message: 'No assertion was called on expect().',
},
],
},
{
code: 'expect(true).toBeDefined;',
errors: [
{
message: 'Matcher was not called',
message: '"toBeDefined" was not called.',
},
],
},
Expand Down
23 changes: 14 additions & 9 deletions packages/eslint-plugin-jest/src/rules/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,37 @@
* @flow
*/

export type Identifier = {|
type Node = MemberExpression | CallExpression;

export type Identifier = {
type: 'Identifier',
name: string,
value: string,
|};
parent: Node,
};

export type MemberExpression = {|
export type MemberExpression = {
type: 'MemberExpression',
name: string,
expression: CallExpression,
property: Identifier,
object: Identifier,
|};
parent: Node,
};

export type Literal = {|
export type Literal = {
type: 'Literal',
value?: string,
rawValue?: string,
|};
parent: Node,
};

export type CallExpression = {|
export type CallExpression = {
type: 'CallExpression',
arguments: [Literal],
callee: Identifier | MemberExpression,
parent: any,
|};
parent: Node,
};

export type EslintContext = {|
report: ({message: string, node: any}) => void
Expand Down
18 changes: 12 additions & 6 deletions packages/eslint-plugin-jest/src/rules/valid-expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,27 @@ module.exports = (context: EslintContext) => {
// checking "expect()" arguments
if (node.arguments.length > 1) {
context.report({
message: 'More than one argument passed to expect()',
message: 'More than one argument was passed to expect().',
node,
});
} else if (node.arguments.length === 0) {
context.report({message: 'No arguments passed to expect()', node});
context.report({
message: 'No arguments were passed to expect().',
node,
});
}

// matcher was not called
if (
node.parent &&
node.parent.type === 'MemberExpression' &&
node.parent.parent &&
node.parent.parent.type !== 'CallExpression' &&
node.parent.parent.type !== 'MemberExpression'
node.parent.parent.type === 'ExpressionStatement'
) {
context.report({message: 'Matcher was not called', node});
context.report({
message: `"${node.parent.property.name}" was not called.`,
node,
});
}
}
},
Expand All @@ -48,7 +54,7 @@ module.exports = (context: EslintContext) => {
node.callee.name === 'expect' &&
node.parent.type === 'ExpressionStatement'
) {
context.report({message: 'Nothing called on expect()', node});
context.report({message: 'No assertion was called on expect().', node});
}
},
};
Expand Down

0 comments on commit de36add

Please sign in to comment.