jest does not "activate" babel support when running on windows on a network-share

Created on 14 Feb 2018  ·  14Comments  ·  Source: facebook/jest

Do you want to request a _feature_ or report a _bug_?

bug

What is the current behavior?

i am running jest in windows, in a directory that is a network-share (for example: \\server1\ is mounted as G:, and i am running the tests in G:\src\). (technically this is a mac-laptop, with parallels, windows in parallels, and my macos-directory is shared into the parallels-windows, and i am running jest on that shared directory in windows; but i assume this problem applies in general to any case where running on a network-drive in windows).

there is a .babelrc in the directory, so jest should automatically apply it but it does not, and then it fails with a syntaxerror on an import statement.

if i copy my project from this shared-disk to the C:\ drive that is "native" windows, and run jest there, jest works correctly.

i can also workaround the issue by running jest --rootDir . instead of jest.

If the current behavior is a bug, please provide the steps to reproduce and
either a repl.it demo through https://repl.it/languages/jest or a minimal
repository on GitHub that we can yarn install and yarn test.

it can be reproduced with the examples/react project from the jest-source-code:

have a network-share in windows, and copy the jest-source-code there. go to the examples/react folder, and try to run the tests there.

What is the expected behavior?

the tests should not fail.

Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.

jest v22.3.0, windows 10, nodev8.9.4, yarn1.3.2

i tried to debug the issue and find the core of the problem, and it seems the following is happening:

  • the network-drive is mounted as a disk-drive in windows, so the directory with the tests is available at (for example) G:\src\ . but the real name of that place is \\server1\src . when i go to G:\src\, and start jest, it looks up the full-name of the current-directory, and somehow it gets the network-ish name (\\server\src). and it gets confused by the double-backslash at the beginning. but if i run it as jest --rootDir ., i assume it stays with the G:\src\ name, and then everything is ok.
Help Wanted Windows

Most helpful comment

Any updates on this?

I'm running babel 7.
My jest.config.js looks like this:

// @ts-ignore
module.exports = {
  testEnvironment: 'node',
  roots: ['<rootDir>/src'],
  testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
  collectCoverageFrom: ['src/**/*.{ts,tsx}'],
  clearMocks: true,
  setupFilesAfterEnv: ['<rootDir>/src/setupEnzyme.ts'],
  moduleNameMapper: {
    '\\.(gif|ttf|eot|svg)$': '<rootDir>/__mocks__/fileMock.js',
  },
};

And my .babel.config.js is

module.exports = {
  presets: ['@babel/preset-env', '@babel/preset-typescript'],
  plugins: [
    '@babel/proposal-class-properties',
    '@babel/proposal-object-rest-spread',
  ],
};

When running either jest --rootDir . or jest, I'm getting the following error:

● Validation Error:

  Module ts-jest in the transform option was not found.
         <rootDir> is: \\Mac\Home\Dev\

All 14 comments

I might be running into a similar problem but I'm not entirely sure it's related. When running jest 22.4.x I get the Unexpected token import syntax error but 22.3.0 appears to be fine in my case. Same story as the OP, I have .babelrc but I don't think it's being used. Adding the --rootDir . option doesn't resolve the issue for me though.

I'm also running Windows but I'm not on a network drive, I'm running inside WSL. Since I'm not quite sure how WSL handles the sharing of drives I suspect it's similar to that of a network drive but correct me if I'm wrong and this issue isn't related at all 😇

For what it's worth I'm running into these issues on the svg-spritemap-webpack-plugin#webpack4 repo. I'm going to stick to 22.3.0 for now as that specific version seems to be working correctly on my end.

@cascornelissen can you try to uninstall jest and then reinstall it? I can see in your lockfile that some versions of the different jest packages have drifted.

It's also recommended to install babel-jest explicitly if you rely on babel for transpilation

@SimenB, that does indeed seem to solve my issue. Sorry for interfering with the original issue!

I _think_ I found the problem

tl;dr from #6051

It's in how path#join (now?) handles network paths at path.join(prefix, aPath, moduleDir); on Line 62

const path = require('path');

let prefix = '\\\\';
const aPath = '\\\\localhost\\C$\\test\\test-typescript-app';
const moduleDir = 'nodule_modules';

