如何用 Chai expect 替换 Jext expect 的类型?
How can I replace the type of Jext expect by the Chai expect?
我想使用 Chai 作为断言库而不是 Jest。我使用打字稿,我想用 Chai expect 的类型替换 global Jest expect。
我尝试做类似的事情:
import chai from "chai";
type ChaiExpect = typeof chai.expect;
declare global {
export const expect: ChaiExpect;
}
global.expect = chai.expect;
但是打字稿抱怨是因为:
Cannot redeclare block-scoped variable 'expect'.ts(2451)
index.d.ts(39, 15): 'expect' was also declared here.
如何覆盖 jest index.d.ts
中声明的类型?
您可以在 javascript 端重新分配 global.expect
的运行时值,但是在打字稿端,没有简单的出路。
Jest 将 expect
声明为 全局变量 (参见 @types/jest/index.d.ts(39, 15)
)。目前 typescript 无法在同一块作用域.
中覆盖 已声明变量的类型
因此,只要您保持 @types/jest/index.d.ts
原样,就无法抑制该错误。
解决方案
1。简单的方法
使用 chai 的 expect
的最简单方法是简单地导入并在每个 .test.ts
文件中使用它:
// sum.test.ts
import { expect } from 'chai'
import { sum } from './sum';
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).eq(3)
});
2。艰难的道路
现在,如果您真的无法忍受重复的 import { expect } from 'chai'
行,那么这里是更难的路径:
将 node_modules/@types/jest
移动到 types/jest
,确保它已在 node_modules
文件夹中。同时从 "package.json => devDependencies".
中删除“@types/jest”
修改types/jest/index.d.ts
,将expect
的类型替换为chai的。您需要将此自定义类型声明提交到您的 git 存储库。
// index.d.ts
+ /// <reference types="chai" />
- declare const expect: Expect
+ declare const expect: Chai.ExpectStatic
- 在您的
tsconfig.json
中,添加:
{
"compilerOptions": {
"typeRoots": ["node_modules/@types", "types"],
...
}
- 创建
jestSetup.js
并添加这一行:
global.expect = require("chai").expect
- 在
jest.config.js
中,设置:
setupFilesAfterEnv: ['./jestSetup.js'],
// or `setupTestFrameworkScriptFile` for older version.
给你。您现在可以在全局范围内使用 chai expect
。
我想使用 Chai 作为断言库而不是 Jest。我使用打字稿,我想用 Chai expect 的类型替换 global Jest expect。
我尝试做类似的事情:
import chai from "chai";
type ChaiExpect = typeof chai.expect;
declare global {
export const expect: ChaiExpect;
}
global.expect = chai.expect;
但是打字稿抱怨是因为:
Cannot redeclare block-scoped variable 'expect'.ts(2451)
index.d.ts(39, 15): 'expect' was also declared here.
如何覆盖 jest index.d.ts
中声明的类型?
您可以在 javascript 端重新分配 global.expect
的运行时值,但是在打字稿端,没有简单的出路。
Jest 将 expect
声明为 全局变量 (参见 @types/jest/index.d.ts(39, 15)
)。目前 typescript 无法在同一块作用域.
因此,只要您保持 @types/jest/index.d.ts
原样,就无法抑制该错误。
解决方案
1。简单的方法
使用 chai 的 expect
的最简单方法是简单地导入并在每个 .test.ts
文件中使用它:
// sum.test.ts
import { expect } from 'chai'
import { sum } from './sum';
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).eq(3)
});
2。艰难的道路
现在,如果您真的无法忍受重复的 import { expect } from 'chai'
行,那么这里是更难的路径:
将
node_modules/@types/jest
移动到types/jest
,确保它已在node_modules
文件夹中。同时从 "package.json => devDependencies". 中删除“@types/jest”
修改
types/jest/index.d.ts
,将expect
的类型替换为chai的。您需要将此自定义类型声明提交到您的 git 存储库。
// index.d.ts
+ /// <reference types="chai" />
- declare const expect: Expect
+ declare const expect: Chai.ExpectStatic
- 在您的
tsconfig.json
中,添加:
{
"compilerOptions": {
"typeRoots": ["node_modules/@types", "types"],
...
}
- 创建
jestSetup.js
并添加这一行:
global.expect = require("chai").expect
- 在
jest.config.js
中,设置:
setupFilesAfterEnv: ['./jestSetup.js'],
// or `setupTestFrameworkScriptFile` for older version.
给你。您现在可以在全局范围内使用 chai expect
。