Skip to content

Commit

Permalink
fix(javascript): do not apply test call formatting to arrow without b…
Browse files Browse the repository at this point in the history
…ody (#5366)
  • Loading branch information
ikatyang committed Nov 8, 2018
1 parent 4b51907 commit fd8ec95
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/language-js/printer-estree.js
Expand Up @@ -6204,8 +6204,10 @@ function isTestCall(n, parent) {
return false;
}
return (
(isFunctionOrArrowExpression(n.arguments[1]) &&
n.arguments[1].params.length <= 1) ||
(n.arguments.length === 2
? isFunctionOrArrowExpression(n.arguments[1])
: isFunctionOrArrowExpressionWithBody(n.arguments[1]) &&
n.arguments[1].params.length <= 1) ||
isAngularTestWrapper(n.arguments[1])
);
}
Expand Down Expand Up @@ -6249,6 +6251,14 @@ function isFunctionOrArrowExpression(node) {
);
}

function isFunctionOrArrowExpressionWithBody(node) {
return (
node.type === "FunctionExpression" ||
(node.type === "ArrowFunctionExpression" &&
node.body.type === "BlockStatement")
);
}

function isUnitTestSetUp(n) {
const unitTestSetUpRe = /^(before|after)(Each|All)$/;
return (
Expand Down
50 changes: 50 additions & 0 deletions tests/test_declarations/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -167,6 +167,8 @@ it("does something really long and complicated so I have to write a very long na
// code
}));
it("does something really long and complicated so I have to write a very long name for the test", fakeAsync(() => new SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS));
/*
* isTestCall(parent) should only be called when parent exists
* and parent.type is CallExpression. This test makes sure that
Expand All @@ -190,6 +192,9 @@ it("does something really long and complicated so I have to write a very long na
// code
}));
it("does something really long and complicated so I have to write a very long name for the test", fakeAsync(() =>
new SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS()));
/*
* isTestCall(parent) should only be called when parent exists
* and parent.type is CallExpression. This test makes sure that
Expand Down Expand Up @@ -218,6 +223,8 @@ it("does something really long and complicated so I have to write a very long na
// code
}));
it("does something really long and complicated so I have to write a very long name for the test", fakeAsync(() => new SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS));
/*
* isTestCall(parent) should only be called when parent exists
* and parent.type is CallExpression. This test makes sure that
Expand All @@ -241,6 +248,9 @@ it("does something really long and complicated so I have to write a very long na
// code
}));
it("does something really long and complicated so I have to write a very long name for the test", fakeAsync(() =>
new SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS()));
/*
* isTestCall(parent) should only be called when parent exists
* and parent.type is CallExpression. This test makes sure that
Expand Down Expand Up @@ -692,6 +702,17 @@ it(\`handles
it("does something quick", () => {
console.log("hello!")
}, 1000000000)
it(
'succeeds if the test finishes in time',
() => new Promise(resolve => setTimeout(resolve, 10))
);
it(
'succeeds if the test finishes in time',
() => new Promise(resolve => setTimeout(resolve, 10)),
250
);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Shouldn't break
Expand Down Expand Up @@ -826,6 +847,15 @@ it("does something quick", () => {
console.log("hello!");
}, 1000000000);
it("succeeds if the test finishes in time", () =>
new Promise(resolve => setTimeout(resolve, 10)));
it(
"succeeds if the test finishes in time",
() => new Promise(resolve => setTimeout(resolve, 10)),
250
);
`;
exports[`test_declarations.js - flow-verify 2`] = `
Expand Down Expand Up @@ -948,6 +978,17 @@ it(\`handles
it("does something quick", () => {
console.log("hello!")
}, 1000000000)
it(
'succeeds if the test finishes in time',
() => new Promise(resolve => setTimeout(resolve, 10))
);
it(
'succeeds if the test finishes in time',
() => new Promise(resolve => setTimeout(resolve, 10)),
250
);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Shouldn't break
Expand Down Expand Up @@ -1082,4 +1123,13 @@ it("does something quick", () => {
console.log("hello!");
}, 1000000000);
it("succeeds if the test finishes in time", () =>
new Promise((resolve) => setTimeout(resolve, 10)));
it(
"succeeds if the test finishes in time",
() => new Promise((resolve) => setTimeout(resolve, 10)),
250
);
`;
2 changes: 2 additions & 0 deletions tests/test_declarations/angular_fakeAsync.js
Expand Up @@ -14,6 +14,8 @@ it("does something really long and complicated so I have to write a very long na
// code
}));

it("does something really long and complicated so I have to write a very long name for the test", fakeAsync(() => new SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS));

/*
* isTestCall(parent) should only be called when parent exists
* and parent.type is CallExpression. This test makes sure that
Expand Down
11 changes: 11 additions & 0 deletions tests/test_declarations/test_declarations.js
Expand Up @@ -117,3 +117,14 @@ it(`handles
it("does something quick", () => {
console.log("hello!")
}, 1000000000)

it(
'succeeds if the test finishes in time',
() => new Promise(resolve => setTimeout(resolve, 10))
);

it(
'succeeds if the test finishes in time',
() => new Promise(resolve => setTimeout(resolve, 10)),
250
);

0 comments on commit fd8ec95

Please sign in to comment.