2

I am trying to write end-to-end tests for a VS Code extension. I would like to use JavaScript, not TypeScript. So, I took the example from here: https://github.com/microsoft/vscode-extension-samples/tree/main/helloworld-test-sample and converted the code to JavaScript and CommonJS. When I do, I get this error:

Error: Path file:///Users/kurmasz/Documents/LocalResearch/QLC/gvQLC/test/suite/index does not point to a valid extension test runner.

Here is my index.js:

const path = require('path')
const Mocha = require('mocha')
const glob = require('glob')

console.log('********************* Here!')

module.exports = function run() {
    // Create the mocha test
    const mocha = new Mocha({
        ui: 'tdd'
    });

    const testsRoot = path.resolve(__dirname, '..');
    console.log(`********************** Test Root: ${testsRoot}`)

    return new Promise((c, e) => {
        glob('**/**.test.js', { cwd: testsRoot }, (err, files) => {
            if (err) {
                return e(err);
            }

            // Add files to the test suite
            files.forEach(f => mocha.addFile(path.resolve(testsRoot, f)));

            try {
                // Run the mocha test
                mocha.run(failures => {
                    if (failures > 0) {
                        e(new Error(`${failures} tests failed.`));
                    } else {
                        c();
                    }
                });
            } catch (err) {
                console.error(err);
                e(err);
            }
        });
    });
}
console.log('********************* Done!')

Both console statements print, so the code is running and not throwing an error.

I am using VS Code 1.101.0 @vscode/test-electron 2.5.2 mocha 11.6.0

I also see this when I run the test:

✔ Validated version: 1.101.1
✔ Found existing install in /Users/.../.vscode-test/vscode-darwin-1.101.1

Can anybody tell what I'm doing wrong?


Update: I cloned helloworld-test-sample, then looked at the JavaScript generated by compiling the TypeScript. This is the beginning of index.js:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.run = run;
const path = require("path");
const Mocha = require("mocha");
const glob = require("glob");
function run() {

When I use this structure (with __esModule), then the "does not point to a valid extension test runner" error goes away. Does this mean that my JavaScript version of index.js is wrong? Or do I just need to use TypeScript for my tests?

1 Answer 1

2

When translating the typescript to javascript you've accidentally changed the semantics of the module export.

The original code has a named export run, but your code has an anonymous default export.

The fix would be to change your code to use a named export as well:

module.exports.run = function run() {};
//            ^^^^ this is the important bit

You can see the difference when we compare the commonjs and esmodule equivalents of your code and the original:

your code

// your commonjs
module.exports = function run() {};
//    ^^^^^^^^ anonymous export

// equivalent esmodule code
export default function run(): Promise<void> {}

original code

// commonjs (generated from typescript)
exports.run = run;
//     ^^^^ named export
function run() {}

// esmodule (typescript)
export function run(): Promise<void> {}

(exports.run is just a short-hand alias for module.exports.run)


Note that the internal name of the function is independent of the exported name. In this case, it's the name of the export (module.exports.run) that is important, not the internal function run() name.

You could declare it this way and it would still work

module.exports.run = function myAmazingFunction() {};
//            ^^^^ this is the external name
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.