FluffySpoon.JavaScript.Testing.Faking icon indicating copy to clipboard operation
FluffySpoon.JavaScript.Testing.Faking copied to clipboard

Object destructuring + rest doesn't work

Open 0xjocke opened this issue 4 years ago • 3 comments

Describe the bug I cant use a Substitute mock for code that uses object destructuring + rest

To Reproduce

import Substitute from "@fluffy-spoon/substitute";

type Entity = { id: number; name: string; lastName: string };

const copyEntity = (entity: Entity) => {
  const { id, ...params } = entity;
  console.log(params);
};

const mockEntity = Substitute.for<Entity>();
mockEntity.id.returns!(1);
mockEntity.name.returns!('test name');
mockEntity.lastName.returns!('test last name');

copyEntity(mockEntity); // empty object
copyEntity({id:1, name: 'test name', lastName: 'test last name'})  // {name: 'test name', lastName: 'test last name'}

Codesandbox: https://codesandbox.io/s/flamboyant-sea-o5zf4?file=/src/index.ts:0-469

Expected behavior I should be able to test functions using destructuring with the rest pattern

Environment

  • Typescript version: 3.9.3
  • substitute.js version: 1.145.0
  • Node version: 12

0xjocke avatar Sep 17 '20 05:09 0xjocke

Thank you for the detailed report! Can confirm the bug ^^

notanengineercom avatar Sep 27 '20 22:09 notanengineercom

Maybe the proxy object has a method for destructuring that we're missing.

ffMathy avatar Sep 28 '20 05:09 ffMathy

Yes, we need to add a trap to handle the spread assignments!

notanengineercom avatar Sep 28 '20 06:09 notanengineercom