microsoft / vscode-test-cli

Command-line runner for VS Code tests
MIT License
19 stars 7 forks source link

Feature Request: Support Requires-injection Transpilers (ts-node, swc-node) #9

Closed JustinGrote closed 4 months ago

JustinGrote commented 7 months ago

There should be no reason I need to compile my tests to an output before running a test, I should be able to point the runner directly to my .ts test files.

Normally with Mocha this is easily done by just adding require: ['@swc-node/register'] and specifying ts files for the search and everything works transparently. With VSCode E2E a little more work has to be done with a bootstrap as the entrypoint but also still totally doable: https://github.com/pester/vscode-adapter/blob/707d6a0b9aa9b42bf5454daf7bcc92e1e580ef1b/test/runTestsInnerLoader.cjs#L5-L12

vscode-cli should be able to support this natively by allowing requires to be specified and have it be able to honor the related search paths.

JustinGrote commented 7 months ago

I think this is doable via the preload option, as long as the preload lines are moved above the creation of the Mocha runner https://github.com/microsoft/vscode-test-cli/blob/ceb33d29116d67a5ae4a825d6edc4c72bf31297b/src/runner.cts#L27C17-L29

JustinGrote commented 7 months ago

When trying to specify a preload get an error that require is not defined. I am assuming because bin.mjs is an ES6 module and require.resolve is not present in this scope without transpilation.

EDIT: Confirmed, if I take out the map of require.resolve then the module import works fine.

// .vscode-test.js
const { defineConfig } = require('@vscode/test-cli')

module.exports = defineConfig({
    files: 'src/log.vscode.test.ts',
    version: 'insiders',

    mocha: {
        ui: 'bdd',
        preload: '@swc-node/register'
    }
})
ReferenceError: require is not defined
    at file:///D:/vscode-pester/node_modules/.pnpm/@vscode+test-cli@0.0.4/node_modules/@vscode/test-cli/out/bin.mjs:307:77
    at Array.map (<anonymous>)
    at file:///D:/vscode-pester/node_modules/.pnpm/@vscode+test-cli@0.0.4/node_modules/@vscode/test-cli/out/bin.mjs:307:58
    at async Promise.all (index 0)
    at async runConfigs (file:///D:/vscode-pester/node_modules/.pnpm/@vscode+test-cli@0.0.4/node_modules/@vscode/test-cli/out/bin.mjs:291:29)
    at async main (file:///D:/vscode-pester/node_modules/.pnpm/@vscode+test-cli@0.0.4/node_modules/@vscode/test-cli/out/bin.mjs:223:20)
JustinGrote commented 7 months ago

I got this working if #10 is merged, and preload does not need to be moved.

// .vscode-test.js
const { defineConfig } = require('@vscode/test-cli')

module.exports = defineConfig({
    files: 'test/test.ts',
    version: 'insiders',
    mocha: {
        ui: 'bdd',
        preload: '@swc-node/register'
    }
})
// test.ts

import assert from 'node:assert'
interface Test {
    readonly a: number
    readonly b: string
}
describe('test', () => {
    it('first test', async () => {
        const x: Test = { a: 1, b: '2' }
        assert(x, 'x is not defined')
    })
})
❯ pnpm vscode-test
Found existing install in C:\Users\JGrote\Projects\vscode-pester\.vscode-test\vscode-win32-x64-archive-insiders. Skipping download

[52896:0103/182646.350:ERROR:node_bindings.cc(302)] Most NODE_OPTIONs are not supported in packaged apps. See documentation for more details.
[52896:0103/182646.350:ERROR:node_bindings.cc(302)] Most NODE_OPTIONs are not supported in packaged apps. See documentation for more details.
[main 2024-01-04T02:26:46.899Z] update#setState disabled
[main 2024-01-04T02:26:46.900Z] update#ctor - updates are disabled by the environment     
Via 'product.json#extensionEnabledApiProposals' extension 'ms-python.python' wants API proposal 'registerIssueDataProvider' but that proposal DOES NOT EXIST. Likely, the proposal has been finalized (check 'vscode.d.ts') or was abandoned.
Started local extension host with pid 34084.
Loading development extension at c:\Users\JGrote\Projects\vscode-pester

  test
    ✔ first test
  1 passing (274ms)
[main 2024-01-04T02:26:49.694Z] Extension host with pid 34084 exited with code: 0, signal: unknown.
Exit code:   0
Done

Works with the Explorer UI too: image

connor4312 commented 4 months ago

10 is now merged and we also support the mocha.require which should get you where you need to go. Let me know if not!