cya*_*sin 6 types chai typescript tsconfig
Other related questions are just asked about JavaScript, but I know the Chai team already provided 'chai/register-expect', etc..
I was migrating from Jest to Chai, and when I used Jest it was done just by typing 'jest' to the "types" field in file tscofnig.json. Then the expect function was automatically referred to with @types/jest index.d.ts.
But @types/chai or Chai do not support this. And they recommend, before reporting an issue, to post on Stack Overflow. What on Earth, right?
After surfing about this, I realize everyone imports the 'expect' function per file, like TypeORM or other TypeScript projects... Holy bleep, it is so awwwwwwwful.
Why on Earth should I import expect() per file? Isn't there a way to avoid that?
I can return to Jest, but that performance is so horrible fecal matter. It is better importing expects for all files.
mocha -r chai/register-expect is not working either.
I was testing:
npx mocha -r node_modules/ts-node/register/transpile-only -r chai/register-expect -r ts-node/register -r tsconfig-paths/register some.test.ts
Run Code Online (Sandbox Code Playgroud)
Here is my tsconfig.json file.
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es5",
"noImplicitAny": false,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"baseUrl": "src",
"skipLibCheck": true,
"downlevelIteration" : true,
"paths": {
... bla bla
"main/*" : [
"main/*"
],
"controllers/*" : [
"main/controllers/*"
],
"middleware/*" : [
"main/middleware/*"
],
"*": [
"node_modules/*"
]
},
"types" : [
"node",
"mocha",
"chai"
],
"typeRoots": [
"node_modules/@types",
"types"
],
"lib": [
"es2017",
"dom"
],
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"resolveJsonModule" : true
},
"exclude": [
"node_modules"
],
"include": [
"src/**/*.ts",
"**/*.test.ts"
]
}
Run Code Online (Sandbox Code Playgroud)
chai/register-expect将注册一个全局 Chai 函数expect。
您需要通过创建自定义定义文件向 TypeScript 编译器解释您拥有它。
创建目录结构:
typings
global
index.d.ts
Run Code Online (Sandbox Code Playgroud)
在您的index.d.ts文件中,添加以下内容:
declare const expect: Chai.ExpectStatic
Run Code Online (Sandbox Code Playgroud)
现在你的 tsconfig.json 应该是这样的:
"typeRoots": [
"node_modules/@types", "./typings"
],
"types": [
"mocha",
"chai",
"node",
"global"
]
Run Code Online (Sandbox Code Playgroud)
请注意模块typings的目录和导入global。
这是必需的,因为 ts-node 通常不关心您的自定义类型。
| 归档时间: |
|
| 查看次数: |
4137 次 |
| 最近记录: |