Skip to content

Commit

Permalink
test: mock context
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add committed Dec 10, 2023
1 parent d485783 commit 0faa4e6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .nycrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"branches": 95,
"branches": 94,
"lines": 99,
"functions": 99,
"statements": 99
Expand Down
7 changes: 4 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,10 @@ module.exports = {
isRuleTesterConstruction(declarator.init) &&
declarator.id.type === 'Identifier'
) {
(sourceCode.getDeclaredVariables || context.getDeclaredVariables)(
declarator
).forEach((variable) => {
const vars = sourceCode.getDeclaredVariables
? sourceCode.getDeclaredVariables(declarator)
: context.getDeclaredVariables(declarator);
vars.forEach((variable) => {
variable.references
.filter((ref) => ref.isRead())
.forEach((ref) => variableIdentifiers.add(ref.identifier));
Expand Down
24 changes: 21 additions & 3 deletions tests/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -761,8 +761,14 @@ describe('utils', () => {
sourceType: 'script',
nodejsScope: true,
});
const context = {
sourceCode: {
getDeclaredVariables:
scopeManager.getDeclaredVariables.bind(scopeManager),
},
}; // mock object
assert.deepEqual(
utils.getTestInfo(scopeManager, ast),
utils.getTestInfo(context, ast),
[],
'Expected no tests to be found'
);
Expand Down Expand Up @@ -827,7 +833,13 @@ describe('utils', () => {
sourceType: 'script',
nodejsScope: true,
});
const testInfo = utils.getTestInfo(scopeManager, ast);
const context = {
sourceCode: {
getDeclaredVariables:
scopeManager.getDeclaredVariables.bind(scopeManager),
},
}; // mock object
const testInfo = utils.getTestInfo(context, ast);

assert.strictEqual(
testInfo.length,
Expand Down Expand Up @@ -1021,7 +1033,13 @@ describe('utils', () => {
sourceType: 'script',
nodejsScope: true,
});
const testInfo = utils.getTestInfo(scopeManager, ast);
const context = {
sourceCode: {
getDeclaredVariables:
scopeManager.getDeclaredVariables.bind(scopeManager),
},
}; // mock object
const testInfo = utils.getTestInfo(context, ast);

assert.strictEqual(
testInfo.length,
Expand Down

0 comments on commit 0faa4e6

Please sign in to comment.