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's test runner not available as builtin #531

Open
DaniFrancisco opened this issue Jun 30, 2023 · 0 comments
Open

Node's test runner not available as builtin #531

DaniFrancisco opened this issue Jun 30, 2023 · 0 comments

Comments

@DaniFrancisco
Copy link

DaniFrancisco commented Jun 30, 2023

Hi there!

I'm trying to run some tests from within the VM. Thought I could use Node's Test Runner but ended up having VMError: Cannot find module 'node:test' error. I confirm I've tried to add the package as a builtin to the NodeVM configuration.

Ended up using the mock feature as alternative - seems to work fine - here's a working example:

import { NodeVM } from 'vm2';
import { mock } from 'node:test';

const vm = new NodeVM({
    require: {
        builtin: ['assert/strict'],
        mock: {
            test: { mock }
        }
    }
});

vm.run(`
    const { mock } = require('node:test');
    const assert = require('node:assert/strict');

    mock.method(console, 'log');

    console.log('Hello World');
   
   assert.equal(console.log.mock.calls[0].arguments[0], 'Hello World');
`);

I'm curious to know why isn't this package available as a builtin and whether you find this a good alternative.

Node version: v18.16.0

Thank you!

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