Skip to content

question: nodejs plugin and esm #80

@boneskull

Description

@boneskull

Hi,

I was having some trouble using the nodejs plugin and relative paths with esm and rewiremock.module(). I'm unable to provide a minimal case to reproduce the problem I'm having, but I'll try to explain:

  • lib/foo.js is a module
  • lib/baz.js is another module
  • lib/bar/quux.js is yet another module
  • lib/bar/quux.js depends on lib/foo.js
  • lib/foo.js depends on lib/baz.js
  • test/bar/quux.spec.js is a test for lib/bar/quux.js, which uses rewiremock to stub lib/foo.js
  • test/foo.spec.js is a test for lib/foo.js, which uses rewiremock to stub lib/baz.js

This is how I'm using rewiremock:

  • stubs uses module paths relative to the test files
  • I'm using overrideEntryPoint() in a separate test helper file to load rewiremock
  • I'm running my tests via mocha -r esm, and with the nodejs plugin enabled.
  • I'm using the rewiremock.module(() => import('/path/to/module') syntax.

If I run each of those two tests individually, they both pass. If I run them together, test/bar/quux.spec.js runs first, then test/foo.spec.js runs. The latter fails, as it can't find lib/baz.js, which is stubbed via the relative path ../lib/baz.

In the resulting stack trace, test/bar/quux.spec.js appears at the top. Running the debugger, when test/foo.spec.js is trying to stub ../lib/baz, the "parent" module is test/bar/quux.spec.js, which seems incorrect.

A couple things I've noticed:

  • If I disable the nodejs plugin and use paths relative to the files under test (e.g., lib/foo.js stubs ./baz instead of ../lib/baz), all tests pass together (in other words, I'm not blocking on a fix)
  • Using rewiremock.enable() paired with rewiremock.disable() seems to have no effect whatsoever

So my question is if I should be using the nodejs plugin, or a different plugin, and how can I specify relative-to-test-file module paths to be stubbed? Is there a bug here?

Thanks for your work on this module!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions