vscode-mocha-test-adapter icon indicating copy to clipboard operation
vscode-mocha-test-adapter copied to clipboard

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension “.ts”

Open Qlub53 opened this issue 3 years ago • 6 comments
trafficstars

Mocha Test Explorer only seems to work with commonjs.

Attempts to make it work with ES Module always ended up with

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension “.ts”

Feel free to try it out using this demo project.


My configurations:

node v16.8.0 VSCode v1.66.0 Mocha Test Explorer v2.13.3

package.json

{
	"type": "module"
}

tsconfig.json

{
    "compilerOptions": {
        "module": "esnext",
        "target": "esnext",
        "esModuleInterop": true,
    }
}

settings.json of VSCode

{
    "mochaExplorer.require": [
        "ts-node/register",
        "esm"
    ]
}

Qlub53 avatar Apr 14 '22 17:04 Qlub53

I have also been encountering this while evaluating @web/web-test-runner from here and here.

I have a .mocharc.yaml like so:

extension: [ 'ts' ]
package: './package.json'
reporter: 'spec'
require: 'ts-node/register'
spec: 
  - 'tests/**/*.spec.ts'
timeout: 10000

A sample test

import { expect } from "@esm-bundle/chai";

describe("index test suite", () => {

  it("registers tests", () => {
    expect(true).to.be.true;
  });

});

This is using @tsconfig/node16 as it's tsconfig.json (node 16.14.0 as of this comment :smile: )

vscode 1.66.2 Mocha Test Explorer 2.13.5

jpmckown avatar May 04 '22 20:05 jpmckown

I have the same problem. .mocharc.json (or even .vscode/settings.json/mochaExplorer.nodeArgv) should be read before running mocha but that does not appear to be the case.

Adding this to .vscode/settings.json (even though those values are available in my .mocharc.json) enabled test adapter to continue and read my .mocharc.json and load correctly.

  "mochaExplorer.nodeArgv": [
    "--loader=ts-node/esm",
    "--no-warnings=ExperimentalWarning",
    "--experimental-specifier-resolution=node"
  ]

chriskuech avatar Jun 24 '22 01:06 chriskuech

@chriskuech Thank you! Your suggestion fixed the problem!

ddnexus avatar Jul 19 '22 06:07 ddnexus

Also had this issue, should be documented or fixed. thank you

h-unterp avatar Jul 31 '22 10:07 h-unterp

I have the same problem. .mocharc.json (or even .vscode/settings.json/mochaExplorer.nodeArgv) should be read before running mocha but that does not appear to be the case.

Adding this to .vscode/settings.json (even though those values are available in my .mocharc.json) enabled test adapter to continue and read my .mocharc.json and load correctly.

  "mochaExplorer.nodeArgv": [
    "--loader=ts-node/esm",
    "--no-warnings=ExperimentalWarning",
    "--experimental-specifier-resolution=node"
  ]

This fixed the issue for me too

danbetama avatar Apr 25 '23 15:04 danbetama

Same problem, adding "nodeArgv" was not enough in my case. It failed on loading my mocharc.cjs (that does not contain relevant config for Explorer anyway) and displayed same error as in https://github.com/hbenl/vscode-mocha-test-adapter/issues/92.

Finally i made it working with:

settings.json

    "mochaExplorer.envPath": "./mochaExplorer.env",
    "mochaExplorer.nodeArgv": [
        "--loader=ts-node/esm"
    ],
    "mochaExplorer.configFile": "",

mochaExplorer.env

TS_NODE_PROJECT = tsconfig.test.json
TS_NODE_FILES = true

tipy01 avatar Jul 06 '23 18:07 tipy01