mocha.js - چگونه میتوان از Mocha و Jest با TypeScript بدون تداخل استفاده کرد؟
من در حال تلاش برای نصب Mocha و Jest همراه با تایپها در یک پروژه هستم. ما از بررسی نوع سختگیرانه استفاده میکنیم، بنابراین خطاهایی مرتبط با نوعهای جهانی متضاد دریافت میکنم.
من سعی کردهام اعلان ماژول مبهمی ایجاد کنم و تنها Mocha را در types در tsconfig تعریف کنم. من تلاش کردهام اعلان Jest را حذف کنم - اما این به صورت جزئی کمک میکند. غیرفعال کردن بررسی نوع سختگیرانه یا بررسی کتابخانه گزینهای نیست.
من انتظار داشتم که آن را بهدرستی کار کند، اما در عوض با خطاهای بعدی مواجه شدم.
node_modules/@types/jest/index.d.ts(29,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'beforeEach' must be of type 'HookFunction', but here has type 'Lifecycle'.
node_modules/@types/jest/index.d.ts(31,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'afterEach' must be of type 'HookFunction', but here has type 'Lifecycle'.
node_modules/@types/jest/index.d.ts(32,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'describe' must be of type 'SuiteFunction', but here has type 'Describe'.
node_modules/@types/jest/index.d.ts(34,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'xdescribe' must be of type 'PendingSuiteFunction', but here has type 'Describe'.
node_modules/@types/jest/index.d.ts(35,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'it' must be of type 'TestFunction', but here has type 'It'.
node_modules/@types/jest/index.d.ts(37,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'xit' must be of type 'PendingTestFunction', but here has type 'It'.
node_modules/@types/jest/index.d.ts(38,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'test' must be of type 'TestFunction', but here has type 'It'.
node_modules/@types/node/globals.d.ts(926,15): error TS2430: Interface 'Global' incorrectly extends interface 'MochaGlobals'.
Types of property 'describe' are incompatible.
Type 'Describe' is not assignable to type 'SuiteFunction'.
Types of property 'only' are incompatible.
Type 'DescribeBase' is not assignable to type 'ExclusiveSuiteFunction'.
Type 'void' is not assignable to type 'Suite'.
حتی حذف همه نوعها از Jest هم این خطا را به من میدهد:
node_modules/@types/node/globals.d.ts(926,15): error TS2430: Interface 'Global' incorrectly extends interface 'MochaGlobals'.
Types of property 'describe' are incompatible.
Type 'Describe' is not assignable to type 'SuiteFunction'.
Types of property 'only' are incompatible.
Type 'DescribeBase' is not assignable to type 'ExclusiveSuiteFunction'.
Type 'void' is not assignable to type 'Suite'.
پاسخ شماره ۱
تا اوت ۲۰۱۹، به نظر میرسد این مشکل در master برطرف شده است، اما نسخه جدید هنوز منتشر نشده است.
در این بین، توانستم این خطاها را با افزودن موارد زیر به یکی از تعاریفم برطرف کنم:
declare module '@jest/types/build/Global' {
interface DescribeBase extends mocha.SuiteFunction {}
interface ItBase extends mocha.TestFunction {}
}
پاسخ شماره ۲
من در فایل قفل بسته جستجو کردم تا ببینم کدام وابستگیها از @types/jest استفاده میکنند و متوجه شدم که در مورد من هنوز ts-jest در پروژه وجود دارد که آن را حذف کردم و تعارضها ناپدید شدند
