![](/img/trans.png)
[英]SyntaxError: JSON Parse error: Unexpected identifier “object” (anonymous function)
[英]Jest unit test 'Function' returns SyntaxError: Unexpected identifier at Function (<anonymous>)
我們有一個調用 wraps 傳單的模板應用程序,樣式函數需要將函數作為方法參數,模板不能這樣做,所以我們傳遞一個對象,然后將其解析為函數,無論如何這有效但試圖用 Jest 測試這給我們帶來了問題。
代碼
public onStylesChange(newVal) {
this.geoJSONstyles = Function('return ('+newVal+')')(); //
if (this.geoJSON) {
this.geoJSON.setStyle(this.geoJSONstyles);
}
}
}
測試
it('Should accept a function parameter', () => {
cut.onObjChange({test:'object'});
// expect the cut.geoJSONStyles property to be of type Function.
expect(typeof cut.geoJSONstyles).toBe('Function');
})
返回
SyntaxError: Unexpected identifier
at Function (<anonymous>)
我該如何解決這個問題?
不知道去年我的頭在哪里,但休息一下讓我的想法變得清晰。
我需要做的是在評論中向我指出的,所以感謝它,是檢查類型,我也可以將函數作為字符串返回並檢查。 (忽略函數的作用,只是為了測試賦值)所以
it('Should accept a string function and eval to a function', () => {
const testString = 'function () {return 0 }'
cut.onObjChangetest(String);
expect(typeof cut.onEachFeature === 'function').toBeTruthy();
expect(cut.geoJSONstyles.toString()).toBe(testString);
})
完美運行,測試通過,哇哦! 謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.