forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Obs AI Assistant] Use Mocha & Synthtrace (elastic#173993)
Two changes here: 1. Use Mocha instead of custom test runner. This allows us to use Mocha's much more extensive API to run the tests: skipping tests, before and after hooks, grep etc. 2. Make Synthtrace available for running tests. One note: - I've added a rule to automatically inject a type reference for Mocha, via `@kbn/ambient-ftr-types`. The reason is Mocha is not typed by default (ie, the types are not available in the global space), because it conflicts with Jest. The rule enforces that `@kbn/ambient-ftr-types` is imported so things like `before()` can be used.
- Loading branch information
1 parent
bf8f721
commit 5359ebe
Showing
17 changed files
with
789 additions
and
258 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
packages/kbn-eslint-plugin-imports/src/rules/require_import.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { RuleTester } from 'eslint'; | ||
import { RequireImportRule } from './require_import'; | ||
import dedent from 'dedent'; | ||
|
||
const fmt = (str: TemplateStringsArray) => dedent(str) + '\n'; | ||
|
||
const tsTester = [ | ||
'@typescript-eslint/parser', | ||
new RuleTester({ | ||
parser: require.resolve('@typescript-eslint/parser'), | ||
parserOptions: { | ||
sourceType: 'module', | ||
ecmaVersion: 2018, | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
}), | ||
] as const; | ||
|
||
const babelTester = [ | ||
'@babel/eslint-parser', | ||
new RuleTester({ | ||
parser: require.resolve('@babel/eslint-parser'), | ||
parserOptions: { | ||
sourceType: 'module', | ||
ecmaVersion: 2018, | ||
requireConfigFile: false, | ||
babelOptions: { | ||
presets: ['@kbn/babel-preset/node_preset'], | ||
}, | ||
}, | ||
}), | ||
] as const; | ||
|
||
for (const [name, tester] of [tsTester, babelTester]) { | ||
describe(name, () => { | ||
tester.run('@kbn/imports/require_import', RequireImportRule, { | ||
valid: [ | ||
{ | ||
options: ['mocha'], | ||
filename: 'foo.ts', | ||
code: fmt` | ||
import 'mocha'; | ||
/// <reference types="mocha"/> | ||
describe(( ) => { | ||
before(( ) => { | ||
}); | ||
}); | ||
`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
options: ['mocha'], | ||
filename: 'foo.ts', | ||
code: fmt` | ||
describe(( ) => { | ||
before(( ) => { | ||
}); | ||
}); | ||
`, | ||
output: fmt`/// <reference types="mocha"/> | ||
describe(( ) => { | ||
before(( ) => { | ||
}); | ||
});`, | ||
errors: [ | ||
{ | ||
line: 1, | ||
message: `Required module 'mocha' is not imported as a type reference`, | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
}); | ||
} |
105 changes: 105 additions & 0 deletions
105
packages/kbn-eslint-plugin-imports/src/rules/require_import.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import type { Rule } from 'eslint'; | ||
import { load } from 'cheerio'; | ||
|
||
type StringModuleConfig = string; | ||
|
||
interface ObjectModuleConfig { | ||
module: string; | ||
as: ReferenceModuleAs; | ||
} | ||
|
||
type ModuleConfig = StringModuleConfig | ObjectModuleConfig; | ||
|
||
enum ReferenceModuleAs { | ||
typeReference = 'typeReference', | ||
} | ||
|
||
export const RequireImportRule: Rule.RuleModule = { | ||
meta: { | ||
type: 'problem', | ||
fixable: 'code', | ||
docs: { | ||
url: 'https://github.com/elastic/kibana/blob/main/packages/kbn-eslint-plugin-imports/README.mdx#kbnimportsrequire_import', | ||
}, | ||
schema: { | ||
type: 'array', | ||
items: { | ||
oneOf: [ | ||
{ | ||
type: 'string', | ||
}, | ||
{ | ||
type: 'object', | ||
additionalProperties: false, | ||
additionalItems: false, | ||
properties: { | ||
module: { | ||
type: 'string', | ||
}, | ||
as: { | ||
type: 'string', | ||
}, | ||
}, | ||
required: ['module', 'type'], | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
|
||
create(context) { | ||
const requiredImports: ModuleConfig[] = context.options; | ||
|
||
const mappedOptions: ObjectModuleConfig[] = requiredImports.map((config) => { | ||
if (typeof config === 'string') { | ||
return { | ||
module: config, | ||
as: ReferenceModuleAs.typeReference, | ||
}; | ||
} | ||
return config; | ||
}); | ||
|
||
return { | ||
'Program:exit': (node) => { | ||
mappedOptions.forEach((option) => { | ||
switch (option.as) { | ||
case ReferenceModuleAs.typeReference: | ||
const hasImport = node.comments?.some((comment) => { | ||
const nodeText = comment.value.match(/\/\s*(<.*>)/)?.[1]; | ||
if (nodeText) { | ||
const parsedNode = load(nodeText, { xml: true })()._root?.children()[0]; | ||
return ( | ||
parsedNode && | ||
parsedNode.name === 'reference' && | ||
parsedNode.attribs.types === option.module | ||
); | ||
} | ||
}); | ||
|
||
if (!hasImport) { | ||
context.report({ | ||
node, | ||
message: `Required module '${option.module}' is not imported as a type reference`, | ||
fix(fixer) { | ||
return fixer.insertTextBefore( | ||
node.body[0], | ||
`/// <reference types="${option.module}"/>\n\n` | ||
); | ||
}, | ||
}); | ||
} | ||
} | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
x-pack/plugins/observability_ai_assistant/scripts/evaluation/.eslintrc.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"overrides": [ | ||
{ | ||
"files": [ | ||
"**/*.spec.ts" | ||
], | ||
"rules": { | ||
"@kbn/imports/require_import": [ | ||
"error", | ||
"@kbn/ambient-ftr-types" | ||
], | ||
"@typescript-eslint/triple-slash-reference": [ | ||
"off" | ||
], | ||
"spaced-comment": [ | ||
"off" | ||
] | ||
} | ||
} | ||
] | ||
} |
Oops, something went wrong.