[英]How can I validate an ejs template of a typescript file with typescript-eslint
I have an ejs template for a typescript file, I need to write a Unit Test with #jest to validate the rendered typescript output of my template using #typescript-eslint (not tslint because it will be deprecated soon: https://medium.com/palantir/tslint-in-2019-1a144c2317a9 ) 我有一个打字稿文件的ejs模板,我需要用#jest编写一个单元测试,用#scriptcript-eslint验证我的模板的渲染的typescript输出(不是tslint,因为它很快就会被弃用: https:// medium。 com / palantir / tslint-in-2019-1a144c2317a9 )
So, I need to require the typescript-eslint module in my spec file and run the validation of my rendered ejs by code like: 所以,我需要在我的spec文件中要求typescript-eslint模块,并通过以下代码运行我渲染的ejs的验证:
it('should be valid typescript file', () => {
return ejs.renderFile('my-ejs-template.ts.ejs', {}).then(async (view) => {
const eslint = require('@typescript-eslint/eslint-plugin');
// what is next ???
});
});
Any help will be very appreciated 任何帮助将非常感激
First install the following modules in devDependencies: 首先在devDependencies中安装以下模块:
Second, import the module eslint in your spec file as follows: 其次,在spec文件中导入模块eslint,如下所示:
const eslint = require('eslint');
Third, create an .eslintrc.js configuration file to parse typescript files, like: 第三,创建一个.eslintrc.js配置文件来解析打字稿文件,例如:
module.exports = {
env: {
node: true,
es6: true,
},
extends: ['plugin:@typescript-eslint/recommended'],
parser: '@typescript-eslint/parser',
parserOptions: {
sourceType: 'module',
typescript: true,
modules: true,
ecmaVersion: 6,
ecmaFeatures: {
experimentalObjectRestSpread: true,
},
},
plugins: ['@typescript-eslint'],
rules: {
indent: ['error', 4],
'linebreak-style': ['error', 'unix'],
quotes: ['error', 'single'],
semi: ['error', 'always'],
},
};
Then, import the above config file in your spec file and create a function to get the linter: 然后,在spec文件中导入上面的配置文件并创建一个函数来获取linter:
const configTsEslint = require('.eslintrc-typescript');
const tsLinter = () => {
const Linter = eslint.Linter;
return new Linter();
};
Finally, validate your typescript file as follows: 最后,验证您的typescript文件,如下所示:
it('should be valid typescript file', () => {
return ejs.renderFile('my-ejs-template.ts.ejs', {}).then(async (view) => {
const result = tsLinter().verifyAndFix(view);
expect(result.messages).toStrictEqual([]);
expect(result.fixed).toBe(true);
});
});
You can add rules that are supported by [typescript-eslint][1] 您可以添加[typescript-eslint] [1]支持的规则
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.