question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Jest preset and Yarn Workspaces

See original GitHub issue

Is this a bug report?

Yes, or maybe incompatibility with a Yarn feature is more accurate

Have you read the Contributing Guidelines?

Yes

Environment

Environment: OS: macOS Sierra 10.12.6 Node: 8.9.1 Yarn: 1.3.2 npm: 5.5.1 Watchman: 4.7.0 Xcode: Xcode 9.2 Build version 9C40b Android Studio: 2.2 AI-145.3360264

Packages: (wanted => installed) react: 16.0.0 react-native: 0.51.0

Target Platform: N/A

Steps to Reproduce

  1. react-native init rnproject
  2. rm -rf rnproject/node_modules
  3. Add package.json:
{
  "private": true,
  "workspaces": [
    "rnproject"
  ]
}
  1. yarn
  2. cd rnproject
  3. yarn test
● Validation Error:

  Module <rootDir>/node_modules/react-native/jest/assetFileTransformer.js in the transform option was not found.

  Configuration Documentation:
  https://facebook.github.io/jest/docs/configuration.html

error Command failed with exit code 1.

Expected Behavior

React Native’s jest preset should work with projects using yarn workspaces, where modules may not be in the project’s node_modules directory (they may be hoisted in the parent dir’s node_modules.)

Actual Behavior

The jest preset provided by React Native specifies <rootDir>/node_modules/... in a couple places, which is not always the location of certain modules (i.e. when using Yarn workspaces):

"modulePathIgnorePatterns": [
    "<rootDir>/node_modules/react-native/Libraries/react-native/"
  ],
  "transform": {
    "^...$": "<rootDir>/node_modules/react-native/jest/assetFileTransformer.js"
  },
  "setupFiles": [
    "<rootDir>/node_modules/react-native/jest/setup.js"
  ],

Thus, Jest produces an error saying that it can’t find the files in the right spots.

The Yarn documentation on workspaces is opinionated on making assumptions about the package layout, I tend to agree:

The package layout will be different between your workspace and what your users will get (the workspace dependencies will be hoisted higher into the filesystem hierarchy). Making assumptions about this layout was already hazardous since the hoisting process is not standardized, so theoretically nothing new here.

But I’m also not sure how to achieve a layout-agnostic kind of a setup.

Reproducible Demo

https://github.com/dingbat/ws-rn-bug-example

Some fixes I tried

  • When running jest, specifying the root dir as the workspace root (--rootDir ..). This caused a bunch more errors and didn’t seem like a good direction, because we should be able to run tests isolated to a subdir anyway.
  • Copy and paste the RN config in with the paths modified for the hoisted node_modules. Tried this with and without "preset": "react-native", neither one worked
    • Using the preset still tried loading in a setup file from the RN preset (I think because it specifies an array) Module <rootDir>/node_modules/react-native/jest/setup.js in the setupFiles option was not found.
    • Not using the preset probably will never work… Cannot find module 'setupDevtools' from 'setup.js'
  • Symlink rnproject/node_modules/react-native to node_modules/react-native (not a real solve since this wouldn’t survive Git and would have to be some sort of build step). No luck, for some reason Jest still can’t find the asset transformer:
    ● Validation Error:
    
      Module <rootDir>/node_modules/react-native/jest/assetFileTransformer.js in the transform option was not found.
    
      Configuration Documentation:
      https://facebook.github.io/jest/docs/configuration.html
    

Note

My intuition tells me there’s no easy fix in this repo, and there’s much better luck of addressing it in the Jest repo. But since the broken jest-preset configuration is provided in this repo, I thought it would make more sense to open an issue here.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:30
  • Comments:24

github_iconTop GitHub Comments

3reactions
chucksellickcommented, Apr 8, 2018

This is a bit of a nightmare, if Yarn supported per-project nohoist then there would be an easy option, but instead I had to blanket nohoist both react-native and jest and all their dependencies (which as you can imagine is an awful lot of dependencies) to get around this. Latest version of react-native (0.55.1). Not sure why this issue is closed as it is definitely an incompatibility with a widely used feature of Yarn?

Read more comments on GitHub >

github_iconTop Results From Across the Web

A guide through The Wild Wild West of ... - AH Technology
This is were the fun starts! Setting up a mono repo from scratch. We will be using TypeScript, Yarn workspaces, Lerna, and Jest....
Read more >
Configuring Jest
Configuring Jest. The Jest philosophy is to work great by default, but sometimes you just need more configuration power.
Read more >
Can not find jest in subpackage of yarn workspace
I have jest installed "locally" in the package with all the tests. But if I check node_modules/ after a yarn install , I...
Read more >
Workspaces - Yarn
Workspaces are a new way to set up your package architecture that's available by default starting from Yarn 1.0. It allows you to...
Read more >
backpack-examples-with-jest-yarn-workspaces v0.6.3 - npm.io
Idea behind the example · New .babelrc file with presets: ["backpack-core/babel"] · Add babel-jest and jest-cli to devDependencies in package.json · Add "jest" ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found