Search code examples
dockerjestjspermission-denied

jest --watch throws Error: EACCES: permission denied for directory owned by root


Problem

When I run jest --watch, I get a permission denied error on a directory in my repository, even if I use watchPathIgnorePatterns for the directory:

Error: EACCES: permission denied, scandir '/path/to/repo/.pgdata'
  • .pgdata/ is a directory used as a Docker volume for postgresql.
  • The directory is owned by root.

Solutions tried

I tried using watchPathIgnorePatterns in my jest config:

watchPathIgnorePatterns: ['<rootDir>/\\.pgdata/'],

I tried a similar pattern for modulePathIgnorePatterns and testPathIgnorePatterns.

I also tried setting the owner of .pgdata to my user with chown, but that required a change in the Docker setup that I don't want to make right now.


Solution

  • Using the roots config property fixed the problem:

    roots: ['<rootDir>/__tests__']
    

    This tells jest to only search for files in the __tests__ directory.

    Note: I only have tests in __tests__, and I don't have a src/ directory. If I did, it would probably have to be included as well, as shown in the example in the documentation.


    Even when using watchPathIgnorePatterns, the jest watcher still tries to run through all directories in the repository. Since it encounters a directory it does not have access to read from, it throws the error (tested on jest 28.1.0)

    This unrelated issue comment for jest hinted at the solution.