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

[node:test] How to mocking function exported with named export #4326

Closed
Lycolia opened this issue Jan 13, 2024 · 4 comments
Closed

[node:test] How to mocking function exported with named export #4326

Lycolia opened this issue Jan 13, 2024 · 4 comments

Comments

@Lycolia
Copy link

Lycolia commented Jan 13, 2024

Details

I'm trying to mock a function that has been exported using named export, but I'm not able to do it successfully.
How can I mock it properly?

Any help would be greatly appreciated!

Expected Behavior

test passing

Actual Behavior

✖ failing tests:

✖ validateFoo (0.793581ms)
  TypeError: "The argument 'methodName' must be a method. Received undefined"
      at __node_internal_captureLargerStackTrace (node:internal/errors:490:5)
      at new NodeError (node:internal/errors:399:5)
      at MockTracker.method (node:internal/test_runner/mock:180:13)
      at TestContext.<anonymous> (/home/user/develop/path/to/src/hoge.spec.ts:8:12)
      at Test.runInAsyncScope (node:async_hooks:206:9)
      at Test.run (node:internal/test_runner/test:550:25)
      at Test.start (node:internal/test_runner/test:466:17)
      at node:internal/test_runner/test:788:71
      at node:internal/per_context/primordials:482:82
      at new Promise (<anonymous>)
      at new SafePromise (node:internal/per_context/primordials:450:29)
      at node:internal/per_context/primordials:482:9
      at Array.map (<anonymous>)
      at arrayToSafePromiseIterable (node:internal/per_context/primordials:479:5)
      at node:internal/per_context/primordials:496:21
      at new Promise (<anonymous>)
      at primordials.SafePromiseAll (node:internal/per_context/primordials:495:3)
      at Suite.run (node:internal/test_runner/test:788:23)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async startSubtest (node:internal/test_runner/harness:203:3)

✖ example (1.794746ms)
  [Error: 1 subtest failed
  ]

Node.js version

v20.0.0

Example code

Implementation Code

export const validateFoo = () => {
  console.log('foo');
};

export const hoge = () => {
  validateFoo();
};

Test Code

import { describe, it } from 'node:test';
import assert from 'node:assert';
import * as HogeObj from './hoge';
import { hoge } from './hoge';

describe('example', () => {
  it('validateFoo', (t) => {
    t.mock.method(HogeObj, 'validateFoo', () => {
      throw new Error('foo');
    });

    assert.throws(() => {
      hoge();
    }, Error);
  });
});

Operating system

Ubuntu 22.04.3 LTS

Scope

test

Module and version

  • node:test
  • node:assert
@Lycolia Lycolia changed the title Native Test Runner: How to mocking function exported with named export Test Runner: How to mocking function exported with named export Jan 13, 2024
@Lycolia Lycolia changed the title Test Runner: How to mocking function exported with named export [node:test] How to mocking function exported with named export Jan 13, 2024
@Molezinif
Copy link

Molezinif commented Mar 1, 2024

I have the same problem, I looked at this issue and apparently the native node test does not have support for mocking modules #4298

@mehulkar
Copy link

Odd note, when I do:

import fs from 'node:fs'
import * as blah from './blah';

test('my test', (t) => {
  // this works
  t.mock.method(fs, 'writeFileSync', () => /* */);

  // this doesn't work
  t.mock.method(blah, 'someExport', () => /* */);
});

I'm guessing this is either because I'm using a loader (node --loader tsx mytest.test.ts), or the node:fs module is actually exporting an object, whereas blah.ts just has export function thing() {}.

In any case, I would also like to be able to mock exports from local files

Copy link

It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment.
If you need further assistance or have questions, you can also search for similar issues on Stack Overflow.
Make sure to look at the README file for the most updated links.

@github-actions github-actions bot added the stale label Oct 10, 2024
Copy link

It seems there has been no activity on this issue for a while, and it is being closed. If you believe this issue should remain open, please leave a comment.
If you need further assistance or have questions, you can also search for similar issues on Stack Overflow.
Make sure to look at the README file for the most updated links.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants