Repository: pyscript/pyscript
File: pyscriptjs/tests/integration/support.py (Python)

Code before:
                ...

        If an error was expected but not found, it raises
        DidNotRaiseJsError().

        If there are MORE errors other than the expected ones, it raises an
        exception:

          - if it finds a single JS error, raise JsError
          - if it finds multiple JS errors, raise JsMultipleErrors

        Upon return, all the errors are cleared, so a subsequent call to
        check_js_errors will not raise, unless NEW JS errors have been reported
        in the meantime.
        """
        expected_messages = list(expected_messages)
        js_errors = self._js_errors[:]

        for i, msg in enumerate(expected_messages):
            for j, error in enumerate(js_errors):
                if msg is not None and error is not None and msg in error.message:
                    # we matched one expected message with an error, remove both
                    expected_messages[i] = None
                    js_errors[j] = None

        # now expected_messages and js_errors should contain only Nones
        expected_messages = [msg for msg in expected_messages if msg is not None]
        js_errors = [err for err in js_errors if err is not None]
        self.clear_js_errors()

        if expected_messages:
            raise JsErrorsDidNotRaise(expected_messages, js_errors)

        if js_errors:
            raise JsErrors(js_errors)

    def clear_js_errors(self):
        """
        Clear all JS errors.
        """
        self._js_errors = []

    def writefile(self, filename, content):
        """
        Very thin helper to write a file in the tmpdir
        """
        f = self.tmpdir.join(filename)
        f.write(content)

    def goto(self, path):
        self.logger.reset()

Review comment (suggestion):
`should contain only Nones` --> `should contain only elements which are not None`

I guess you wrote the opposite.

Code after:
    def check_js_errors(self, *expected_messages):
        """
        Check whether JS errors were reported.

        expected_messages is a list of strings of errors that you expect they
        were raised in the page.  They are checked using a simple 'in' check,
        equivalent to this:
            if expected_message in actual_error_message:
                ...

        If an error was expected but not found, it raises
        DidNotRaiseJsError().

        If there are MORE errors other than the expected ones, it raises JsErrors.

        Upon return, all the errors are cleared, so a subsequent call to
        check_js_errors will not raise, unless NEW JS errors have been reported
        in the meantime.
        """
        expected_messages = list(expected_messages)
        js_errors = self._js_errors[:]

        for i, msg in enumerate(expected_messages):
            for j, error in enumerate(js_errors):
                if msg is not None and error is not None and msg in error.message:
                    # we matched one expected message with an error, remove both
                    expected_messages[i] = None
                    js_errors[j] = None

        # if everything is find, now expected_messages and js_errors contains
        # only Nones. If they contain non-None elements, it means that we
        # either have messages which are expected-but-not-found or errors
        # which are found-but-not-expected.
        expected_messages = [msg for msg in expected_messages if msg is not None]
        js_errors = [err for err in js_errors if err is not None]
        self.clear_js_errors()

        if expected_messages:
            # expected-but-not-found
            raise JsErrorsDidNotRaise(expected_messages, js_errors)

        if js_errors:
            # found-but-not-expected
            raise JsErrors(js_errors)

    def clear_js_errors(self):
        """
        Clear all JS errors.
        """
        self._js_errors = []