Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jest not happy with 3.0.1 #209

Closed
jp928 opened this issue Mar 30, 2021 · 11 comments
Closed

jest not happy with 3.0.1 #209

jp928 opened this issue Mar 30, 2021 · 11 comments

Comments

@jp928
Copy link

jp928 commented Mar 30, 2021

I got error

Cannot find module 'astronomia/julian' from '../node_modules/date-holidays-parser/lib/Julian.cjs'
@jp928 jp928 changed the title jest not happy with 3.0.0 jest not happy with 3.0.1 Mar 30, 2021
@commenthol
Copy link
Owner

Hi, I just made a cjs project with a small test.

Interestingly the test works without complaints in mocha.
jest on the other hand fails with the stated:

FAIL  test/index.test.js
 ● Test suite failed to run

   Cannot find module 'astronomia/julian' from 'node_modules/date-holidays-parser/lib/Julian.cjs'

   Require stack:
     node_modules/date-holidays-parser/lib/Julian.cjs
     ...
     node_modules/date-holidays/lib/index.cjs
     index.js
     test/index.test.js

     at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:306:11)
     at Object.<anonymous> (node_modules/date-holidays-parser/lib/Julian.cjs:3:14)

Due to the observed I would image that jest is not (yet) capable to resolve the package.json exports as used by astronomia.

Those work according to See https://nodejs.org/dist/latest-v14.x/docs/api/esm.html#esm_resolution_algorithm under PACKAGE_IMPORTS_EXPORTS_RESOLVE.

I think the related issue is jestjs/jest#11167

For the moment I would suggest to maintain v2 and v3 of this project.
The last changes on [email protected] for CA-QC will be cheery-picked on the v2 branch.

@hosuaby
Copy link

hosuaby commented Apr 28, 2021

I had a simular problem while impot date-holidays into Angular project:

import * as Holidays from 'date-holidays';
Error: ./node_modules/date-chinese/lib/Chinese.cjs
Module not found: Error: Can't resolve 'astronomia/base' in '/home/hosuaby/projects/my-timesheet/node_modules/date-chinese/lib'
resolve 'astronomia/base' in '/home/hosuaby/projects/my-timesheet/node_modules/date-chinese/lib'

There is way to completely exclude date-chinese or astronomia from bundle ?

@michaelhays
Copy link

I've run into the same issue as @hosuaby , when running Cypress integration tests on a Vite + Vue 3 project.

Here's the error message:

Webpack Compilation Error
./node_modules/date-chinese/lib/Chinese.cjs
Module not found: Error: Can't resolve 'astronomia/base' in '/path/to/project/node_modules/date-chinese/lib'
resolve 'astronomia/base' in '/path/to/project/node_modules/date-chinese/lib'
  Parsed request is a module
  using description file: /path/to/project/node_modules/date-chinese/package.json (relative path: ./lib)
    resolve as module
      looking for modules in /path/to/project/node_modules
        using description file: /path/to/project/package.json (relative path: ./node_modules)
          using description file: /path/to/project/node_modules/astronomia/package.json (relative path: ./base)
            no extension
              /path/to/project/node_modules/astronomia/base doesn't exist
            .js
              /path/to/project/node_modules/astronomia/base.js doesn't exist
            .json
              /path/to/project/node_modules/astronomia/base.json doesn't exist
            .jsx
              /path/to/project/node_modules/astronomia/base.jsx doesn't exist
            .mjs
              /path/to/project/node_modules/astronomia/base.mjs doesn't exist
            .coffee
              /path/to/project/node_modules/astronomia/base.coffee doesn't exist
            as directory
              /path/to/project/node_modules/astronomia/base doesn't exist

[/path/to/project/node_modules/astronomia/base]
[/path/to/project/node_modules/astronomia/base.js]
[/path/to/project/node_modules/astronomia/base.json]
[/path/to/project/node_modules/astronomia/base.jsx]
[/path/to/project/node_modules/astronomia/base.mjs]
[/path/to/project/node_modules/astronomia/base.coffee]
 @ ./node_modules/date-chinese/lib/Chinese.cjs 3:11-37
 @ ./node_modules/date-chinese/lib/index.cjs
 @ ./node_modules/date-holidays-parser/lib/Chinese.cjs
 @ ./node_modules/date-holidays-parser/lib/CalEventFactory.cjs
 @ ./node_modules/date-holidays-parser/lib/DateFn.cjs
 @ ./node_modules/date-holidays-parser/lib/Holidays.cjs
 @ ./node_modules/date-holidays-parser/lib/index.cjs
 @ ./node_modules/date-holidays/lib/Holidays.cjs
 @ ./node_modules/date-holidays/lib/index.cjs
 @ ./src/utils.js
 @ ./cypress/integration/test.spec.js

