javascriptyamlplaywright

How to get rid of double quote missing error in aria snapshot?


I'm using the method toMatchAriaSnapshot. I have dynanic text so I'm generating my snapshot text like that:

await expect(page.locator('#contract')).toMatchAriaSnapshot(`
     - dialog:
       - document:
         - heading "Add" [level=5]
         - button "Close"

         - text: "` + (error["login"] ?? "") + messages["login"] + ` *"
         - textbox
`);

But, sometimes it will require the quote because of the content of error["login"]. I will get message like this: Missing information. Error code : A-002 Identifiant *.

When the error["login"] is empty, I will get this output:

-     - text: "Identifiant *"
+     - text: Identifiant *

the error I'm getting

When not using quotes, and when I have the full message, here is the result:

Error: expect.toMatchAriaSnapshot: Nested mappings are not allowed in compact mappings at line 13, column 13:

    - text: Missing information. Error code : A-002 Identifiant *
            ^

So, I need the double quote, but sometimes playwright does NOT want I put quotes.

How can I make playwright accept quote when they are not required?


Solution

  • To fix this, I made my own method to made the text line like this:

    function w(s) {
        return (s.includes && s.includes(":") && s.includes(" ")) || typeof s == 'number' ? "\"" + s + "\"" : s;
    }
    
    function getTextbox(val, disabled = false) {
        return val == null || val == "" ? "textbox" + (disabled ? "[disabled]" : "") : "textbox" + (disabled ? " [disabled]" : "") + ": " + w(val);
    }
    

    The function w is to wrap content into quoted (or not) content.

    I'm using it like this:

    await expect(page.locator('#contract')).toMatchAriaSnapshot(`
         - dialog:
           - document:
             - heading "Add" [level=5]
             - button "Close"
             - text: ` + w((error["login"] ?? "") + messages["login"] + " *") + `
             - ` + getTextbox(contract.LoginId ?? "") + `
    `);
    

    A better workaround would be, as discussed in comment, to have real snapshot with only text that will be updated more automatically