Karma: use Windows' Chrome from WSL

Viewed 6250

I am trying to launch karma from WSL using the Windows version of Google Chrome.

In the karma.conf.js I simply use the Chrome browser:

[...],
browsers: ['Chrome'],
[...]

And I export the CHROME_BIN environment variable like this:

export CHROME_BIN='/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe'

Karma successfully find Google Chrome, and a new tab is opened at the right URL when Karma is launched. However, I get this error:

Google chrome can't read and write to its data directory /tmp/karma-XXXX

I tried starting chrome as administrator and changing the cache folder to the root of my project but it doesn't work.

I assumed there was a issue with the format of the path that Karma give to Chrome (WSL path vs Windows path).

So I create a custom karma launcher specifying the chromeDataDir:

browsers: ['WindowsChrome'],
customLaunchers: {
  WindowsChrome: {
    base: 'Chrome',
    chromeDataDir: 'D:\\'
  }
}

By doing that a I don't have the previous error, a new instance of Chrome is launched but Chrome seems unable to resolve the URL, and karma timeout. Moreover, a lot of Chrome folders are created inside my project.

Have someone already make karma work from WSL using Chrome or have any cue on what is going on ?

2 Answers

I found that if you create a C:\tmp\karma folder under Windows, this error goes away, and Chrome finds and uses this folder for temp files.

You can also change the temporary folder that Chrome uses for its data by setting the TEMP environment variable like so:

export TEMP='/path/to/your/folder'

The important thing is that C:\path\to\your\folder must exist under Windows.

Now in january 2022, on WSL2, tested on debian 11/WSL under windows 11, it's "easy" :

From windows command prompt :

SET CHROME_EXECUTABLE=C:\Program Files\Google\Chrome\Application\chrome.exe

Test :

echo %CHROME_EXECUTABLE%
C:\Program Files\Google\Chrome\Application\chrome.exe

Share this Windows env variable with WSL (doc. https://devblogs.microsoft.com/commandline/share-environment-vars-between-wsl-and-windows/) :

set WSLENV=CHROME_EXECUTABLE/p

From Windows command prompt, enter wsl, :

wsl

Verify the env variable is ok under wsl :

echo $CHROME_EXECUTABLE
/mnt/c/Program Files/Google/Chrome/Application/chrome.exe
Related