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

[jest-each] Template table heading validation and extraction #8766

Merged
merged 9 commits into from Dec 13, 2020

Conversation

mattphillips
Copy link
Contributor

@mattphillips mattphillips commented Jul 30, 2019

Summary

Currently any additional words in a template will break jest-each's expected behaviour of injecting the template headings as variables to the test function.

For example:

test.only.each`
  a    | b    | expected
  This test demonstrates jest-each
  ${1} | ${2} | ${3}
`('$a + $b == $expected', ({a, b, expected}) => {
  expect(a + b).toBe(expected);
});

This test will fail as expected equals undefined.

That is because currently jest-each splits on | and removes whitespace so the expected key is actually deserialised to expectedThistestdemonstratesjest-each which is not correct.


The proposed solution to this is to validate the headings to make sure there are no additional words in the first row (heading row). Any words appearing after the first row are simply ignored by jest-each.

By ignoring the additional rows user's are able to add whatever they like - currently though prettier does strip out all additional text in the template after the first interpolation (${variable}), perhaps it is worth investigating how to prevent that.

This PR relates to point 2 in this comment

Test plan

See tests

EDIT: the above example will also now pass

@jeysal
Copy link
Contributor

jeysal commented Jul 31, 2019

Okay now I see what you mean. Just ignoring lines that do not match the expected format gives the possibility to put headlines in between, unrelated to any comment/skip API though.
It seems kinda magic and not like something I'd recommend people to do though. And doesn't work if there's just one parameter I guess.

Copy link
Contributor

@jeysal jeysal left a comment

Choose a reason for hiding this comment

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

Possibly breaking BTW if someone relies on multiple words between pipes

@mattphillips
Copy link
Contributor Author

@jeysal It should work fine even if there is only one heading/param (see b64b642) 😄

I'm not sure I agree that it's a breaking change, I doubt anyone does depend on this behaviour and it definitely is a bug (well is an unintended behaviour) and needs fixing.

I agree about not recommending people to add subheadings/additional text to the template - at least this way we do not put semantics (and code) around // syntax.

I suspect if we want to support a convention around this then it will need a little more thinking about. By fixing this behaviour it doesn't prevent people adding custom text should they want to in the meantime.

@jeysal
Copy link
Contributor

jeysal commented Aug 1, 2019

I'm not sure I agree that it's a breaking change, I doubt anyone does depend on this behaviour

Not with

a | b
some thing

'$a => $bsomething'

perhaps, but quite possibly with

a | some thing
${1} | ${2}

'$a => $something'

CHANGELOG.md Outdated Show resolved Hide resolved
@chrisblossom
Copy link
Contributor

I don't think this should be considered a breaking change. Although it is true that someone's test would break with a b | c, this is a bug and (most likely) unintended by the user.

@chrisblossom
Copy link
Contributor

Are there any updates on this (and/or #8717)?

@SimenB
Copy link
Member

SimenB commented May 4, 2020

@mattphillips ping 🙂

@SimenB SimenB added this to the Jest 27 milestone May 4, 2020
@jeysal
Copy link
Contributor

jeysal commented May 4, 2020

I think this is still good to go, but I would still say it's breaking because the example I gave in my last comment (using the behavior in headings) is actually not that far-fetched. Also, it's been lying around so long, might as well play it safe and put it into a major.

@jeysal jeysal force-pushed the jest-each-heading-validation branch from aeed61d to 2f75fb8 Compare May 4, 2020 20:28
@SimenB
Copy link
Member

SimenB commented Nov 14, 2020

@jeysal rebase and land?

@SimenB
Copy link
Member

SimenB commented Dec 5, 2020

@jeysal would you be able to take a look at CI?

* master: (30 commits)
  chore: verify TS project references are correct (jestjs#10941)
  chore(deps): bump actions/setup-node from v2.1.2 to v2.1.3 (jestjs#10940)
  docs: Rectify typo in tutorialReactNative (jestjs#10930)
  chore: patch react-native jest preprocessor to avoid warning
  Ensure `toContain` only accepts strings when `received` is a string (jestjs#10929)
  chore: update lockfile after publish
  v27.0.0-next.2
  Document and test custom, async, inline snapshot matcher (jestjs#10922)
  feat(transform): pass config options through to transformer (jestjs#10926)
  chore: bump eslint-config-prettier
  chore: run prettier using eslint
  chore: update lockfile after publish
  v27.0.0-next.1
  fix: move binary file declaration from runtime to repl (jestjs#10925)
  chore(test-result): remove deprecated `sourcemap` property (jestjs#10355)
  chore: remove mapCoverage remainings; remove deprecated CLI options test (jestjs#9968)
  refactor(jest-runtime,jest-transform): add readonly for some class fields (jestjs#10918)
  chore: ensure single environment package as well
  chore: fix failing tests (jestjs#10924)
  chore: fix lint warning
  ...
@jeysal
Copy link
Contributor

jeysal commented Dec 13, 2020

I think the new snapshots it's trying to write look appropriate. pushed them

@SimenB SimenB merged commit 79f4fe3 into jestjs:master Dec 13, 2020
@github-actions
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 May 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants