1

I learn javascript (yes, I know there are type- and coffeescript.). In an application which requires a function func(a, b) = () => {"a": a, "b": ...b }, I wish to be able to NOT spread b in case it is not spreadable. Is there some property flag that allows this kind of check? I know Python has mutable and non-mutable types, but I have never seen such isMutable(arg) as well. :-/

Application: Jest matcher toHaveBeenNthCalledWith accepts arguments (nthCall, arg1, arg2, ....) such that nthCall is compulsory and ...args are variadic, and allows the function below:

export const expectToHaveBeenNthCalledWith = (functionHandler, expectation) => {
  expect(functionHandler).toHaveBeenNthCalledWith(
    expectation.callIndex,
    ...expectation.args
  );
};

The unit-test of such function is somewhat given below. It works, fine! But... I wish to to able NOT to embrace the arguments on an array. :-/

describe("expectTo", () => {
  ...
  it(
    'assert Nth call with argument', 
    () => {
      const functionCaller = (functionCall, arg_1, arg_2) => {
        functionCall(arg_1);
        functionCall(arg_2);
      }
      
      const string_1 = "ackbar";
      const string_2 = "Boba Fett";
    
      const expectation_1 = {"callIndex": 1, "args": [string_1]};
      const expectation_2 = {"callIndex": 2, "args": [string_2]};
    
      const f = jest.fn();
    
      functionCaller(f, string_1, string_2);
      
      expectToHaveBeenNthCalledWith(f, expectation_1);
      expectToHaveBeenNthCalledWith(f, expectation_2);
    }
  );
  ...
})
  • What are you going to spread `b` into when it's spreadable? Right now it gives a syntax error when using `...` in this position. – Valentin Jan 31 '23 at 20:07
  • Are you asking for [Check if a value is an object in JavaScript](https://stackoverflow.com/q/8511281)? – VLAZ Jan 31 '23 at 20:10
  • I made an application example to make it clear. I know javascript have types like python (Check `typeof`), and spreadable means object somewhat. I try to write a function `isSpreadable(candidate)` that returns `true` for spreadable objects and `false` otherwise. – Bruno Peixoto Jan 31 '23 at 20:21
  • In case you say there is NO OTHER WAY except to array-embrace them, I will leave like this – Bruno Peixoto Jan 31 '23 at 20:23
  • 1
    `{ ...x }` and `fn(...x)` are two completely different things. The first one spreads an object's properties, the latter will take out all members of an iterable (like an array) and pass them as arguments. See [my answer here](https://stackoverflow.com/a/64612639). – VLAZ Jan 31 '23 at 21:03
  • "*The unit-test of such function is somewhat given below.*" - I don't get what that test is testing or what you mean by "somewhat". – Bergi Jan 31 '23 at 23:20
  • This is a meta-test: I test the function that test. In this case, I wish to make the interface compatible and intuitive between jest-provided and facade-library – Bruno Peixoto Feb 01 '23 at 01:08

0 Answers0