我是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;
}
}
还有其他方法吗?
答案 0 :(得分:4)
({fun, obj} = foo());
对象解构需要从块语句中消除。