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

test.each(): Accept a readonly (as const) table properly #14565

Merged
merged 4 commits into from
Oct 2, 2023

Conversation

Peeja
Copy link
Contributor

@Peeja Peeja commented Sep 23, 2023

Summary

Sometimes the inferred types in a test.each() table are too wide for the test that uses them. For instance, consider this test in which letterCase has to be specifically 'upper' or 'lower':

const changeCase = (letterCase: 'upper' | 'lower', str: string) => {
  if (letterCase === 'upper') {
    return str.toUpperCase();
  } else {
    return str.toLowerCase();
  }
};

test.each([
  ['upper', 'abc', 'ABC'],
  ['lower', 'ABC', 'abc'],
])('some test', (letterCase, str, expected) => {
  expect(changeCase(letterCase, str)).toBe(expected);  // Argument of type 'string' is not assignable to parameter of type '"upper" | "lower"'
});

You can keep the literal types of the table values by giving it as const, but that causes a different (stranger) error:

test.each([
  ['upper', 'abc', 'ABC'],
  ['lower', 'ABC', 'abc'],
] as const)('some test', (letterCase, str, expected) => {
//                        ^-----------------------^
// Argument of type '(letterCase: "upper" | "lower", str: "abc" | "ABC", expected: "abc" | "ABC") => void' is not assignable to parameter of type '(...args: readonly ["upper", "abc", "ABC"] | readonly ["lower", "ABC", "abc"]) => TestReturnValue | TestReturnValueGenerator'.
//   Types of parameters 'letterCase' and 'args' are incompatible.
//     Type 'readonly ["upper", "abc", "ABC"] | readonly ["lower", "ABC", "abc"]' is not assignable to type '[letterCase: "upper" | "lower", str: "abc" | "ABC", expected: "abc" | "ABC"]'.
//       The type 'readonly ["upper", "abc", "ABC"]' is 'readonly' and cannot be assigned to the mutable type '[letterCase: "upper" | "lower", str: "abc" | "ABC", expected: "abc" | "ABC"]'.
  expect(changeCase(letterCase, str)).toBe(expected);
});

TypeScript appears to infer the type of the argument list as mutable, and then tries to assign the readonly argument tuple to it, which is an error. But we don't actually need the argument list to be mutable, so we want to tell TypeScript to consider it readonly as well. This type change does that, and makes the error go away.

Test plan

I believe as long as this test (and the rest of the suite) passes, it's safe. To verify, see that the test fails without this type change, and that everything passes with it.

Without this, TypeScript infers the types strangely such that it tries
to assign the readonly argument list from the table to a mutable
argument list type in the test function signature. This change correctly
infers the argument list in the test function as readonly, which
resolves the error.
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Sep 23, 2023

CLA Signed

The committers listed above are authorized under a signed CLA.

@netlify
Copy link

netlify bot commented Sep 23, 2023

Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit c4cb26b
🔍 Latest deploy log https://app.netlify.com/sites/jestjs/deploys/651aaa446de6ac0008b22639
😎 Deploy Preview https://deploy-preview-14565--jestjs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@Peeja Peeja changed the title test.each(): Accept a readonly table properly test.each(): Accept a readonly (as const) table properly Sep 23, 2023
Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@SimenB SimenB merged commit 8c729b6 into jestjs:main Oct 2, 2023
7 checks passed
@SimenB
Copy link
Member

SimenB commented Oct 30, 2023

Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants