neotest-jest icon indicating copy to clipboard operation
neotest-jest copied to clipboard

fix(windows): path reducing for jest pattern

Open eugeny-dementev opened this issue 1 year ago • 3 comments

Fix for https://github.com/nvim-neotest/neotest-jest/issues/113

There are few issues with it on windows:

  • First issue is there a huge difference how vim.fn.expand("%") works on windows and unix: for file __tests__/file.test.ts in nvim running command :lua =vim.fn.expand("%") will show unix: __tests__/file.test.ts windows: C:/project\__tests__/file.test.ts
    • neotest uses that internal function to get current file and pass result to adapters
  • Second is jest not exactly expecting pattern to be full path to the file. I'm not sure but I assume jest always run pattern search on current directory and providing full path just makes so there is no files matching that pattern ever
    • Also jest seem to expect pattern to be unix style always, no matter the OS it's running on

Solution in the PR is to reduce windows version of path to the same pattern it is for unix: C:/project\__tests__/file.test.ts => __tests__/file.test.ts.

Fix is universal, on unix vim.fn.substitute (string replace) just doesn't do anything since path already lacking cwd part

I've tested it on both windows and ubuntu and it seem to work as intended on both platforms

Help needed with writing tests because ./scripts/test throws an error for me so I have no idea if I broke something

Error detected while processing command line:
E492: Not an editor command: PlenaryBustedDirectory tests/ {minimal_init = 'tests/init.vim'}

eugeny-dementev avatar Apr 03 '24 08:04 eugeny-dementev

I had the same issues with running the tests locally, raised an issue for it here #114 to try get some help from others.

conermurphy avatar Apr 04 '24 12:04 conermurphy

I'm using Neovim in Windows 11 with PowerShell 7.4.1 and it only works for me after I apply your code and remove the escapeTestPattern call.

JanPeter avatar Apr 12 '24 08:04 JanPeter

I'm using Neovim in Windows 11 with PowerShell 7.4.1 and it only works for me after I apply your code and remove the escapeTestPattern call.

I've encountered the same issue. To fix this I've had to gsub out the backslash characters for it to work. It seems to normalize the path to C:\/....\/.... which windows does not like.

escapeTestPattern(reducePattern(cwd, pos.path)):gsub("\\", ""),

BitsAndDroids avatar Jun 05 '24 16:06 BitsAndDroids