Skip to content

Add support for import alias #69

Description

@Poyoman39

Hi quibble team,

I've a feature request. On my project i'm using import alias defined in my package.json like that :

{
  // [ ... ]
  "type": "module",
  "imports": {
    "#src/*": "./*"
  },
  // [ ... ]
}

It looks like quibble loader fails at managing such import. In my .test.js file i tried to replace alias path with await td.replaceEsm('#src/dependencies'); this lead to the following error :

TypeError [ERR_PACKAGE_IMPORT_NOT_DEFINED]: Package import specifier "#src/dependencies" is not defined in package /.../node_modules/quibble/package.json imported from /.../node_modules/quibble/lib/esm-import-functions.js

Full call stack :

      at new NodeError (node:internal/errors:371:5)
      at throwImportNotDefined (node:internal/modules/esm/resolve:442:9)
      at packageImportsResolve (node:internal/modules/esm/resolve:819:3)
      at moduleResolve (node:internal/modules/esm/resolve:973:21)
      at defaultResolve (node:internal/modules/esm/resolve:1080:11)
      at resolve (file:///.../node_modules/quibble/lib/quibble.mjs:12:25)
      at resolve (file:///.../node_modules/quibble/lib/quibble.mjs:20:41)
      at ESMLoader.resolve (node:internal/modules/esm/loader:530:30)
      at ESMLoader.getModuleJob (node:internal/modules/esm/loader:251:18)
      at ESMLoader.import (node:internal/modules/esm/loader:332:22)
      at importModuleDynamically (node:internal/modules/cjs/loader:1042:29)
      at importModuleDynamicallyWrapper (node:internal/vm/module:437:21)
      at importModuleDynamically (node:vm:381:46)
      at importModuleDynamicallyCallback (node:internal/process/esm_loader:35:14)
      at Object.dummyImportModuleToGetAtPath (node_modules/quibble/lib/esm-import-functions.js:8:70)
      at Function.esmImportWithPath (node_modules/quibble/lib/quibble.js:117:35)
      at Object.replaceEsModule (node_modules/testdouble/lib/replace/module/index.js:39:50)
      at Module.replaceEsm (node_modules/testdouble/lib/replace/index.js:23:21)
      at Context.<anonymous> (file:///.../lib/cron/crons/common/utils/parseSalary.test.js:18:14)
      at processImmediate (node:internal/timers:466:21)

I remain available if you need more information

Activity

  1. Poyoman39 commented on May 12, 2022

    @Poyoman39
    Author

    I don't know the exact way quibble is working internally. But it looks like the problem come from "dummyImportModuleToGetAtPath" function. When it try to "import", the original parentUrl context is lost, then package.json alias are lost too.

  2. searls commented on May 12, 2022

    @searls
    Member

    Paging @giltayar for help! 🙏

  3. giltayar commented on May 21, 2022

    @giltayar
    Contributor

    @Poyoman39 I'll look into it.

  4. rosston commented on Oct 10, 2026

    @rosston
    Member

    This is fixed in quibble 0.12.0 (via #123), and testdouble.js 3.23.0 picks it up.

    Since 0.12.0, quibble resolves non-relative specifiers (package names and package.json imports like #src/*) from the file that called quibble.esm() / esmImportWithPath(). It skips files passed to ignoreCallsFromThisFile(), so td.replaceEsm('#src/dependencies') resolves from your test file. No --experimental-import-meta-resolve flag is needed.

    I've added regression tests in #127 that cover quibble.esm, a self-ignoring wrapper (like td.replaceEsm) and esmImportWithPath with a #src/* mapping. To get the fix, upgrade to testdouble.js 3.23.0 or later (or quibble 0.12.0 or later if you use it directly). Closing, but please reopen if you still hit ERR_PACKAGE_IMPORT_NOT_DEFINED after upgrading.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions