如何设置从JS中的函数返回的两个全局对象?

时间:2018-02-28 14:51:15

标签: javascript unit-testing jest

我是Javascript和Jest的新手,我有这个代码:

const foo = () => {
  const fun = //some function
  const obj = //some object
  //...
  return {fun, obj};
}

describe('Battery test', () =>
  let fun = ()=>{};
  let obj = {};
  beforeEach(() => {
    {fun, obj} = foo();
  }
}

这会产生错误:

Unexpected token

  > 36 |         {fun, obj} = foo();
       |                    ^                        

我想到的唯一(超级丑陋)解决方法是:

describe('Battery test', () =>
  let fun = ()=>{};
  let obj = {};
  beforeEach(() => {
    const wrapper = foo();
    fun = wrapper.fun;
    obj = wrapper.obj;
  }
}

还有其他方法吗?

1 个答案:

答案 0 :(得分:4)

 ({fun, obj} = foo());

对象解构需要从块语句中消除。