Skip to content

Instantly share code, notes, and snippets.

@rix0rrr
Created April 29, 2020 17:20
Show Gist options
  • Save rix0rrr/c62608a504987fc63d95b789a78a529a to your computer and use it in GitHub Desktop.
Save rix0rrr/c62608a504987fc63d95b789a78a529a to your computer and use it in GitHub Desktop.
make sure subdir exists

Jest/Graceful-FS Bug Report

This repository demonstrates how Jest version 25.5.0 breaks the functionality of path.resolve() after chdiring, if you happen to have another (older) version of graceful-fs already in your dependencies.

Analysis

Having different versions of graceful-fs in the closure will prevent full deduplication of dependencies, which leads to (for some reason), process.chdir() in the test and process.cwd() as used by the path module lead to be different graceful-fs modules, which makes their cwd-caches be different and makes path.resolve() fail.

Validity

In this demo I brought in the older graceful-fs by depending on it directly using my own package.json, and so it would be easy enough to change.

In an actual practical case where we encountered this graceful-fs was being brought in by another dependency, which we don't control. Hence we have no control over the versions in the tree and the deduplication.

{
"name": "jest-is-broken",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"graceful-fs": "4.2.3",
"jest": "^25.5.0"
}
}
const path = require('path');
test('path.resolve is sane', () => {
// Reasons why this might not be true:
// graceful-fs, which is used by Jest, hooks into process.cwd() and
// process.chdir() and caches the values, but process.chdir() here
// and the process.cwd() used inside path.resolve() are different
// copies of graceful-fs, for some reason.
const targetDir = path.join(__dirname, 'subdir');
process.chdir(targetDir);
expect(process.cwd()).toEqual(targetDir);
const resolved = path.resolve('.');
expect(resolved).toEqual(targetDir);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment