Skip to content
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

fix(javascript): do not apply test call formatting to arrow without body #5366

Merged
merged 6 commits into from Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/language-js/printer-estree.js
Expand Up @@ -6200,8 +6200,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 @@ -6245,6 +6247,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
);