A breakdown of what's happening:

Starting from the bottom, a file in my project calls import Holidays from 'date-holidays', which imports date-holidays/lib/index.cjs (not sure why it's cjs, but it doesn't seem to matter). This follows a string of imports, until it gets to date-chinese/lib/Chinese.cjs, which attempts to import several items from astronomia.

import base from 'astronomia/base' is called first, which searches for the file (trying several different file extensions) in:

/path/to/project/node_modules/astronomia/base

However, it should be trying to find this file at either:

/path/to/project/node_modules/astronomia/src/base.js
/path/to/project/node_modules/astronomia/lib/base.cjs

The astronomia/package.json file specifies this translation in exports, but apparently it is not working.

I'm able to make things work by manually adding a /path/to/project/node_modules/astronomia/base.js file, with these contents:

import base from './src/base'
export default base

But obviously this isn't a long-term solution.

I'm guessing something is misconfigured somewhere, but I don't know nearly enough about ES Modules/CommonJS to go any further. Any thoughts, @commenthol?

@michaelhays
Copy link

Disregard my last comment! I upgraded date-holidays to 3.3.0 and everything is working :)

@jp928 jp928 closed this as completed May 12, 2021
@jp928
Copy link
Author

jp928 commented May 12, 2021

@michaelhays Closed.

@hosuaby
Copy link

hosuaby commented May 12, 2021

I confirm that 3.3.0 works. With one small problem. For some strange reason code fails in Karma tests with :

TypeError: date_holidays_1.default is not a constructor
    at new HolidaysService (http://localhost:9876/_karma_webpack_/webpack:/src/app/locals/services/holidays.service.ts:12:22)

But changing import by fixes the problem:

import Holidays from 'date-holidays/src';

Infortunatly, 3.3.0 still based on plain old CommonsJs modules, so no tree-shakable.

@commenthol
Copy link
Owner

Hi @hosuaby, may I ask why you think that v3.3.0 still relies on CommonJs?

@hosuaby
Copy link

hosuaby commented May 18, 2021

If I import it as following, the import is resolved to the file node_modules/date-holidays/lib/index.cjs, which is clearly a CommonJs module:

import Holidays from 'date-holidays';

If I change the import like that, the import will be resolved by source node_modules/date-holidays/src/index.js, which is ES6 module, but it depends on 'date-holidays-parser', which is a CommonJs module:

import Holidays from 'date-holidays/src';

@alexsegura
Copy link

On my side, I had the following error:

● Test suite failed to run

  Jest encountered an unexpected token

  This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

  By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

  Here's what you can do:
   • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
   • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
   • If you need a custom transformation specify a "transform" option in your config.
   • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

  You'll find more details and examples of these config options in the docs:
  https://jestjs.io/docs/en/configuration.html

  Details:

  /srv/coopcycle/node_modules/astronomia/data/vsop87Bearth.js:2611
  export default m;
  ^^^^^^

  SyntaxError: Unexpected token 'export'

    at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
    at Object.<anonymous> (node_modules/date-holidays-parser/lib/Equinox.cjs:4:17)

If fixed it by adding this to jest.config.js

{
  transformIgnorePatterns: [
    "node_modules/(?!(astronomia)/)"
  ]
}

@estyxx
Copy link

estyxx commented Jul 13, 2021

Hey I still have this problem, tried with the solution suggested above seems working:
I'm using the latest version "date-holidays": "^3.8.0",

    Jest encountered an unexpected token
.....
    Details:

    /Users/ester.beltrami/Made/mercury/node_modules/astronomia/data/vsop87Bearth.js:2611
    export default m;
    ^^^^^^

If I change to import import Holidays from 'date-holidays/src'; instead I got this:

    /Users/ester.beltrami/Made/mercury/node_modules/date-holidays/src/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { Holidays as default } from './Holidays.js'
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

    > 1 | import Holidays from 'date-holidays/src';
        | ^
      2 |
      3 | const hd = new Holidays("GB", "ENG");
      4 |

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1479:14)
      at Object.<anonymous> (src/lib/hooks/useNextBusinessDay/index.tsx:1:1)


@axe-me
Copy link

axe-me commented Jul 16, 2021

Add a module mapper into jest.config.js solved my problem:

moduleNameMapper: {
    "astronomia/data(.*)$": "<rootDir>/node_modules/astronomia/lib/data$1.cjs",
  },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants