jest: path.relative appears subtly broken in Jest 25

πŸ› Bug Report

Somewhere between 24.9 and 25.1, path.relative stopped caring about process.cwd(). path.relative, in addition to its arguments from and to should also take the current directory into account, but with Jest 25.1, it allows behaves as if executed from the initial working directory.

logging process.cwd() always indicates the expected location and not necessarily the location path.relative uses.

To Reproduce

Steps to reproduce the behavior:

/**
 * @file foo/bar/test-path-bug.spec.ts
 *
 * Run this with
 * 1. `npx jest --  --testPathPattern foo/bar/jest-path-bug.spec.js`
 * 2. `node ./foo/bar/jest-path-bug.spec.js`
 *
 * Note that the assertions hold for case 2, but the second assertion fails when
 * run by jest. It seems that jest muchs with either process.chdir or
 * path.relative such that path.relative can't work.
 */

'use strict';

const path = require('path');
const assert = require('assert');

function testBodyWithOrWithoutJest() {
  const from = __dirname;
  const to = 'foo';
  const executedFromRepoRoot = path.relative(from, to);
  assert.equal(executedFromRepoRoot, '..');

  process.chdir('/tmp');
  const executedFromTmp = path.relative(from, to);
  assert.notEqual(
    executedFromRepoRoot,
    executedFromTmp,
    'When we call path.relative from a different location, we should get a different result.'
  );
}

if (typeof describe !== 'undefined') {
  describe('jest', () => {
    it('does not break path.relative', testBodyWithOrWithoutJest);
  });
} else {
  testBodyWithOrWithoutJest();
}

Expected behavior

Whether executed by Jest 25 or node (or Jest 24), the preceding code should exit cleanly. However, when executed by Jest 25, the second assertion does not hold.

Link to repl or repo (highly encouraged)

Interestingly, I can’t get the above code to break in repl.it, but it breaks in two different projects on to two different macOS devices.

envinfo

  System:
    OS: macOS 10.15.3
    CPU: (4) x64 Intel(R) Core(TM) i7-4578U CPU @ 3.00GHz
  Binaries:
    Node: 12.16.1 - ~/.nvm/versions/node/v12.16.1/bin/node
    npm: 6.13.4 - ~/.nvm/versions/node/v12.16.1/bin/npm
  npmPackages:
    jest: ^25.1.0 => 25.1.0

(also happens with node 10)

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 18 (7 by maintainers)

Most upvoted comments

It appears to in this contrived example. I’ll have to check later to see what it would to take to get our monorepo to a single version of graceful-fs.