Run code in a sandbox, with modules in the same context

I am running the ECMA-402 test package against Intlpolyfill, which I wrote, and I hit some problems. Tests are currently being run with a fully integrated version of the library, which means having to recompile each time changes are made before tests can be run. I am trying to improve it by breaking the code into separate modules and using the tests required to run.

The main problem arises in focus when I try to run tests using a module vm. If I add polyfill to the test sandbox, some tests fail when testing my own behavior - polyfill objects are not inherited from the test context Object.prototype, for example. Passing requirefor tests will not work because the modules are still compiled and executed in the parent context.

The easiest solution in my head is to create a new node process and write the code to the process stdin, but the generated nodeprocess does not execute the code written on it and just waits indefinitely. This is the code I tried:

function runTest(testPath, cb) {
    var test,
        err = '',
        content = 'var IntlPolyfill = require("' + LIB_PATH + '");\n';

    content += LIBS.fs.readFileSync(LIBS.path.resolve(TEST_DIR, testPath)).toString();
    content += 'runner();';

    test = LIBS.spawn(process.execPath, process.execArgv);
    test.stdin.write(content, 'utf8');

    // cb runs the next test
    test.on('exit', cb);
}

- , Node.js , stdin, , , ?

+4
2

-e, node. :

function runTest(testPath, cb) {
    var test,
        err = '',
        content = 'var IntlPolyfill = require("' + LIB_PATH + '");\n';

    content += LIBS.fs.readFileSync(LIBS.path.resolve(TEST_DIR, testPath)).toString();
    content += 'runner();';

    test = LIBS.spawn(process.execPath, process.execArgv.concat('-e', content));

    // cb runs the next test
    test.on('exit', cb);
}
+1

stdin . , .

test.stdin.end();
+2

Source: https://habr.com/ru/post/1542472/


All Articles