hbenl / vscode-mocha-test-adapter

Mocha Test Adapter for the VS Code Test Explorer
MIT License
91 stars 31 forks source link

autoload not working with typescript #142

Open sean256 opened 3 years ago

sean256 commented 3 years ago

The tests are not auto(re)loaded on any file changes. Clicking run in the toolbar does work, so this seems to just be an issue with it watching for changes.

vscode settings.json

{
    "typescript.tsdk": "node_modules/typescript/lib",
    "mochaExplorer.files": "src/**/*.test.ts",
    "mochaExplorer.require": "ts-node/register",
    "mochaExplorer.autoload": true,
}

tsconfig.json

{
    "compilerOptions": {
        "baseUrl": "src",
        "module": "commonjs",
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "forceConsistentCasingInFileNames": true,
        "noFallthroughCasesInSwitch": true,
        "resolveJsonModule": true,
        "strictBindCallApply": true,
        "target": "es6",
        "noImplicitAny": true,
        "moduleResolution": "node",
        "sourceMap": false,
        "outDir": "release"
    }
}
hbenl commented 3 years ago

This works for me. Note that Mocha Test Explorer doesn't actually watch any files, it reacts to the vscode.workspace.onDidSaveTextDocument event, which means this will only work if you change a file in VS Code, but not if you change it in an external editor. If you're not using an external editor, you can add "mochaExplorer.logpanel": true to your settings and have a look at the "Mocha Explorer Log" output panel to see why it isn't working.

ralfhandl commented 3 years ago

I've got the same problem: saving the test.ts file in VS Code triggers test-execution but does not reload the test file, instead it runs with the last explicitly loaded test file contents.

Here's the log:

