Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

ENOENT: no such file or directory, open '/private/var/folders/ #364

Open
YonatanKra opened this issue Aug 7, 2022 · 3 comments
Open

ENOENT: no such file or directory, open '/private/var/folders/ #364

YonatanKra opened this issue Aug 7, 2022 · 3 comments

Comments

@YonatanKra
Copy link

Using mock-fs gives me the following error:

ENOENT: no such file or directory, open '/private/var/folders/_4/y5tgvqvx12n9gyl97xvdgpxm0000gq/T/jest_dz/ts-jest/ce/607fabb27dcc5bf120948e1a795d93148671b2/e9a8ee471bc676fe755c57379268f21a1390a7a4'

The stack trace is:

at TsJestTransformer.getCacheKey (../../node_modules/ts-jest/dist/ts-jest-transformer.js:245:40)
      at ScriptTransformer._getCacheKey (../../node_modules/@jest/transform/build/ScriptTransformer.js:281:41)
      at ScriptTransformer._getFileCachePath (../../node_modules/@jest/transform/build/ScriptTransformer.js:352:27)
      at ScriptTransformer.transformSource (../../node_modules/@jest/transform/build/ScriptTransformer.js:595:32)
      at ScriptTransformer._transformAndBuildScript (../../node_modules/@jest/transform/build/ScriptTransformer.js:765:40)
      at ScriptTransformer.transform (../../node_modules/@jest/transform/build/ScriptTransformer.js:822:19)

Any idea what can cause that?

@mixn
Copy link

mixn commented Apr 11, 2023

@YonatanKra Did you manage to somehow solve this issue? I’m just doing

mock({
  'path/to/file.txt': 'file content here',
});

as it says in the docs and getting the same error. Tried different node versions and package versions, too, but nothing changed.

Thanks in advance!

@BenJeaterSS
Copy link

I am leaving this here for anyone who needs it in the future.

My testing says that you need to turn off the mocking between tests because Jest can't do its own internal logging. That's the best reason I can come up with anyway. Info in the README

You also need to turn off the mocking if you're doing anything with snapshots. The README has this information at the bottom.

import * as mockFs from 'mock-fs';

describe('your test', () => {
  beforeEach(() => {
    mockFs({
      // Mocked file system
    });
  });

  afterEach(() => {
    mockFs.restore();
  });

  // Your tests
});

@isocroft
Copy link

isocroft commented Jul 27, 2024

Hello @YonatanKra an @mixn ,

All files created with mock-fs using relative paths are relative to process.cwd().

So, ensure you want to create the file in the current working directory (which in this case will be the root folder where a nodejs process is created from to run your tests).

Or use __dirname which is the path to the folder where your test file is located for absolute paths to files.

import * as mockFs from 'mock-fs';
import path from 'path';

describe('your test', () => {
  beforeEach(() => {
    const rootPublicFolderPath = path.join(__dirname, '..', 'public')

    mockFs({
      'path/to/file.txt': 'file content here',
      [rootPublicFolderPath]: mockFs.directory({
        items: {
           'my-file.txt': 'hello world'
        }
      })
    });
  });

  afterEach(() => {
    mockFs.restore();
  });

  test('test reading a file', () => {
     const file = fs.readFileSync(`${process.cwd()}\path\to\file.txt`)
  })
});

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

No branches or pull requests

4 participants