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

Stubbing a CJS module from an ESM #532

Open
3 of 7 tasks
benkiefer opened this issue Jun 26, 2024 · 0 comments
Open
3 of 7 tasks

Stubbing a CJS module from an ESM #532

benkiefer opened this issue Jun 26, 2024 · 0 comments

Comments

@benkiefer
Copy link

Description

In the testdouble repo, I created examples/node-esm/test/lib/numbers-only-test.mjs with the following code.

import assert from 'assert'
import mocha from 'mocha'
import * as td from 'testdouble'

mocha.describe('numbers-only', function () {
  mocha.it.only('goes boom', async function () {
    const isNumber = td.replace('is-number');
    const numbersOnly = await import('../../lib/numbers-only.mjs')
    td.when(isNumber('a string')).thenReturn(true) // tee-hee, this is silly

    const result = numbersOnly.default('a string')

    assert.equal(result, true)
  });
});

Based on what I read from the docs, I would expect this code to pass, but whenever ../../lib/numbers-only.mjs is loaded, the real is-number is resolved instead of the faked module.

Issue

the real module is being loaded instead of the fake.

Environment

  • node -v 20.1.0 or 22.3.0:
  • npm -v (or yarn --version) output: 10.5.2 or 10.8.1
  • npm ls testdouble (or yarn list testdouble) version: from the example

Failing Test

See above

Example Repo

See above

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

Code-fenced Examples

var td = require('testdouble')

// Your steps here.
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

1 participant