eclipse-thingweb / eclipse-thingweb/node-wot
Hard to find typescript error in tests - and proposal
- Dominant language
- TypeScript
- Stars
- 192
- Forks
- 100
- Avg merge
- 3d 2h
- Merged PRs (30d)
- 6
Description
At this time, if a typo or typescript error appears in a test.ts file mocha wont find it.
For instance if a test contains
```
const oops: number = "this is not a number but a string, typescript will complain";
```
running mocha produces:
```
npx mocha test/opcua-codec-test.ts
Exception during run: TypeError: Unknown file extension ".ts" for /home/etienne/projects/node-wot/packages/binding-opcua/test/opcua-codec-test.ts
at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:219:9)
at defaultGetFormat (node:internal/modules/esm/get_format:245:36)
at defaultLoad (node:internal/modules/esm/load:120:22)
at async ModuleLoader.loadAndTranslate (node:internal/modules/esm/loader:580:32)
at async ModuleJob._link (node:internal/modules/esm/module_job:116:19) {
code: 'ERR_UNKNOWN_FILE_EXTENSION'
```
with no clue about where the error is.
At this time the solution is to scrutanize the source code in the IDE for the wavy red indicators. we have no way to detect those erreur during build time as test/**/*.ts are not accessible in the tsconfig.json
The reason is that Mocha doesn't perform typescript compilatipon
A prefered situation would be that test scripts get compiled automatically as we build the solution.
One way to do this would be to change the tsconfig.json file
```diff
// in tsconfig.json
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist",
- "rootDir": "src",
+ "rootDir": "./",
"skipLibCheck": true
},
+ "include": ["./src/**/*"],
+ "include": ["./src/**/*", "./test/**/*],
"references": [{ "path": "../core" }]
}
```
This has an impact on the generated files that goes
from:
```txt
dist/main.js
```
to:
```txt
dist/src/main.js
dist/test/main.test.js
```
and also requires a small change in the package.jon file, as we may not want to publish the transpiled test source.
```diff
{
"name": "@node-wot/binding-opcua",
"version": "0.9.2",
"files": [
- "dist",
+ "dist/src",
"src"
],
- "main": "dist/index.js",
- "types": "dist/index.d.ts",
+ "main": "dist/src/index.js",
+ "types": "dist/src/index.d.ts",
"devDependencies": {
```
Open for comment !
Contributor guide
Assessment
This issue has not been assessed yet.