Skip to content

Commit

Permalink
Support test.macro()
Browse files Browse the repository at this point in the history
Fixes #329.
  • Loading branch information
novemberborn committed Sep 18, 2021
1 parent cf361f1 commit abc162f
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 11 deletions.
5 changes: 3 additions & 2 deletions create-ava-rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,13 @@ module.exports = () => { // eslint-disable-line eslint-plugin/prefer-object-rule

return {
hasTestModifier: mod => getTestModifierNames(currentTestNode).includes(mod),
hasNoHookModifier: () => {
hasNoUtilityModifier: () => {
const modifiers = getTestModifierNames(currentTestNode);
return !modifiers.includes('before') &&
!modifiers.includes('beforeEach') &&
!modifiers.includes('after') &&
!modifiers.includes('afterEach');
!modifiers.includes('afterEach') &&
!modifiers.includes('macro');
},
isInTestFile: () => isTestFile,
isInTestNode: () => currentTestNode,
Expand Down
2 changes: 1 addition & 1 deletion rules/no-identical-title.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const create = context => {
CallExpression: visitIf([
ava.isInTestFile,
ava.isTestNode,
ava.hasNoHookModifier
ava.hasNoUtilityModifier
])(node => {
const args = node.arguments;
const titleNode = args.length > 1 || ava.hasTestModifier('todo') ? args[0] : undefined;
Expand Down
3 changes: 2 additions & 1 deletion rules/no-unknown-modifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const modifiers = new Set([
'serial',
'skip',
'todo',
'failing'
'failing',
'macro'
]);

const unknownModifiers = node => util.getTestModifiers(node)
Expand Down
2 changes: 1 addition & 1 deletion rules/test-title-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const create = context => {
CallExpression: visitIf([
ava.isInTestFile,
ava.isTestNode,
ava.hasNoHookModifier
ava.hasNoUtilityModifier
])(node => {
const requiredLength = ava.hasTestModifier('todo') ? 1 : 2;
const hasTitle = node.arguments.length >= requiredLength;
Expand Down
2 changes: 1 addition & 1 deletion rules/test-title.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const create = context => {
CallExpression: visitIf([
ava.isInTestFile,
ava.isTestNode,
ava.hasNoHookModifier
ava.hasNoUtilityModifier
])(node => {
const firstArgumentIsFunction = node.arguments.length === 0 || util.isFunctionExpression(node.arguments[0]);

Expand Down
16 changes: 11 additions & 5 deletions rules/use-t.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,24 @@ const create = context => {
ava.isInTestFile,
ava.isTestNode
])(node => {
const functionArgIndex = node.arguments.length - 1;
if (functionArgIndex > 1) {
const index = node.arguments.length - 1;
if (index > 1) {
return;
}

const functionArg = node.arguments[functionArgIndex];
let implementationArg = node.arguments[index];
if (ava.hasTestModifier('macro') && implementationArg.type === 'ObjectExpression') {
const execProperty = implementationArg.properties.find(p => {
return p.key.name === 'exec';
});
implementationArg = execProperty && execProperty.value;
}

if (!functionArg || !functionArg.params || functionArg.params.length === 0) {
if (!implementationArg || !implementationArg.params || implementationArg.params.length === 0) {
return;
}

if (functionArg.params[0].name !== 't') {
if (implementationArg.params[0].name !== 't') {
context.report({
node,
message: 'Test parameter should be named `t`.'
Expand Down
1 change: 1 addition & 0 deletions test/no-unknown-modifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ ruleTester.run('no-unknown-modifiers', rule, {
`${header}test.after.always(t => {});`,
`${header}test.afterEach.always(t => {});`,
`${header}test.failing(t => {});`,
`${header}test.macro(t => {});`,
// Shouldn't be triggered since it's not a test file
'test.foo(t => {});'
],
Expand Down
1 change: 1 addition & 0 deletions test/test-title.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ruleTester.run('test-title', rule, {
header + 'test.after(t => {});',
header + 'test.beforeEach(t => {});',
header + 'test.afterEach(t => {});',
header + 'test.macro(t => {});',
header + 'test.cb.before(t => {}); test.before.cb(t => {});',
header + 'notTest(t => { t.pass(); t.end(); });',
header + 'test([], arg1, arg2);',
Expand Down
11 changes: 11 additions & 0 deletions test/use-t.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ ruleTester.run('use-t', rule, {
header + 'test((t, foo) => {});',
header + 'test(function (t) {});',
header + 'test(testFunction);',
header + 'test.macro(testFunction);',
header + 'test.macro(t => {});',
header + 'test.macro({exec: t => {}, title: () => "title"});',
header + 'test.todo("test name");',
// Shouldn't be triggered since it's not a test file
'test(foo => {});',
Expand All @@ -50,6 +53,14 @@ ruleTester.run('use-t', rule, {
{
code: header + 'test(function (foo) {});',
errors: parameterNotNamedTErrors
},
{
code: header + 'test.macro(function (foo) {});',
errors: parameterNotNamedTErrors
},
{
code: header + 'test.macro({ exec(foo) {} });',
errors: parameterNotNamedTErrors
}
]
});

0 comments on commit abc162f

Please sign in to comment.