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: Support accessing attributes in name interpolation #6452

Closed
jackwilsdon opened this issue Jun 12, 2018 · 6 comments · Fixed by #6457
Closed

jest-each: Support accessing attributes in name interpolation #6452

jackwilsdon opened this issue Jun 12, 2018 · 6 comments · Fixed by #6457

Comments

@jackwilsdon
Copy link
Contributor

🚀 Feature Proposal

jest-each should support accessing attributes on objects when generating a description for a test.

For example;

each`
 object           | expectedValue
 ${{ value: 5 }}  | ${5}
 ${{ value: 10 }} | ${10}
`.test('object with a value of ${object.value} has a value of $expectedValue', () => ...)

Admittedly this is not a great example, but I'm sure you can get the idea.

Motivation

I came across this whilst trying to test my Redux reducers using jest-each;

// Actions
const increment = () => ({ type: 'increment' });
const decrement = () => ({ type: 'decrement' });

// Reducer
const reducer = (state = 0, { type }) => {
  switch (type) {
    case 'increment':
      return state + 1;
    case 'decrement':
      return state - 1;
    default:
      return state;
  }
};

each`
  state | action         | nextState
  ${0}  | ${increment()} | ${1}
  ${0}  | ${decrement()} | ${-1}
`.test(
  'returns $nextState when the state is $state and ${action.type} is dispatched',
  ({ state, action, nextState }) => {
    console.log(state, action, nextState);
    expect(3).toEqual(4);
  },
);

This would be a useful feature, as it would allow generation of descriptions for tests using object variables.

Example

See motiviation.

Pitch

Why does this feature belong in the Jest core platform?

It is an extension of an existing piece of functionality which can't be easily added (unless I'm missing something) without forking the library.

@mattphillips
Copy link
Contributor

Hey @jackwilsdon I really like this idea.

I've gone ahead and implemented this over in #6457 with a slightly different syntax.

Instead of:
${action.type}

I've gone with:
$action.type

Feel free to give feedback on the PR 😄

@mattphillips
Copy link
Contributor

BTW @jackwilsdon if you are using Jest@23 you can now use jest-each natively with: test.each and describe.each see docs here

@jackwilsdon
Copy link
Contributor Author

jackwilsdon commented Jun 13, 2018

@mattphillips great! Much appreciated 😁. The reason I suggested the ${} syntax was to avoid ambiguity, for example; This is a $sentence.I hope it comes out alright! (we'd end up getting $sentence.I instead of just $sentence in this example).

I understand that's not the best example in the world, but I'm sure you get the idea.

Cheers! 🎉

@szafirov
Copy link

szafirov commented Mar 2, 2020

Is there a special syntax to print the value of something like a.b[0]?
It currently prints the value of a.b followed by '[0]' instead.

@jackwilsdon
Copy link
Contributor Author

Have you tried a.b.0?

@github-actions
Copy link

This issue 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 11, 2021
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 a pull request may close this issue.

3 participants