Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

issue with unused imports (and libraries that monkey w/ require caches?) #798

@boneskull

Description

@boneskull

I've adapted the "proxyquire" scenario from the tests to show an issue. Below, I've added a second assertion, and a third file, c.js. The assertions made below, using CJS, pass (to prove it works as intended):

// index.js
const assert = require("assert")
const proxyquire = require("proxyquire")

// `path` and `b.js` are stubbed ONLY in a.js
const a = proxyquire.load("./a.js", {
    path: {
      extname: () => "c"
    },
    "./b.js": "b"
  })

assert.strictEqual(a(), "abc")

// `path` and `b.js` are now stubbed when loaded by ANY module
const b = new String("b") // this is hideous, but what can you do?
b["@global"] = true
const c = proxyquire.load("./c.js", {
  path: {
    extname: () => "c",
    "@global": true
  },
  "./b.js": b
})

assert.strictEqual(c(), "abc")
// a.js
const b = require("./b.js")
const {extname} = require("path")

module.exports = () => "a" + b + extname("")
// b.js
// Empty module.
// c.js
const a = require("./a.js")
const {extname} = require("path") // UNUSED

module.exports = () => a()

Now, if we write these using ES modules via node -r esm:

// index.js
import assert from "assert"
import proxyquire from "proxyquire"

const a = proxyquire
  .load("./a.js", {
    path: {
      extname: () => "c"
    },
    "./b.js": "b"
  })

assert.strictEqual(a.default(), "abc")

const b = new String("b")
b["@global"] = true
const c = proxyquire
  .load("./c.js", {
    path: {
      extname: () => "c",
      "@global": true
    },
    "./b.js": b
  })

assert.strictEqual(c.default(), "abc")
// a.js
import b from "./b.js"
import { extname } from "path"

export default () => "a" + b + extname("")
// b.js
// Empty module.
// c.js
import a from "./a.js"
import { extname } from "path" // UNUSED

export default () => a()

The result:

AssertionError [ERR_ASSERTION]: Input A expected to strictly equal input B:
+ expected - actual

- 'ab'
+ 'abc'
    at Object.<anonymous> (/Users/boneskull/projects/standard-things/esm/test/fixture/scenario/proxyquire/index.js:25:8)
    at Generator.next (<anonymous>)

If we remove that unused import in c.js:

// c.js
import a from "./a.js"

export default () => a()

the assertion passes.

I don't know what the problem is, but that's why I'm making this issue.

I can replicate this behavior in both rewiremock and proxyquire. I'll probably send a PR to add some rewiremock tests.

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