Skip to content

Commit 512163f

Browse files
committed
Added tests with violations
1 parent 5854bf6 commit 512163f

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

packages/code-analyzer-eslint-engine/test/end-to-end.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ describe('End to end test', () => {
6969
'@typescript-eslint/no-unused-vars', // there are 4 of these
7070
'no-invalid-regexp'
7171
]));
72+
const violationsFromHTMLFile: Violation[] = engineRunResults.violations.filter(v => path.extname(v.codeLocations[0].file) === '.html');
73+
expect(violationsFromHTMLFile).toHaveLength(1);
74+
expect(violationsFromHTMLFile[0].ruleName).toEqual('@salesforce-ux/slds/enforce-bem-usage');
7275

7376
const warnLogs: LogEvent[] = logEvents.filter(e => e.logLevel == LogLevel.Warn);
7477
expect(warnLogs).toHaveLength(0);
@@ -109,6 +112,10 @@ describe('End to end test', () => {
109112
'no-invalid-regexp'
110113
]));
111114

115+
// SLDS violations are only relevant for v9+
116+
const violationsFromHTMLFile: Violation[] = engineRunResults.violations.filter(v => path.extname(v.codeLocations[0].file) === '.html');
117+
expect(violationsFromHTMLFile).toHaveLength(0);
118+
112119
const warnLogs: LogEvent[] = logEvents.filter(e => e.logLevel == LogLevel.Warn);
113120
expect(warnLogs).toHaveLength(1);
114121
expect(warnLogs[0].message).toContain('Using ESLint v8 instead of ESLint v9');

packages/code-analyzer-eslint-engine/test/engine.test.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -499,16 +499,31 @@ describe('Typical tests for the runRules method of ESLintEngine', () => {
499499
"primaryLocationIndex": 0,
500500
"ruleName": "@typescript-eslint/no-wrapper-object-types"
501501
};
502+
const expectedHTMLViolation_enforceBemUsage: Violation = {
503+
"codeLocations": [
504+
{
505+
"endColumn": 27,
506+
"endLine": 1,
507+
"file": path.join(workspaceWithNoCustomConfig, 'dummy1.html'),
508+
"startColumn": 11,
509+
"startLine": 1
510+
}
511+
],
512+
"message": "slds-m-top--none has been retired. Update it to the new name slds-m-top_none.",
513+
"primaryLocationIndex": 0,
514+
"ruleName": "@salesforce-ux/slds/enforce-bem-usage"
515+
};
502516

503-
it('When running with defaults and no customizations, then violations for javascript and typescript are found correctly', async () => {
517+
it('When running with defaults and no customizations, then violations for javascript, typescript and html are found correctly', async () => {
504518
const engine: Engine = await createEngineFromPlugin(DEFAULT_CONFIG_FOR_TESTING);
505519
const runOptions: RunOptions = createRunOptions(new Workspace('id', [workspaceWithNoCustomConfig]));
506-
const results: EngineRunResults = await engine.runRules(['no-invalid-regexp', '@typescript-eslint/no-wrapper-object-types'], runOptions);
520+
const results: EngineRunResults = await engine.runRules(['no-invalid-regexp', '@typescript-eslint/no-wrapper-object-types', '@salesforce-ux/slds/enforce-bem-usage'], runOptions);
507521

508-
expect(results.violations).toHaveLength(3);
522+
expect(results.violations).toHaveLength(4);
509523
expect(results.violations).toContainEqual(expectedJsViolation_noInvalidRegexp);
510524
expect(results.violations).toContainEqual(expectedTsViolation_noInvalidRegexp);
511525
expect(results.violations).toContainEqual(expectedTsViolation_noWrapperObjectTypes);
526+
expect(results.violations).toContainEqual(expectedHTMLViolation_enforceBemUsage);
512527
});
513528

514529
it('When workspace only targets javascript files, then only javascript violations are returned', async () => {
@@ -528,7 +543,15 @@ describe('Typical tests for the runRules method of ESLintEngine', () => {
528543
expect(results.violations).toEqual([expectedTsViolation_noInvalidRegexp]);
529544
});
530545

531-
it('When workspace does not contains javascript or typescript files, then zero violations are returned', async () => {
546+
it('When workspace only contains html files, then only html violations are returned', async () => {
547+
const engine: Engine = await createEngineFromPlugin(DEFAULT_CONFIG_FOR_TESTING);
548+
const runOptions: RunOptions = createRunOptions(new Workspace('id', [path.join(workspaceWithNoCustomConfig, 'dummy1.html')]));
549+
const results: EngineRunResults = await engine.runRules(['@salesforce-ux/slds/enforce-bem-usage'], runOptions);
550+
551+
expect(results.violations).toEqual([expectedHTMLViolation_enforceBemUsage]);
552+
});
553+
554+
it('When workspace does not contains javascript, typescript or html files, then zero violations are returned', async () => {
532555
const engine: Engine = await createEngineFromPlugin(DEFAULT_CONFIG_FOR_TESTING);
533556
const runOptions: RunOptions = createRunOptions(new Workspace('id', [path.join(workspaceWithNoCustomConfig, 'dummy3.txt')]));
534557
const results: EngineRunResults = await engine.runRules(['no-invalid-regexp'], runOptions);
@@ -587,7 +610,7 @@ describe('Typical tests for the runRules method of ESLintEngine', () => {
587610
});
588611

589612

590-
it('When custom rules only apply to file extensions that are not javascript or typescript based, then when specifying file extensions, the rules run', async () => {
613+
it('When custom rules only apply to file extensions that are not javascript, typescript, or html based, then when specifying file extensions, the rules run', async () => {
591614
const engine: Engine = await createEngineFromPlugin({...DEFAULT_CONFIG_FOR_TESTING,
592615
eslint_config_file: path.join(workspaceThatHasCustomConfigWithNewRules, 'eslint-config-only-for-other-files.js'),
593616
file_extensions:{
@@ -622,7 +645,7 @@ describe('Typical tests for the runRules method of ESLintEngine', () => {
622645
engine.onEvent(EventType.LogEvent, (event: LogEvent) => logEvents.push(event));
623646

624647
const runOptions: RunOptions = createRunOptions(new Workspace('id', [path.join(testDataFolder,'workspaceWithFlatConfigJs')]));
625-
const results: EngineRunResults = await engine.runRules(['no-invalid-regexp', '@typescript-eslint/no-wrapper-object-types'], runOptions);
648+
const results: EngineRunResults = await engine.runRules(['no-invalid-regexp', '@typescript-eslint/no-wrapper-object-types', '@salesforce-ux/slds/enforce-bem-usage'], runOptions);
626649

627650
expect(results.violations).toHaveLength(2); // Should not contain js violations but should contain ts violations
628651
expect(path.extname(results.violations[0].codeLocations[0].file)).toEqual('.ts');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<p>Bonjour!</p>
1+
<p class="slds-m-top--none">Bonjour!</p>

0 commit comments

Comments
 (0)