Skip to content
This repository was archived by the owner on Jan 6, 2024. It is now read-only.

Conversation

@JiangWeixian
Copy link

it's looks like failed on pnpm

@piranna
Copy link
Collaborator

piranna commented Feb 8, 2022

Never used pnpm before, and only tested it with npm. Can you explain your changes?

@JiangWeixian
Copy link
Author

JiangWeixian commented Feb 8, 2022

Never used pnpm before, and only tested it with npm. Can you explain your changes?

in my project, dep-a looks like

{
  "exports": {
    "./package.json": "./package.json",
    "./module": {
        "import": "./es/module.js",
        "require": "./cjs/module.js"
      }
  },
  "dependencies": {
	  "dep-b": "version"
   }
}

dep-a depends on dep-b, dep-b is also contian conditions-exports, it looks like

{
  "exports": {
    "./package.json": "./package.json",
    "./moduleb": "./cjs/module.js",
  },
}

in file ./cjs/module.js under dep-a

const foo = require('dep-b/moduleb')

the key is pnpm has special node_module structure

node_modules
  dep_a
    node_modules
      dep-b

require(dep-b/package.json) will throw error. So I try to find dep-b from basedir

But, I have not test on npm project and non-monorepo project yet... 😢

@piranna
Copy link
Collaborator

piranna commented Feb 8, 2022

the key is pnpm has special node_module structure

node_modules
  dep_a
    node_modules
      dep-b

This is standard Node.js structure, just only not doing flatting dependencies. That scheme is fully supported by Node.js, in fact it's how originally worked by default until about npm v5.

require(dep-b/package.json) will throw error. So I try to find dep-b from basedir

Your exports doesn't have defined require entries, only import ones. Are you sure this is not the source issue?

@JiangWeixian
Copy link
Author

JiangWeixian commented Feb 8, 2022

Your exports doesn't have defined require entries, only import ones. Are you sure this is not the source issue?

I update dep-a & dep-b package exports, here is my test case and jest.config.js

// file.spect.ts
import bar from 'dep-a/module'

it('bar', () => {
  // some test code
})
/**
 * @type {import('@jest/types').Config.InitialOptions}
 */

module.exports = {
  roots: ['<rootDir>/src'],
  transform: {
    '^.+\\.tsx?$': 'ts-jest',
  },
  resolver: '<rootDir>/resolver.js',
  testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
  moduleNameMapper: {
    // If you defined paths in tsconfig.json
    // you have to define them again here
    // for jest doesn't respect tsconfig.json's paths
    '@lib/(.*)': '<rootDir>/src/$1'
  },
  testEnvironment: 'jsdom',
  globals: {
    'ts-jest': {
      tsconfig: '<rootDir>/tsconfig.test.json',
    },
  }
};

'<rootDir>/resolver.js' is fork version of this repo, it works fine for me

Are you sure this is not the source issue?

maybe is monorepo issue, i will test it later.

@piranna
Copy link
Collaborator

piranna commented Feb 8, 2022

resolver: '<rootDir>/resolver.js',

Why do you have a custom resolver instead of using this module? What's its content?

@JiangWeixian
Copy link
Author

JiangWeixian commented Feb 8, 2022

<rootDir>/resolver.js is fork version of this repo, same as this pr

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants