Introduce/improve check_js_errors and improve test_no_implicit_target (#874)

Until now, we didn't have a nice way to check that we expect a specific JS error in the web page.
This PR improves check_js_errors() so that now you can pass a list of error messages that you expect.
It is tricky because we need to handle (and test!) all various combinations of cases:

- errors expected and found / expected but not found
- unexpected errors found / not found

Moreover, JS exceptions now are logged in the special category console.js_error, which means that the printed text is also available using e.g. self.console.js_error.text or self.console.all.text. However, this should never be required and it's preferred to use self.check_js_errors to check for exceptions. This fixes #795 .

Finally, use the new logic to improve test_no_implicit_target.
This commit is contained in:
Antonio Cuni
2022-10-24 16:24:52 +02:00
committed by GitHub
parent f9194cc833
commit 58f7c2137d
6 changed files with 280 additions and 106 deletions

View File

@@ -5,7 +5,7 @@ import tempfile
import pytest
import requests
from .support import JsError, PyScriptTest
from .support import PyScriptTest
URL = "https://github.com/pyodide/pyodide/releases/download/0.20.0/pyodide-build-0.20.0.tar.bz2"
TAR_NAME = "pyodide-build-0.20.0.tar.bz2"
@@ -105,29 +105,32 @@ class TestConfig(PyScriptTest):
assert version == "0.20.0"
def test_invalid_json_config(self):
with pytest.raises(JsError) as exc:
self.pyscript_run(
snippet="""
<py-config type="json">
[[
</py-config>
"""
)
msg = str(exc.value)
assert "SyntaxError" in msg
# we need wait_for_pyscript=False because we bail out very soon,
# before being able to write 'PyScript page fully initialized'
self.pyscript_run(
"""
<py-config type="json">
[[
</py-config>
""",
wait_for_pyscript=False,
)
self.page.wait_for_selector(".py-error")
self.check_js_errors("Unexpected end of JSON input")
def test_invalid_toml_config(self):
with pytest.raises(JsError) as exc:
self.pyscript_run(
snippet="""
<py-config>
[[
</py-config>
"""
)
msg = str(exc)
assert "<ExceptionInfo JsError" in msg
# we need wait_for_pyscript=False because we bail out very soon,
# before being able to write 'PyScript page fully initialized'
self.pyscript_run(
"""
<py-config>
[[
</py-config>
""",
wait_for_pyscript=False,
)
self.page.wait_for_selector(".py-error")
self.check_js_errors("SyntaxError: Expected DoubleQuote")
def test_multiple_py_config(self):
self.pyscript_run(