YuriGor / deepdash

eachDeep, filterDeep, findDeep, someDeep, omitDeep, pickDeep, keysDeep etc.. Tree traversal library written in Underscore/Lodash fashion
https://deepdash.io/
MIT License
272 stars 12 forks source link

deepdash-es not working with jest-resolve #133

Open jonnytest1 opened 2 years ago

jonnytest1 commented 2 years ago

i did some debugging and the jest-resolve => resolve.exports package expects each export to have a 3rd field either "default" "require" or "browser"

Julian-B90 commented 1 year ago

I have the same error Cannot find module 'deepdash-es/omitDeep' from

rommni commented 1 year ago

Same error here, someone has an idea how to fix this ?

jonnytest1 commented 1 year ago

something along the lines of this in the jest-setup.js should work

const packageJsonPath = join(__dirname, 'node_modules/deepdash-es/package.json');
const packageJson = JSON.parse(readFileSync(packageJsonPath, { encoding: 'utf8' }));

// fix package.json

writeFileSync(packageJsonPath, JSON.stringify(packageJson, undefined, '  '));
Dinika commented 10 months ago

@jonnytest1 The above does not work in non-nodejs environments, right? At least I get the following errors:

ReferenceError: join is not defined                                // I fixed this by using Array.prototype.join
ReferenceError: readFileSync is not defined

I also don't completely understand the solution. It seems to me we are trying to change the contents of package.json file for deepdash-es module, which looks a bit hacky. Is there a better, more official way of handling this?

Dinika commented 10 months ago

My work around for the issue was the following:

// jest.config.js

  moduleNameMapper: {
    ... other mapping,
    'deepdash-es': '<rootDir>/global.mock.js', 
  },

// global.mock.js

// Mock implementations for deepdash-es functions being used by the component under test 
export const findDeep = (flatTree, findFn) => flatTree.find(findFn);
export const reduceDeep = (arr) => arr;

Obviously, the work-around is not great because it requires overriding functionality provided by this library that the component under test relies on, but at least jest is able to run the tests where I can test overall functionality of my react component.

jonnytest1 commented 10 months ago

yeah the "fix" is definitely hacky , but i dont think thers a good way to do it - cleanest would probably be to fork the project and deploy it with the fix

jonnytest1 commented 10 months ago
import { readFileSync, writeFileSync } from 'fs';
import { join } from 'path';

to get the methods - and well the fix implementation would still need some testing to add the property in exports

Dinika commented 10 months ago

@jonnytest1 Thanks for the comment. I did try the suggested solution but I still get the error Cannot find module 'deepdash-es/standalone' from path/to/component/that/uses/deepdash-es;

Here's my jest config:

// File - jest.config.js
const customJestConfig = {
    ... // Other config
      setupFiles: ['./global.mock.js'], // The file inside which I've added the suggested lines
      moduleNameMapper: {
            '^@/(.*)$': '<rootDir>/src/$1',
      },
}
module.exports = createJestConfig(customJestConfig);

And here's the setup file called global.mock.js

import { readFileSync, writeFileSync } from 'fs';
import { join } from 'path';

const packageJsonPath = join(__dirname, 'node_modules/deepdash-es/package.json');
const packageJson = JSON.parse(readFileSync(packageJsonPath, { encoding: 'utf8' }));

// fix package.json
writeFileSync(packageJsonPath, JSON.stringify(packageJson, undefined, '  '));
jonnytest1 commented 10 months ago

the comment where it says "fix package.json" still needs an implementation i havent looked at it in details but i think it should be soemthing like

for(let export in packageJson.exports){
   if(!packageJson.exports[export]{
      packageJson.exports[export].require=export+".js
   }
}

or something similar , but the current package.json implementation over at https://github.com/YuriGor/deepdash/blob/master/package.json already seems to be correct , so maybe try to update to the latest version first maybe thers already a release for it