Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Unknown file extension ".ts" | #71

Open
Jason-Terry opened this issue Jan 4, 2024 · 6 comments
Open

TypeError: Unknown file extension ".ts" | #71

Jason-Terry opened this issue Jan 4, 2024 · 6 comments

Comments

@Jason-Terry
Copy link

Jason-Terry commented Jan 4, 2024

package.json

  "name": "sd-tracker-api",
  "version": "1.0.0",
  "description": "",
  "main": "main.ts",
  "scripts": {
    "build": "tsc -p .",
    "dev": "SET NODE_ENV=dev & ts-node-dev --respawn --transpile-only -r tsconfig-paths/register src/main.ts",
    "test": "SET NODE_ENV=test & mocha --check-leaks -r ts-node/register -r tsconfig-paths/register tests/**.spec.ts"
  },
  "author": "Jason Terry",
  "license": "ISC",
  "devDependencies": {
    "@types/chai": "^4.3.11",
    "@types/express": "^4.17.21",
    "@types/mocha": "^10.0.6",
    "@types/node": "^20.10.6",
    "@types/supertest": "^6.0.2",
    "chai": "^5.0.0",
    "mocha": "^10.2.0",
    "supertest": "^6.3.3",
    "ts-node": "^10.9.2",
    "ts-node-dev": "^2.0.0",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.3.3"
  },
  "dependencies": {
    "dotenv": "^16.3.1",
    "express": "^4.18.2"
  }
}

tsconfig.json

{
  "exclude": ["./coverage", "./dist", "./node_modules"],
  "ts-node": {
    "transpileOnly": true,
    "files": true
  },
  "compilerOptions": {
    "target": "es2016", 
    "module": "Node16",  
    "rootDir": "./src",    
    "moduleResolution": "Node16",
    "baseUrl": "./src",
    "outDir": "./dist",
    "esModuleInterop": true,                             
    "forceConsistentCasingInFileNames": true,            
    "strict": true,                                      
    "skipLibCheck": true                                 
  }
}

.mocharc.json

{
  "extension": ["ts"],
  "spec": "tests/**/*.spec.ts",
  "require": "ts-node/register"
}

Greetings, I am unable to run mocha when including import { expect } from 'chai'; into my project.

It causes the following to occour.

npm run test

> sd-tracker-api@1.0.0 test
> SET NODE_ENV=test & mocha --check-leaks -r ts-node/register -r tsconfig-paths/register tests/**.spec.ts


TypeError: Unknown file extension ".ts" for E:\my-learning\sd-tracker-api\tests\types.spec.ts
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:160:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:203:36)
    at defaultLoad (node:internal/modules/esm/load:141:22)
    at async ModuleLoader.load (node:internal/modules/esm/loader:409:7)
    at async ModuleLoader.moduleProvider (node:internal/modules/esm/loader:291:45)
    at async link (node:internal/modules/esm/module_job:76:21)

I am not sure what to do to solve this? I followed the guidance of the following video along with the TS example project.

Video
Project

Any help would be greatly appreciated.

@huangjihua
Copy link

the same

@Sweet-PotEdos
Copy link

same here!

@AntonSweb
Copy link

same problem

@AntonSweb
Copy link

"loader=ts-node/esm" need to be added to the node-option in the mocha configuration

.mocharc.json

{
  "extension": [
    "ts"
  ],
  "spec": "tests/**/*.spec.ts",
  "require": [
    "ts-node/register"
  ],
  "node-option": [
    "loader=ts-node/esm"
  ],
  "recursive": true
}

@seventeen-yao
Copy link

This question comes from Chat.js 5.x, using 4.x will solve that.
chaijs/chai#1594 (reply in thread)

@sahin52
Copy link

sahin52 commented May 9, 2024

it was related to chai in probably your example
please also check
chaijs/chai#1597

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants