1

This may be too basic but it's escaping me at the moment. I have accidentally created the following working code in Jest whilst writing a test for a Vue application:

const updateMethod = wrapper.vm.updateMethod = jest.fn()
expect(updateVal).toHaveBeenCalledWith('methodArgument')

I was just wondering what is the name of this sort of an assignment? I'm lacking the thought process at the moment due to burn out to refactor this.

Thank you!

skyboyer
  • 22,209
  • 7
  • 57
  • 64
Cold Fridge
  • 113
  • 8
  • It's a form of 'chaining' in my book, though not the most common method of doing that in JS – MikeB May 05 '20 at 12:56
  • I've never seen a term for repeated application of the assignment operator. It's important to really understand what it means in the context of a variable declaration as in your example, and it's a good idea to avoid the pattern until you understand it. – Pointy May 05 '20 at 13:31
  • I agree with avoiding it. Some modern languages, like Swift, prevent doing that entirely. – Robo Robok May 05 '20 at 13:33
  • 1
    There's no specific term, it's just a statement that works because it's valid. `wrapper.vm.updateMethod = jest.fn()` is a separate expression that's evaluated first because of operator precedence so this no different than `const updateMethod = whatever`. FWIW, it's a bad practice to assign jest.fn() to mock a function because it cannot be restored. A proper way that doesn't involve multiple assignment is `const updateMethod = jest.spyOn(wrapper.vm, 'updateMethod').mockImplementation()`. – Estus Flask May 05 '20 at 14:24
  • Related question, https://stackoverflow.com/questions/1758576/multiple-left-hand-assignment-with-javascript – Estus Flask May 05 '20 at 14:24
  • Thank you @EstusFlask, I appreciate the explanation and the spyOn alternative! – Cold Fridge May 06 '20 at 12:03

1 Answers1

1

This expression assigns the last value to multiple variables. Example:

const a = b = c = d;

This means:

c = d;
b = c;
const a = b;

As a result, a, b, c end up having the value of d.

Robo Robok
  • 21,132
  • 17
  • 68
  • 126