Skip to content

td.replaceEsm does not work when specifier is resolved via loader #528

@JakobJingleheimer

Description

@JakobJingleheimer

Description

When the specifier provided to replaceEsm() needs to be resolved by a loader, the resolution is done correctly, but testdouble is not using the resolved value, which causes the replacement to actually fail:

node --loader=testdouble --loader=alias-resolver ./main.mjs
// main.mjs

await ts.replaceEsm('…/foo.mjs', { isFake: true });
console.log('foo:', await import('…/foo.mjs')) // the original foo.mjs

Where alias-resolver replaces …/ and provides the true url.

Environment

  • node -v output: 21.7.0 (and also 20.11.1)
  • npm -v (or yarn --version) output: 10.5.0
  • npm ls testdouble (or yarn list testdouble) version: 3.20.1

Failing Test

  • Fork the repo
  • Add a failing test (probably to the `/regression/src' directory)
  • Submit a pull request for the failing test or link to your branch here

Example Repo

Reproduction: JakobJingleheimer/td-global-mock@0008fb5
"Fixed": JakobJingleheimer/td-global-mock@e5775aa

This is truly mind-bending because all the urls testdouble output are correct (ex from td.listReplacedModules()), so everything appears to be working—except that it clearly isn't.

Runkit Notebook

  • Create a Runkit notebook
  • Invoke var td = require('testdouble') at the top
  • Verify the behavior your issue is concerned with by clicking "Run"
  • Link to the Runkit here

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