path.join(prefix, aPath, moduleDir); // returns '\\localhost....'  : Bad

prefix = '';
path.join(prefix, aPath, moduleDir); // returns '\\\\localhost....' : Good

Changing this line 38 to prefix = ''; gets jest to actually make it to the tests.

In the sample project I posted in the other issue, the tests don't get found, I just think this is the regex and I'm a test noob. I got the tests in my other project to run(with the Jest Extension) by changing line 38 in node_modules\jest-resolve\build\node_modules_paths.js.

Maybe Line 35 should be

 if (process.platform === 'win32') {
    prefix = '';
  } 

I've also got steps to reproduce the issue by making a network drive from \\localhost\c$ on #6051

I ran this on docker images node[6,8,9]-alpine

console.log(process.version + ": should be false:", "\\\\localhost\\C$\\test" == path.win32.join("\\\\", "\\\\localhost\\C$\\test")); 
console.log(process.version + ": should be true:", "\\\\localhost\\C$\\test" == path.win32.join("", "\\\\localhost\\C$\\test"))'

and got these results:

v6.14.1: should be false: false
v6.14.1: should be true: true

v8.11.1: should be false: false
v8.11.1: should be true: true

v9.11.1: should be false: false
v9.11.1: should be true: true

cmd:
docker run --rm node:9-alpine node -e 'console.log(process.version + ": should be false:", "\\\\localhost\\C$\\test" == path.win32.join("\\\\", "\\\\localhost\\C$\\test")); console.log(process.version + ": should be true:", "\\\\localhost\\C$\\test" == path.win32.join("", "\\\\localhost\\C$\\test"))'

Should probably be good with suggested fix

Feel free to send a PR with that change :)

I had this same issue with the most recent Babel 6.x and 7.x releases which only manifested when running from a Windows network share path. I believe it is related to Babel changes explained in #6053. The workaround given of using babel.config.js with Babel 7 fixed this problem for me; I wasn't able to get this PR to work.

Any updates on this?

I'm running babel 7.
My jest.config.js looks like this:

// @ts-ignore
module.exports = {
  testEnvironment: 'node',
  roots: ['<rootDir>/src'],
  testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
  collectCoverageFrom: ['src/**/*.{ts,tsx}'],
  clearMocks: true,
  setupFilesAfterEnv: ['<rootDir>/src/setupEnzyme.ts'],
  moduleNameMapper: {
    '\\.(gif|ttf|eot|svg)$': '<rootDir>/__mocks__/fileMock.js',
  },
};

And my .babel.config.js is

module.exports = {
  presets: ['@babel/preset-env', '@babel/preset-typescript'],
  plugins: [
    '@babel/proposal-class-properties',
    '@babel/proposal-object-rest-spread',
  ],
};

When running either jest --rootDir . or jest, I'm getting the following error:

● Validation Error:

  Module ts-jest in the transform option was not found.
         <rootDir> is: \\Mac\Home\Dev\

@ivancuric Hey man did you figure out a solution? Im having the exact same issue on RN 0.59 AFTER upgrading from 0.56.

I arrived at this page when getting the fore-mentioned error.

MY PROBLEM WAS: I have ts-jest installed globally ONLY but I forgot to install it in my local project. I only had jest installed so the global ts-jest kicked in. After local install of ts-jest I was good to go.

Mostly likely my fix is completely unrelated to previous comments.

@AckerApple installing it locally worked a treat. Thank you!

It's still broken!!

I work on Mac with Windows in Parallels with shared directory
Jest is OK on MAC, but the same issue on Windows

I use the solution of jhr007

Modifying the function 'nodeModulesPaths' (path 'node_modules/jest-resolve/build/nodeModulesPaths.js')

after :

...
if (/^([A-Za-z]:)/.test(basedirAbs)) {
    prefix = '';
  } else if (/^\\\\/.test(basedirAbs)) {
    prefix = '\\\\';
  } // The node resolution algorithm (as implemented by NodeJS and TypeScript)
  // traverses parents of the physical path, not the symlinked path
 ```

by adding : 
```javacsript
 if (process.platform === 'win32') {
    prefix = '';
  } 
Was this page helpful?
0 / 5 - 0 ratings