I've beend creating a node application with typescript and I'm using jest to write unit tests.
My issue: I can't really write unit tests simulating invalid function argument types, because typescript won't compile. I mean it's nice that typescript realises that I try to put wrong data in those functions, but the way I understood ts so far is, it only works during compile time. There won't be any checks of argument types during runtime.
So I still have to test the correct behaviour of my IO depending functions with jest, right?
I thought I'll just write a xyz.spec.js that and leave all the interfaces and types behind for that specific test. But then I receive an error from jest regarding the import of modules. I guess because it's not a ts file.
Do I have to change my jest or ts setup to make it work?
Here is a screenshot of the test and the error:
here my package.json:
{
"name": "state",
"version": "0.0.0",
"files": [
"build"
],
"main": "build/index",
"types": "build/index",
"scripts": {
"clean": "rimraf build && rimraf coverage",
"format": "prettier --write \"{src,__tests__}/**/*.ts\" --single-quote --trailing-comma es5",
"lint": "tslint --force --format verbose \"src/**/*.ts\"",
"prepublishOnly": "npm run build",
"start": "node ./build/index.js",
"prebuild": "npm run clean && npm run format && npm run lint && echo Using TypeScript && tsc --version",
"build": "tsc --pretty",
"build:watch": "nodemon --legacy-watch src/index.ts",
"test": "jest --no-cache",
"test:watch": "jest --no-cache --watch",
"coverage": "jest --no-cache --coverage"
},
"dependencies": {
"mongoose": "^5.6.0"
},
"devDependencies": {
"@types/jest": "^24.0.13",
"@types/mongoose": "^5.5.6",
"@types/node": "^10.14.7",
"coveralls": "^3.0.2",
"jest": "^24.8.0",
"nodemon": "^1.19.0",
"prettier": "^1.14.3",
"rimraf": "^2.6.2",
"ts-jest": "^24.0.2",
"ts-node": "^8.1.0",
"tslint": "^5.11.0",
"tslint-config-prettier": "^1.15.0",
"typescript": "^3.1.1"
},
"engines": {
"node": ">=10.0.0"
},
"jest": {
"preset": "ts-jest"
}
}
and here my tsconfig.json:
{
"compilerOptions": {
"declaration": true,
"module": "commonjs",
"moduleResolution": "node",
"lib": ["esnext"],
"target": "es2015",
"outDir": "./<%= buildpath %>",
"removeComments": true,
"inlineSourceMap": true,
"inlineSources": true,
"preserveConstEnums": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "build"]
}
Best regards fea
