Skip to content

Commit 2dfb5eb

Browse files
authored
test: update SourceCodeTraverser tests (#19763)
* test: update `SourceCodeTraverser` tests * clarify additional checks
1 parent b03ad17 commit 2dfb5eb

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

‎tests/lib/linter/source-code-traverser.js‎

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -344,12 +344,12 @@ describe("SourceCodeTraverser", () => {
344344
describe("traversing the entire AST", () => {
345345
/**
346346
* Gets a list of emitted types/selectors from the traverser, in emission order
347-
* @param {string} sourceText The source text to parse and traverse
347+
* @param {ASTNode} ast The AST to traverse
348348
* @param {Array<string>|Set<string>} possibleQueries Selectors to detect
349349
* @returns {Array[]} A list of emissions, in the order that they were emitted. Each emission is a two-element
350350
* array where the first element is a string, and the second element is the emitted AST node.
351351
*/
352-
function getEmissions(sourceText, possibleQueries) {
352+
function getEmissions(ast, possibleQueries) {
353353
const emissions = [];
354354
const emitter = Object.create(createEmitter(), {
355355
emit: {
@@ -359,7 +359,6 @@ describe("SourceCodeTraverser", () => {
359359

360360
possibleQueries.forEach(query => emitter.on(query, () => {}));
361361

362-
const ast = espree.parse(sourceText, ESPREE_CONFIG);
363362
const sourceCode = createMockSourceCode(ast);
364363
const traverser = SourceCodeTraverser.getInstance(jslang);
365364

@@ -374,7 +373,7 @@ describe("SourceCodeTraverser", () => {
374373
* Creates a test case that asserts a particular sequence of traverser emissions
375374
* @param {string} sourceText The source text that should be parsed and traversed
376375
* @param {string[]} possibleQueries A collection of selectors that rules are listening for
377-
* @param {Array[]} expectedEmissions A function that accepts the AST and returns a list of the emissions that the
376+
* @param {(ast: ASTNode) => Array[]} getExpectedEmissions A function that accepts the AST and returns a list of the emissions that the
378377
* traverser is expected to produce, in order.
379378
* Each element of this list is an array where the first element is a selector (string), and the second is an AST node
380379
* This should only include emissions that appear in possibleQueries.
@@ -383,13 +382,28 @@ describe("SourceCodeTraverser", () => {
383382
function assertEmissions(
384383
sourceText,
385384
possibleQueries,
386-
expectedEmissions,
385+
getExpectedEmissions,
387386
) {
388387
it(possibleQueries.join("; "), () => {
389388
const ast = espree.parse(sourceText, ESPREE_CONFIG);
390-
const emissions = getEmissions(sourceText, possibleQueries);
391389

392-
assert.deepStrictEqual(emissions, expectedEmissions(ast));
390+
const actualEmissions = getEmissions(ast, possibleQueries);
391+
const expectedEmissions = getExpectedEmissions(ast);
392+
393+
assert.deepStrictEqual(actualEmissions, expectedEmissions);
394+
395+
/*
396+
* `assert.deepStrictEqual()` compares objects by their properties.
397+
* Here, we additionally compare node objects by reference to ensure
398+
* the emitted objects are expected instances from the AST.
399+
*/
400+
actualEmissions.forEach((actualEmission, index) => {
401+
assert.strictEqual(
402+
actualEmission[1],
403+
expectedEmissions[index][1],
404+
"Expected a node instance from the AST",
405+
);
406+
});
393407
});
394408
}
395409

@@ -675,7 +689,7 @@ describe("SourceCodeTraverser", () => {
675689
* @param {ASTNode} ast The AST to traverse
676690
* @param {Record<string, string[]>} visitorKeys The custom visitor keys.
677691
* @param {string[]} possibleQueries A collection of selectors that rules are listening for
678-
* @param {Array[]} expectedEmissions A function that accepts the AST and returns a list of the emissions that the
692+
* @param {(ast: ASTNode) => Array[]} getExpectedEmissions A function that accepts the AST and returns a list of the emissions that the
679693
* generator is expected to produce, in order.
680694
* Each element of this list is an array where the first element is a selector (string), and the second is an AST node
681695
* This should only include emissions that appear in possibleQueries.
@@ -685,16 +699,30 @@ describe("SourceCodeTraverser", () => {
685699
ast,
686700
visitorKeys,
687701
possibleQueries,
688-
expectedEmissions,
702+
getExpectedEmissions,
689703
) {
690704
it(possibleQueries.join("; "), () => {
691-
const emissions = getEmissions(
705+
const actualEmissions = getEmissions(
692706
ast,
693707
visitorKeys,
694708
possibleQueries,
695-
).filter(emission => possibleQueries.includes(emission[0]));
696-
697-
assert.deepStrictEqual(emissions, expectedEmissions(ast));
709+
);
710+
const expectedEmissions = getExpectedEmissions(ast);
711+
712+
assert.deepStrictEqual(actualEmissions, expectedEmissions);
713+
714+
/*
715+
* `assert.deepStrictEqual()` compares objects by their properties.
716+
* Here, we additionally compare node objects by reference to ensure
717+
* the emitted objects are expected instances from the AST.
718+
*/
719+
actualEmissions.forEach((actualEmission, index) => {
720+
assert.strictEqual(
721+
actualEmission[1],
722+
expectedEmissions[index][1],
723+
"Expected a node instance from the AST",
724+
);
725+
});
698726
});
699727
}
700728

0 commit comments

Comments
 (0)