pestphp / pestphp/pest

[Bug]: assertNoJavaScriptErrors() on browser plugin doesn't show line number of the error

Open
#1,649 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
PHP
Stars
11.7k
Forks
538
Avg merge
4d 11h
Merged PRs (30d)
8

Description

What Happened

Hi!

I expect that in the failure message of the assertNoSmoke() and assertNoJavaScriptErrors() the filename and line number where the error is thrown shows up in the message. Part of the stack trace may be good too!

The case is I'm using LLMs and agents to code, but when the test fails it doesn't know where to look for the error exactly, so it keeps in a loop of guessing.

In my case the error message in the test failure was:
`The following JavaScript errors were found:

  • Uncaught TypeError: Cannot read properties of undefined (reading 'toString')`.

This doesn't help.

I did a workaround for now in my Pest.php file:

expect()->extend('hasNoJavaScriptErrors', function () {
    $script = 'window.__pestBrowser.jsErrors';
    $result = $this->value->script($script);

    if(!empty($result)) {
        $errors = collect($result)->map(function ($item) {
            return [
                'message' => $item['message'],
                'filename' => $item['filename'],
                'line_number' => $item['lineno'],
                'column_number' => $item['colno'],
            ];
        })->map(function ($item) {
            return $item['message'] . " at " . $item['filename'] . ' line number ' . $item['line_number'] . ' column number ' . $item['column_number'];
        })->last();
        
        test()->fail('JavaScript errors found: ' . $errors);
    }

    return $this;
});

I use it like this: expect($page)->hasNoJavaScriptErrors();

The output is:

JavaScript errors found: Uncaught TypeError: Cannot read properties of undefined (reading 'toString') at http://127.0.0.1:5173/resources/js/routes/a/b/index.ts?t=1772473135604 line number 124 column number 70

it may help if my agent access this URL and analyze the output (it doesn't do that), but the error in the browser shows like this:

index.ts:264 Uncaught TypeError: Cannot read properties of undefined (reading 'toString')
    at show.url (index.ts:264:54)
    at show (index.ts:226:15)
    at breadcrumbs (show.tsx:49:19)
    at ShowStudentPage (show.tsx:58:33)
show.url	@	index.ts:264
show	@	index.ts:226
breadcrumbs	@	show.tsx:49
ShowStudentPage	@	show.tsx:58

this one would be the best!

They link to different files. The __jestBrowser.jsErrors uses the file without source map. The error shown in the browser console links to file from source map.

how can we improve that?

thank you!

How to Reproduce

It's a Laravel 12 + Inertia + React project using Pest as the test framework. In my case what was causing the error was that a prop in the page component was being accessed wrong. Laravel replied with a Resource and the page prop was being accessed not considering the data is inside the data key causing the wayfinder function to call toString() in undefined.

show.url = // ...
    // ...
    return show.definition.url
            .replace('{student}', parsedArgs.student.toString()) // here student was undefined. This is line 264.
            .replace(/\/+$/, '') + queryParams(options)
   }
Sample Repository

No response

Pest Version

4.4.1

PHP Version

8.5.3

Operation System

Linux

Notes

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Locate the browser plugin implementation of assertNoJavaScriptErrors() and the handling of window.__pestBrowser.jsErrors. Compare the current failure message with the workaround's filename, lineno, colno, and stack information, then inspect the relevant browser-plugin tests. Done means JavaScript assertion failures expose useful source location or stack details, including behavior when source maps are involved.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, php
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.