简体   繁体   English

JSON.parse 位置 0 处的 JSON 中的意外标记 u

[英]Unexpected token u in JSON at position 0 at JSON.parse

I am trying to write jest test case for submit function which has JSON.parse involed in it.我正在尝试为包含 JSON.parse 的提交函数编写 jest 测试用例。 Below is the code and test case for it.下面是它的代码和测试用例。

handleFormSubmit = (e) => {
        e.preventDefault();
        let requestData = JSON.parse(JSON.stringify(this.props.tempAddRequest));
            if (e.target[1].value.length === 0) {
                this.setState({ rrNumberFeedback: true, rrNumberErr: FIELD_ERROR });
            }
}

 it("should check if rr is null on clicking submit button in modal", () => {
        const instance = wrapper.instance();
        const e = {
            target: [{ name: "branchNumber", value: "" }, { name: "rrNumber", value: "" }, { name: "crdNumber", value: "1234" }],
            preventDefault: () => { }
        }
        instance.handleFormSubmit(e);
        expect(wrapper.state('rrNumberErr')).toEqual("E");
        expect(wrapper.state('rrNumberFeedback')).toEqual(true);
    });

参考这张图片 If the handleFormSubmit involves JSON.parse() you need to send the stringified JSON in the value of event如果handleFormSubmit涉及JSON.parse()需要在 event 的值中发送字符串化的 JSON

try changing event as尝试将event更改为

const e = {
        target: JSON.stringify([{ name: "branchNumber", value: "" }, { name: "rrNumber", value: "" }, { name: "crdNumber", value: "1234" }]),
        preventDefault: () => { }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获到的SyntaxError:JSON中的意外令牌u在JSON.parse的位置0 - Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse 语法错误:在 JSON 中的意外令牌 u 在 position 0 在 JSON.parse - SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse SyntaxError:JSON中的意外令牌u在JSON.parse( <anonymous> ) - SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) 意外的令牌:你的JSON.parse()问题 - Unexpected token: u JSON.parse() issue 具有JSON.parse的JSON中位置0处的意外令牌&lt; - Unexpected token < in JSON at position 0 with JSON.parse JSON 中的意外令牌 a 在 position 0 在 JSON.parse - unexpected token a in JSON at position 0 at JSON.parse Uncaught SyntaxError: JSON.parse (<anonymous> ) 在 Response.Body.json</anonymous> - Uncaught SyntaxError: Unexpected token U in JSON at position 0 at JSON.parse (<anonymous>) at Response.Body.json JSON. 解析 position 0 处的意外令牌 - JSON.parse unexpected token s at position 0 VM299:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous> ) - VM299:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) 未捕获的语法错误:JSON 中的意外令牌 u 在 JSON.parse 的 position 0 处(<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM