Skip to content

Commit

Permalink
Allow passing an argument to t.end() (fixes #164) (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmengels authored and sindresorhus committed Jan 10, 2017
1 parent ede93cc commit b433c20
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
32 changes: 12 additions & 20 deletions rules/assertion-arguments.js
Expand Up @@ -8,10 +8,6 @@ const expectedNbArguments = {
min: 2,
max: 3
},
end: {
min: 0,
max: 0
},
fail: {
min: 0,
max: 1
Expand Down Expand Up @@ -74,21 +70,6 @@ const expectedNbArguments = {
}
};

function nbArguments(node) {
const name = node.property.name;
const nArgs = expectedNbArguments[name];

if (nArgs) {
return nArgs;
}

if (node.object.type === 'MemberExpression') {
return nbArguments(node.object);
}

return false;
}

const create = context => {
const ava = createAvaRule();
const options = context.options[0] || {};
Expand All @@ -115,7 +96,18 @@ const create = context => {
}

const gottenArgs = node.arguments.length;
const nArgs = nbArguments(callee);
const members = util.getMembers(callee)
.filter(name => name !== 'skip');

if (members[0] === 'end') {
if (gottenArgs > 1) {
report(node, `Too many arguments. Expected at most 1.`);
}

return;
}

const nArgs = expectedNbArguments[members[0]];

if (!nArgs) {
return;
Expand Down
32 changes: 26 additions & 6 deletions test/assertion-arguments.js
Expand Up @@ -35,7 +35,6 @@ function testCase(message, content, errorMessage, useHeader) {
ruleTester.run('assertion-arguments', rule, {
valid: [
testCase(false, `t.plan(1);`),
testCase(false, `t.end();`),
testCase(false, `t.deepEqual({}, {}, 'message');`),
testCase(false, `t.fail('message');`),
testCase(false, `t.false(false, 'message');`),
Expand Down Expand Up @@ -82,7 +81,6 @@ ruleTester.run('assertion-arguments', rule, {
testCase(false, `t.foo(1, 2, 3, 4);`),

testCase('always', `t.plan(1);`),
testCase('always', `t.end();`),
testCase('always', `t.pass('message');`),
testCase('always', `t.fail('message');`),
testCase('always', `t.truthy('unicorn', 'message');`),
Expand All @@ -108,7 +106,6 @@ ruleTester.run('assertion-arguments', rule, {
testCase('always', `t.foo(1, 2, 3, 4);`),

testCase('never', `t.plan(1);`),
testCase('never', `t.end();`),
testCase('never', `t.pass();`),
testCase('never', `t.fail();`),
testCase('never', `t.truthy('unicorn');`),
Expand All @@ -132,7 +129,27 @@ ruleTester.run('assertion-arguments', rule, {

testCase('never', `t.context.a(1, 2, 3, 4);`),
testCase('never', `t.context.is(1, 2, 3, 4);`),
testCase('never', `t.foo(1, 2, 3, 4);`)
testCase('never', `t.foo(1, 2, 3, 4);`),

// Special case for `t.end()``
testCase(false, `t.end();`),
testCase(false, `t.end(error);`),
testCase(false, `t.end.skip();`),
testCase(false, `t.end.skip(error);`),
testCase(false, `t.skip.end();`),
testCase(false, `t.skip.end(error);`),
testCase('always', `t.end();`),
testCase('always', `t.end(error);`),
testCase('always', `t.end.skip();`),
testCase('always', `t.end.skip(error);`),
testCase('always', `t.skip.end();`),
testCase('always', `t.skip.end(error);`),
testCase('never', `t.end();`),
testCase('never', `t.end(error);`),
testCase('never', `t.end.skip();`),
testCase('never', `t.end.skip(error);`),
testCase('never', `t.skip.end();`),
testCase('never', `t.skip.end(error);`)
],
invalid: [
// Not enough arguments
Expand All @@ -155,7 +172,6 @@ ruleTester.run('assertion-arguments', rule, {

// Too many arguments
testCase(false, `t.plan(1, 'extra argument');`, tooManyError(1)),
testCase(false, `t.end('extra argument');`, tooManyError(0)),
testCase(false, `t.pass('message', 'extra argument');`, tooManyError(1)),
testCase(false, `t.fail('message', 'extra argument');`, tooManyError(1)),
testCase(false, `t.truthy('unicorn', 'message', 'extra argument');`, tooManyError(2)),
Expand Down Expand Up @@ -209,6 +225,10 @@ ruleTester.run('assertion-arguments', rule, {
testCase('never', `t.notRegex(a, /a/, 'message');`, foundError),
testCase('never', `t.ifError(new Error(), 'message');`, foundError),
testCase('never', `t.skip.is('same', 'same', 'message');`, foundError),
testCase('never', `t.is.skip('same', 'same', 'message');`, foundError)
testCase('never', `t.is.skip('same', 'same', 'message');`, foundError),

testCase(false, `t.end('too many', 'arguments');`, tooManyError(1)),
testCase(false, `t.skip.end('too many', 'arguments');`, tooManyError(1)),
testCase(false, `t.end.skip('too many', 'arguments');`, tooManyError(1))
]
});

0 comments on commit b433c20

Please sign in to comment.