我正在尝试为我的nodejs服务器应用程序编写测试,到目前为止一切正常。我可以将我的代码从打字稿编译为javascript,而不会出现任何错误,但是,当我尝试运行mocha测试时,由于在ConcurrentDictionary<K,V>
中找不到我自己定义的类型,打字稿预编译失败。
这是我在Dictionary<K,V>
内的声明
typings.d.ts
在我的typings.d.ts
中,我通过从原始包中导入原始接口/类型来使用这些类型
import {User} from "./users/user.model";
import {MainDatabase} from "./core/models/database.model";
import {Translation} from "./core/services/i18n";
import {LanguageDefinition} from "./core/models/language.model";
import {Reminder} from "./core/services/schedule/schedule.model";
import {UserProfileModel} from "./core/database/models/user_profile";
import {UserClientModel} from "./core/database/models/user_client";
import {PhoneNumberModel} from "./core/database/models/phone_number";
import {CountryModel} from "./core/database/models/country";
import {CustomerTypeModel} from "./core/database/models/customer/customer_type";
import {CustomerModel} from "./core/database/models/customer/customer";
import {ReminderModel} from "./core/database/models/reminder_options";
import {Customer} from "./customers/customer.model";
import {IUserAccount} from "./core/database/models/user_account";
import {UserAccountModel} from "./core/database/models/user_account";
import {MailModel} from "./core/services/mail/mail.model";
import {Request} from "express";
import {Models} from "sequelize";
declare global {
module NodeJS {
interface Global {
translation: Translation;
db: MainDatabase;
/*Classes to be reach through the global object*/
User: User;
Reminder: Reminder;
Customer: Customer;
Mailer: MailModel;
}
}
}
declare module "Express"{
export interface Request {
user?: IUserAccount;
authenticated: boolean;
locals: {
files?: File[];
};
locale?: LanguageDefinition;
info?:{
userAgent?: string;
ip?: string;
}
}
}
// extending the original sequelize interfaces
declare module "sequelize" {
/**
* Database models
* only list SEQUELIZE Models here not classes
* */
interface Models {
UserProfile: UserProfileModel;
UserAccount: UserAccountModel;
UserClient: UserClientModel;
Reminder: ReminderModel;
PhoneNumber: PhoneNumberModel,
CustomerType: CustomerTypeModel,
Customer: CustomerModel,
Country: CountryModel
}
}
现在,当我运行app.ts
时,所有文件都已成功编译,并且服务器启动时没有任何问题。由于我的tsconfig.json配置,编译器可以看到//omitted for brevity
/**
* Setup all globals that do not belong to the database
* */
private setupServerGlobals() {
try {
global.Mailer = new MailModel();
} catch (e) {
console.error('setupServerGlobals', e);
}
}
private setupTranslation(): Observable<any> {
// create translation
const translation = new Translation({
language: { language: 'de', fullLanguage: 'de' },
});
return translation.init().pipe(
tap(() => {
global.translation = translation;
}),
);
}
内部的全局定义类型。
但是当我运行tsc --watch & nodemon ./dist/src/app.js
时会抛出此错误:
typings.d.ts
我的设置如下:
+每个要测试的文件旁边都有所有测试文件
+我有一个测试文件夹,其中有我的tsc & mocha
文件
这是我的/Users/user/Documents/Project/subfolder/project-name/node_modules/ts-node/src/index.ts:261
return new TSError(diagnosticText, diagnosticCodes)
^
TSError: ⨯ Unable to compile TypeScript:
src/app.ts(60,14): error TS2339: Property 'Mailer' does not exist on type 'Global'.
src/app.ts(78,11): error TS2339: Property 'info' does not exist on type 'Request'.
src/app.ts(156,16): error TS2339: Property 'translation' does not exist on type 'Global'.
文件的内容:
mocha.opts
最后这是我的mocha.opts
文件:
src/**/*.spec.ts
--require ts-node/register
--recursive
要像运行服务器一样运行我的测试,而不会编译问题。
感谢您的帮助。
我可以通过像这样在tsconfig.json
文件中导入{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"allowJs": true,
"importHelpers": true,
"jsx": "react",
"alwaysStrict": true,
"sourceMap": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noImplicitAny": false,
"noImplicitThis": false,
"strictNullChecks": false,
"outDir": "dist"
},
"include": ["**/*.ts"]
}
的类型来解决问题
typings.d.ts
但是对我来说,这似乎并不是解决此问题的好方法,因为在每个文件中,我将使用app.ts
变量,因此需要像上面那样重新导入类型。
如果您知道怎么做,仍然可以随时解决此问题。
答案 0 :(得分:3)
就我而言,我将以下内容插入了tsconfig.json
{
"ts-node": {
"transpileOnly": true
},
"compilerOptions": {
...
}
}
它有效。
答案 1 :(得分:0)
TypeScript在tsconfig.json
中包含以下typeRoots and types配置:
"typeRoots": [
"./node_modules/@types",
"./typings"
],
"types": [
"mocha"
]
如果指定了typeRoots,则仅包含typeRoots下的软件包 typeRoots:在这些文件夹中查找键入内容。
如果指定了类型,则仅包括列出的软件包。例如摩卡咖啡。如果需要typeRoots的所有软件包(如您的情况),则不要提供types选项。指定"types": []
将禁用自动包含@types软件包。