[2021-03-07 15:43:39.770] [INFO] c:\git\ts-mocha-c8\test\hello.test.ts was saved - checking if this affects c:\git\ts-mocha-c8
[2021-03-07 15:43:39.771] [INFO] Reloading because c:\git\ts-mocha-c8\test\hello.test.ts is a test file
[2021-03-07 15:43:39.771] [INFO] Loading test files of c:\git\ts-mocha-c8
[2021-03-07 15:43:39.771] [DEBUG] Using working directory: c:\git\ts-mocha-c8
[2021-03-07 15:43:40.043] [DEBUG] Using nodePath: C:\Program Files\nodejs\node.exe
[2021-03-07 15:43:40.348] [DEBUG] Using Mocha options: {"ui":"bdd","timeout":2000,"retries":0,"requires":["ts-node/register"],"delay":false,"fullTrace":false,"exit":false,"asyncOnly":false,"parallel":false}
[2021-03-07 15:43:40.348] [DEBUG] Looking for test files ["test/**/*.ts"] in c:\git\ts-mocha-c8
[2021-03-07 15:43:40.349] [DEBUG] Found test files ["c:/git/ts-mocha-c8/test/hello.test.ts"]
[2021-03-07 15:43:40.351] [DEBUG] Using environment variables from config: {}
[2021-03-07 15:43:40.351] [DEBUG] Spawning c:\Users\ralfh\.vscode\extensions\hbenl.vscode-mocha-test-adapter-2.10.2\out\worker\bundle.js with IPC options {}
[2021-03-07 15:43:40.442] [INFO] Worker: Using the mocha package at c:\git\ts-mocha-c8\node_modules\mocha
[2021-03-07 15:43:40.518] [INFO] Worker: Patching Mocha
[2021-03-07 15:43:40.518] [INFO] Worker: Trying requireOrImport('ts-node/register')
[2021-03-07 15:43:40.821] [INFO] Worker: Loading files
[2021-03-07 15:43:40.821] [INFO] Worker: Trying to use Mocha's experimental ESM module loader
[2021-03-07 15:43:40.823] [INFO] Worker: Patching describe
[2021-03-07 15:43:40.823] [INFO] Worker: Patching describe.skip
[2021-03-07 15:43:40.823] [INFO] Worker: Patching describe.only
[2021-03-07 15:43:40.823] [INFO] Worker: Patching it
[2021-03-07 15:43:40.823] [INFO] Worker: Patching it.only
[2021-03-07 15:43:40.823] [INFO] Worker: Patching it.skip
[2021-03-07 15:43:40.824] [INFO] Worker: Copying it.retries
[2021-03-07 15:43:40.824] [INFO] Worker: Patching context
[2021-03-07 15:43:40.824] [INFO] Worker: Patching context.skip
[2021-03-07 15:43:40.824] [INFO] Worker: Patching context.only
[2021-03-07 15:43:40.824] [INFO] Worker: Patching specify
[2021-03-07 15:43:40.824] [INFO] Worker: Patching specify.only
[2021-03-07 15:43:40.824] [INFO] Worker: Patching specify.skip
[2021-03-07 15:43:40.824] [INFO] Worker: Copying specify.retries
[2021-03-07 15:43:40.824] [INFO] Worker: Patching describe
[2021-03-07 15:43:40.824] [INFO] Worker: Patching describe.skip
[2021-03-07 15:43:40.824] [INFO] Worker: Patching describe.only
[2021-03-07 15:43:40.824] [INFO] Worker: Patching it
[2021-03-07 15:43:40.824] [INFO] Worker: Patching it.only
[2021-03-07 15:43:40.824] [INFO] Worker: Patching it.skip
[2021-03-07 15:43:40.825] [INFO] Worker: Copying it.retries
[2021-03-07 15:43:40.825] [INFO] Worker: Patching context
[2021-03-07 15:43:40.825] [INFO] Worker: Patching context.skip
[2021-03-07 15:43:40.825] [INFO] Worker: Patching context.only
[2021-03-07 15:43:40.825] [INFO] Worker: Patching specify
[2021-03-07 15:43:40.825] [INFO] Worker: Patching specify.only
[2021-03-07 15:43:40.825] [INFO] Worker: Patching specify.skip
[2021-03-07 15:43:40.825] [INFO] Worker: Copying specify.retries
[2021-03-07 15:43:42.002] [INFO] Worker: Looking for c:/git/ts-mocha-c8/test/hello.test.ts in Error: 
    at findCallLocation (c:\Users\ralfh\.vscode\extensions\hbenl.vscode-mocha-test-adapter-2.10.2\out\worker\bundle.js:836:17)
    at c:\Users\ralfh\.vscode\extensions\hbenl.vscode-mocha-test-adapter-2.10.2\out\worker\bundle.js:827:30
    at Suite.<anonymous> (c:\git\ts-mocha-c8\test\hello.test.ts:6:3)
    at Object.create (c:\git\ts-mocha-c8\node_modules\mocha\lib\interfaces\common.js:148:19)
    at context.describe.context.context (c:\git\ts-mocha-c8\node_modules\mocha\lib\interfaces\bdd.js:42:27)
    at c:\Users\ralfh\.vscode\extensions\hbenl.vscode-mocha-test-adapter-2.10.2\out\worker\bundle.js:825:37
    at Object.<anonymous> (c:\git\ts-mocha-c8\test\hello.test.ts:5:1)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Module.m._compile (c:\git\ts-mocha-c8\node_modules\ts-node\src\index.ts:1056:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
[2021-03-07 15:43:42.002] [INFO] Worker: Looking for c:/git/ts-mocha-c8/test/hello.test.ts in Error: 
    at findCallLocation (c:\Users\ralfh\.vscode\extensions\hbenl.vscode-mocha-test-adapter-2.10.2\out\worker\bundle.js:836:17)
    at c:\Users\ralfh\.vscode\extensions\hbenl.vscode-mocha-test-adapter-2.10.2\out\worker\bundle.js:827:30
    at Object.<anonymous> (c:\git\ts-mocha-c8\test\hello.test.ts:5:1)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Module.m._compile (c:\git\ts-mocha-c8\node_modules\ts-node\src\index.ts:1056:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Object.require.extensions.<computed> [as .ts] (c:\git\ts-mocha-c8\node_modules\ts-node\src\index.ts:1059:12)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
[2021-03-07 15:43:42.005] [INFO] Worker (stdout): 

[2021-03-07 15:43:42.005] [INFO] Worker (stdout): 

[2021-03-07 15:43:42.005] [INFO] Worker (stdout):   0 passing (1ms)

[2021-03-07 15:43:42.006] [INFO] Worker (stdout): 

[2021-03-07 15:43:42.006] [INFO] Worker: Converting tests and suites
[2021-03-07 15:43:42.007] [INFO] Received tests from worker
[2021-03-07 15:43:42.038] [INFO] Worker finished with code 0 and signal null
ralfhandl commented 3 years ago

The problem goes away if I set this in the workspace config

    "mochaExplorer.esmLoader": false

Apparently Mocha's experimental ESM module loader is too experimental on Windows 😄

ralfhandl commented 3 years ago

@hbenl I can reproduce this with a plain JavaScript project: tests are not reloaded with the mochaExplorer.esmLoader default setting, or when it is explicitly set to true.

Need to explicitly set it to false to correctly reload changed tests.