Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[BUG]: Extra Command Output Breaks Discovery #43

Open
StevanFreeborn opened this issue Jan 24, 2025 · 1 comment · May be fixed by #44
Open

[BUG]: Extra Command Output Breaks Discovery #43

StevanFreeborn opened this issue Jan 24, 2025 · 1 comment · May be fixed by #44

Comments

@StevanFreeborn
Copy link

Problem

Scenario: Playwright config command produces warnings as part of output

GIVEN the following command produces output that is not just JSON.

playwright test --list --reporter=json
Warning: Cannot polyfill `DOMMatrix`, rendering may be broken.
Warning: Cannot polyfill `Path2D`, rendering may be broken.
{
  "config": {
   ...many more lines
  }
}

WHEN discovery is performed

THEN no tests are found.

Logs

WARN | 2025-01-23T22:51:17Z-0600 | ...al/nvim-data/lazy/neotest/lua/neotest/lib/subprocess.lua:164 | CHILD | Error in remote call Vim:E474: Unidentified byte: Warning: Cannot polyfill `DOMMatrix`, rendering may be broken.
Warning: Cannot polyfill `Path2D`, rendering may be broken.
{
  "config": {
    "configFile": "C:\\Users\\sfree\\software_projects\\OnspringEnd2EndTests\\playwright.config.ts",
    "rootDir": "C:/Users/sfree/software_projects/OnspringEnd2EndTests/tests",
    "forbidOnly": false,
    "fullyParallel": true,
    "globalSetup": null,
    "globalTeardown": null,
    "globalTimeout": 0,
    "grep": {},
    "grepInvert": null,
    "maxFailures": 0,
    "metadata": {
      "environment": "QA"
    },
    "preserveOutput": "always",
    "reporter": [
      [
        "json"
      ]
    ],
    "reportSlowTests": null,
    "quiet": false,
    "projects": [
      {
        "outputDir": "C:/Users/sfree/software_projects/OnspringEnd2EndTests/test-results",
        "repeatEach": 1,
        "retries": 0,
        "metadata": {
          "environment": "QA"
        },
        "id": "setup",
        "name": "setup",
        "testDir": "C:/User
stack traceback:
	[C]: in function 'json_decode'
	...neotest-playwright/lua/neotest-playwright/playwright.lua:225: in function 'run'
	...neotest-playwright/lua/neotest-playwright/playwright.lua:196: in function 'get_config'
	...y/neotest-playwright/lua/neotest-playwright/discover.lua:254: in function 'refresh_data'
	...y/neotest-playwright/lua/neotest-playwright/discover.lua:248: in function 'func'
	...al/nvim-data/lazy/neotest/lua/neotest/lib/subprocess.lua:156: in function <...al/nvim-data/lazy/neotest/lua/neotest/lib/subprocess.lua:155>
	[C]: in function 'xpcall'
	...al/nvim-data/lazy/neotest/lua/neotest/lib/subprocess.lua:155: in function <...al/nvim-data/lazy/neotest/lua/neotest/lib/subprocess.lua:154>

Proposed Solution

Obviously a fix can be achieved for this by addressing the warnings or additional output being produced I believe my just making sure to parse the beginning and ending of the JSON here we can handle this more gracefully

const run = (cmd: string) => {
const [handle, errmsg] = io.popen(cmd);
if (typeof errmsg === 'string') {
logger('error', errmsg);
}
if (!handle) {
emitError(`Failed to execute command: ${cmd}`);
return;
}
const output = handle.read('*a');
handle.close();
if (typeof output !== 'string') {
emitError(`Failed to read output from command: ${cmd}`);
return;
}
if (output === '') {
emitError(`No output from command: ${cmd}`);
return;
}
const decoded = vim.fn.json_decode(output) as P.JSONReport;
return decoded;
};

I'm thinking something like this will do the trick:

const jsonMatch = string.match(output, "%b{}");

if (!jsonMatch) {
        emitError(`Failed to parse JSON output: ${output}`);
        return;
}

const jsonString = jsonMatch[0];

try {
        const decoded = vim.fn.json_decode(jsonString) as P.JSONReport;
        return decoded;
} catch (err) {
        if (err instanceof Error) {
                emitError(`Failed to decode JSON: ${err.message}`);
        }
        return;
}
@StevanFreeborn StevanFreeborn linked a pull request Jan 24, 2025 that will close this issue
@StevanFreeborn
Copy link
Author

There is a fork here for use in the mean time: https://github.com/StevanFreeborn/neotest-playwright/tree/fork

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant