Skip to content

Commit a79f7a5

Browse files
committed
test: mock context
1 parent d42b905 commit a79f7a5

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

.nycrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"branches": 95,
2+
"branches": 94,
33
"lines": 99,
44
"functions": 99,
55
"statements": 99

lib/utils.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -468,9 +468,10 @@ module.exports = {
468468
isRuleTesterConstruction(declarator.init) &&
469469
declarator.id.type === 'Identifier'
470470
) {
471-
(sourceCode.getDeclaredVariables || context.getDeclaredVariables)(
472-
declarator
473-
).forEach((variable) => {
471+
const vars = sourceCode.getDeclaredVariables
472+
? sourceCode.getDeclaredVariables(declarator)
473+
: context.getDeclaredVariables(declarator);
474+
vars.forEach((variable) => {
474475
variable.references
475476
.filter((ref) => ref.isRead())
476477
.forEach((ref) => variableIdentifiers.add(ref.identifier));

tests/lib/utils.js

+30-3
Original file line numberDiff line numberDiff line change
@@ -761,8 +761,17 @@ describe('utils', () => {
761761
sourceType: 'script',
762762
nodejsScope: true,
763763
});
764+
const context = {
765+
sourceCode: {
766+
getDeclaredVariables:
767+
scopeManager.getDeclaredVariables.bind(scopeManager),
768+
},
769+
}; // mock object
770+
context.getSourceCode = function () {
771+
return context.sourceCode;
772+
}; // make eslint v7 tests passing
764773
assert.deepEqual(
765-
utils.getTestInfo(scopeManager, ast),
774+
utils.getTestInfo(context, ast),
766775
[],
767776
'Expected no tests to be found'
768777
);
@@ -827,7 +836,16 @@ describe('utils', () => {
827836
sourceType: 'script',
828837
nodejsScope: true,
829838
});
830-
const testInfo = utils.getTestInfo(scopeManager, ast);
839+
const context = {
840+
sourceCode: {
841+
getDeclaredVariables:
842+
scopeManager.getDeclaredVariables.bind(scopeManager),
843+
},
844+
}; // mock object
845+
context.getSourceCode = function () {
846+
return context.sourceCode;
847+
}; // to make eslint v7 tests passing
848+
const testInfo = utils.getTestInfo(context, ast);
831849

832850
assert.strictEqual(
833851
testInfo.length,
@@ -1021,7 +1039,16 @@ describe('utils', () => {
10211039
sourceType: 'script',
10221040
nodejsScope: true,
10231041
});
1024-
const testInfo = utils.getTestInfo(scopeManager, ast);
1042+
const context = {
1043+
sourceCode: {
1044+
getDeclaredVariables:
1045+
scopeManager.getDeclaredVariables.bind(scopeManager),
1046+
},
1047+
}; // mock object
1048+
context.getSourceCode = function () {
1049+
return context.sourceCode;
1050+
}; // make eslint v7 tests passing
1051+
const testInfo = utils.getTestInfo(context, ast);
10251052

10261053
assert.strictEqual(
10271054
testInfo.length,

0 commit comments

Comments
 (0)