jimp-dev / jimp

An image processing library written entirely in JavaScript for Node, with zero external or native dependencies.
http://jimp-dev.github.io/jimp/
MIT License
13.95k stars 762 forks source link

Cannot find module `@jimp/plugin-print/load-font`, when used in Jest test #1325

Closed domdomegg closed 1 month ago

domdomegg commented 1 month ago

Expected Behavior

Can run a basic test with Create React App using Jimp 1.2.0

Current Behavior

 FAIL  src/App.test.tsx
  ● Test suite failed to run

    Cannot find module '@jimp/plugin-print/load-font' from 'node_modules/jimp/dist/commonjs/index.js'

    Require stack:
      node_modules/jimp/dist/commonjs/index.js
      src/lib/2-png-to-jpg.ts
      src/lib/index.ts
      src/App.tsx
      src/App.test.tsx

      at Resolver.resolveModule (node_modules/jest-resolve/build/resolver.js:324:11)
      at Object.<anonymous> (node_modules/jimp/src/index.ts:171:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.355 s, estimated 1 s
Ran all test suites related to changed files.

Failure Information (for bugs)

The rest of Jimp seems to load perfectly fine in the environment. I don't think I use this plugin, and commenting out line 166 of jimp/dist/commonjs/index.js (i.e. var load_font_1 = require("@jimp/plugin-print/load-font");) makes the tests pass.

I think this is a Jimp issue rather than a CRA/Jest issue given that the rest of Jimp works fine in this environment, and it's just this one line it has issue with.

Steps to Reproduce

  1. Set up a create react app project
  2. Run a very simple test:
    import { expect, test } from '@jest/globals';
    test('Jimp can load', () => {
      // eslint-disable-next-line global-require
      expect(() => require('jimp')).not.toThrow();
    });

Context

domdomegg commented 1 month ago

Minimal repro here: https://github.com/domdomegg/jimp-1325-mre

hipstersmoothie commented 1 month ago

That's using react.scripts with is on an old version of jest (27)

Reading through this issue it seems like support for the feature came in jest >28

Could you try updating? If you can't you might be able to write your own custom resolver for jest

domdomegg commented 1 month ago

Installing Jest 28 (or 29) doesn't seem to change the error, unfortunately.

hipstersmoothie commented 1 month ago

I got the tests to pass there by using jest-node-exports-resolver so it indeed does seem to be an issue in jest resolving package exports

CleanShot 2024-09-03 at 10 43 22

This does seem to be fixed in newer versions of jest. I just checked here

https://github.com/hipstersmoothie/jimp-jest