Jest-puppeteer: Yarn 2 with PNP mode cannot resolve jest-puppeteer preset correctly

Created on 15 Feb 2020  ·  6Comments  ·  Source: smooth-code/jest-puppeteer

🐛 Bug Report

● Validation Error:

  Preset jest-puppeteer is invalid:

  The "id" argument must be of type string. Received type object
  TypeError [ERR_INVALID_ARG_TYPE]: The "id" argument must be of type string. Received type object
    at validateString (internal/validators.js:112:11)
    at Module.require (internal/modules/cjs/loader.js:841:3)
    at require (internal/modules/cjs/helpers.js:74:18)
    at setupPreset (/x/.yarn/cache/jest-config-npm-25.1.0-d4275d52c2-2.zip/node_modules/jest-config/build/normalize.js:298:14)
    at normalize (/x/.yarn/cache/jest-config-npm-25.1.0-d4275d52c2-2.zip/node_modules/jest-config/build/normalize.js:643:15)
    at readConfig (/x/.yarn/cache/jest-config-npm-25.1.0-d4275d52c2-2.zip/node_modules/jest-config/build/index.js:220:68)
    at async readConfigs (/x/.yarn/cache/jest-config-npm-25.1.0-d4275d52c2-2.zip/node_modules/jest-config/build/index.js:401:26)
    at async runCLI (/x/.yarn/cache/@jest-core-npm-25.1.0-8e811ad5a3-2.zip/node_modules/@jest/core/build/cli/index.js:203:59)
    at async Object.run (/x/.yarn/cache/jest-cli-npm-25.1.0-6214862080-2.zip/node_modules/jest-cli/build/cli/index.js:216:37)

The problem is I have config in LIB repo and acutal tests in another APP repo, so I have to resolve preset from the LIB repo. But I can't just write require.resolve('jest-puppeteer') because there are no js files, only json config.

I tried to put it like this:

exports.preset = require('path').dirname(require.resolve('jest-puppeteer/jest-preset.json'));

It works but it produces next error:

Test environment jest-environment-puppeteer cannot be found. Make sure the testEnvironment configuration option points to an existing node module.

Because jest-environment-puppeteer should be resolved from jest-puppeteer.

I suggest to make config a regular js file instead of json and return resolved paths instead of just package identifiers.

Workaround

Manually install jest-environment-puppeteer and expect-puppeteer and put following in config:

exports.globalSetup = require.resolve('jest-environment-puppeteer/setup');
exports.globalTeardown = require.resolve('jest-environment-puppeteer/teardown');
exports.testEnvironment= require.resolve('jest-environment-puppeteer');
module.exports.setupFilesAfterEnv = [require.resolve('expect-puppeteer')];
bug 🐛

All 6 comments

Another problem is that puppeteer is not listed as peerDependency and jest-environment-puppeteer is not listed at all. Although it is used: https://github.com/smooth-code/jest-puppeteer/blob/61d04c7e54908c2d662152cc0840b121742437fb/packages/jest-environment-puppeteer/src/PuppeteerEnvironment.js#L2

As a workaround in order to fix it add following to .yarnrc.yml:

packageExtensions:
  "jest-environment-puppeteer@*":
    peerDependencies:
      puppeteer: "*"
    dependencies:
      jest-environment-node: "*"

Any updates? More than a month has passed and no response.

Are you sure that it have been installed properly?
I found that it hadn't even been installed. Check your package.json in devDependencies
If it's missing maybe you made the same mistake as me.

To install properly use:

yarn add --dev puppeteer jest-puppeteer

Not

yarn add dev puppeteer jest-puppeteer

omg 😮

Reference:
https://github.com/electron-userland/electron-builder/issues/3180#issuecomment-408363783

I am 100% sure it's installed correctly. If you look at my comment you'll see that it's the problem of package, not installation. Package is using unlisted dependencies.

I recently switched from npm to pnpm and started receiving the error Test environment jest-environment-puppeteer cannot be found. Make sure the testEnvironment configuration option points to an existing node module.

From what I understand about pnpm, this means that jest-environment-puppeteer is being resolved through methods that aren't technically legal, and rely on a side-effect of how npm structures modules.

Error with Yarn 2

Error: jest-environment-puppeteer tried to access puppeteer, but it isn't declared in its dependencies; this makes the require call ambiguous and unsound.
Was this page helpful?
0 / 5 - 0 ratings