From 7878f10f6bc1bc897b98106ce56e0dac57e79a10 Mon Sep 17 00:00:00 2001 From: Jeff Bartolotta Date: Fri, 10 Jul 2020 11:25:16 -0700 Subject: [PATCH 1/2] Add more defaults to DEFAULT_ENV_VARS added jasmine, jest, jquery, and mocha to reduce scanning noise. --- src/lib/eslint/BaseEslintEngine.ts | 12 ++++++---- .../projects/js/src/baseConfigEnv.js | 23 ++++++++++++++++++- .../projects/js/src/fileThatUsesJasmine.js | 14 ----------- .../projects/js/src/fileThatUsesQUnit.js | 7 ++++++ test/commands/scanner/run.test.ts | 15 ++++++------ 5 files changed, 45 insertions(+), 26 deletions(-) delete mode 100644 test/code-fixtures/projects/js/src/fileThatUsesJasmine.js create mode 100644 test/code-fixtures/projects/js/src/fileThatUsesQUnit.js diff --git a/src/lib/eslint/BaseEslintEngine.ts b/src/lib/eslint/BaseEslintEngine.ts index a41fb9aa3..d8bcd2505 100644 --- a/src/lib/eslint/BaseEslintEngine.ts +++ b/src/lib/eslint/BaseEslintEngine.ts @@ -10,10 +10,14 @@ import * as path from 'path'; // requires deleting DEFAULT_ENV_VARS, so be it. // These are the environment variables that we'll want enabled by default in our ESLint baseConfig. const DEFAULT_ENV_VARS: LooseObject = { - 'es6': true, // `Map` class and others - 'node': true, // `process` global var and others - 'browser': true, // `document` global var - 'webextensions': true // Chrome + es6: true, // `Map` class and others + node: true, // `process` global var and others + browser: true, // `document` global var + webextensions: true, // Chrome + jasmine: true, // `describe', 'expect', 'it' global vars + jest: true, // 'jest' global var + jquery: true, // '$' global var + mocha: true // `describe', 'it' global var }; const ENV = 'env'; diff --git a/test/code-fixtures/projects/js/src/baseConfigEnv.js b/test/code-fixtures/projects/js/src/baseConfigEnv.js index 5e7d11d86..b71436034 100644 --- a/test/code-fixtures/projects/js/src/baseConfigEnv.js +++ b/test/code-fixtures/projects/js/src/baseConfigEnv.js @@ -5,4 +5,25 @@ var x = new Map(); // ex6 if (x) { console.log(document.body); // browser } -console.log(chrome.tabs); // webextensions \ No newline at end of file +console.log(chrome.tabs); // webextensions + +// 'describe', 'it', and 'expect' are jasmine keywords +// 'describe' and 'it' are mocha keywords +describe('In the Age of Ancients, the world was unformed and shrouded by fog.', () => { + it('A land of grey crags, arch trees, and everlasting dragons.', () => { + expect(true).toBe(true, 'Then there was Fire, and with Fire came disparity'); + }); + }); + +describe('Heat and cold; life and death; and of course, light and dark', () => { + it('And from the dark they came, and found the Souls of Lords within the flame', () => { + const lordSouls = ['Gravelord Nito, First of the Dead', 'The Witch of Izalith and her Daughters of Chaos', + 'Gwynn, Lord of Sunlight and his faithful knights', 'The Furtive Pygmy, so easily forgotten' + ]; + expect(lordSouls.length).toBe(4, 'And with the strength of lords, they challenged the dragons'); + }); +}); + +$('.cssClass').show(); // JQuery + +jest.useFakeTimers(); // Jest diff --git a/test/code-fixtures/projects/js/src/fileThatUsesJasmine.js b/test/code-fixtures/projects/js/src/fileThatUsesJasmine.js deleted file mode 100644 index ba410ad08..000000000 --- a/test/code-fixtures/projects/js/src/fileThatUsesJasmine.js +++ /dev/null @@ -1,14 +0,0 @@ -describe('In the Age of Ancients, the world was unformed and shrouded by fog.', () => { - it('A land of grey crags, arch trees, and everlasting dragons.', () => { - expect(true).toBe(true, 'Then there was Fire, and with Fire came disparity'); - }); -}); - -describe('Heat and cold; life and death; and of course, light and dark', () => { - it('And from the dark they came, and found the Souls of Lords within the flame', () => { - const lordSouls = ['Gravelord Nito, First of the Dead', 'The Witch of Izalith and her Daughters of Chaos', - 'Gwynn, Lord of Sunlight and his faithful knights', 'The Furtive Pygmy, so easily forgotten' - ]; - expect(lordSouls.length).toBe(4, 'And with the strength of lords, they challenged the dragons'); - }); -}); diff --git a/test/code-fixtures/projects/js/src/fileThatUsesQUnit.js b/test/code-fixtures/projects/js/src/fileThatUsesQUnit.js new file mode 100644 index 000000000..66de121fc --- /dev/null +++ b/test/code-fixtures/projects/js/src/fileThatUsesQUnit.js @@ -0,0 +1,7 @@ +// Example from https://qunitjs.com/ +const add = (a, b) => a + b; +QUnit.module('add', function() { + QUnit.test('should add two numbers', function(assert) { + assert.equal(add(1, 1), 2, '1 + 1 = 2'); + }); +}); diff --git a/test/commands/scanner/run.test.ts b/test/commands/scanner/run.test.ts index d984ae7d3..6f002400b 100644 --- a/test/commands/scanner/run.test.ts +++ b/test/commands/scanner/run.test.ts @@ -826,16 +826,17 @@ describe('scanner:run', function () { .stdout() .stderr() .command(['scanner:run', - '--target', path.join('test', 'code-fixtures', 'projects', 'js', 'src', 'fileThatUsesJasmine.js'), + '--target', path.join('test', 'code-fixtures', 'projects', 'js', 'src', 'fileThatUsesQUnit.js'), '--format', 'json' ]) - .it('By default, frameworks such as Jasmine are not included in the baseConfig', ctx => { - // We expect there to be 8 errors about jasmine-related syntax being undefined. + .it('By default, frameworks such as QUnit are not included in the baseConfig', ctx => { + // We expect there to be 2 errors about qunit-related syntax being undefined. // There's currently some weird issue with the test framework that causes this specific execution to act // like the --verbose flag was supplied. So we'll just pull out a JSON by getting everything from the first // instance of '[' to the last instance of ']', since the JSON takes the form of an array. const parsedCtx = JSON.parse(ctx.stdout.slice(ctx.stdout.indexOf('['), ctx.stdout.lastIndexOf(']') + 1)); - expect(parsedCtx[0].violations.length).to.equal(6, 'Should be 6 violations'); + expect(parsedCtx[0].violations.length).to.equal(2, `Should be 2 violations ${JSON.stringify(parsedCtx[0].violations)}`); + expect(parsedCtx[0].violations[0].message).to.contain("'QUnit' is not defined."); }); // TODO: THIS TEST WAS IMPLEMENTED FOR W-7791882. THE FIX FOR THAT BUG WAS SUB-OPTIMAL AND WE NEED TO REDO IT IN 3.0. @@ -845,11 +846,11 @@ describe('scanner:run', function () { .stderr() .command(['scanner:run', - '--target', path.join('test', 'code-fixtures', 'projects', 'js', 'src', 'fileThatUsesJasmine.js'), + '--target', path.join('test', 'code-fixtures', 'projects', 'js', 'src', 'fileThatUsesQUnit.js'), '--format', 'json', - '--env', '{"jasmine": true}' + '--env', '{"qunit": true}' ]) - .it('Providing jasmine in the --env override should resolve errors about that framework', ctx => { + .it('Providing qunit in the --env override should resolve errors about that framework', ctx => { expect(ctx.stdout).to.contain('No rule violations found.', 'Should be no violations found in the file.'); }); }); From 68172173bd4f42ff2d66364035e9c8fcb4912680 Mon Sep 17 00:00:00 2001 From: Jeff Bartolotta Date: Fri, 10 Jul 2020 11:30:31 -0700 Subject: [PATCH 2/2] Minor comment tweak --- src/lib/eslint/BaseEslintEngine.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/eslint/BaseEslintEngine.ts b/src/lib/eslint/BaseEslintEngine.ts index d8bcd2505..fab78f7ce 100644 --- a/src/lib/eslint/BaseEslintEngine.ts +++ b/src/lib/eslint/BaseEslintEngine.ts @@ -17,7 +17,7 @@ const DEFAULT_ENV_VARS: LooseObject = { jasmine: true, // `describe', 'expect', 'it' global vars jest: true, // 'jest' global var jquery: true, // '$' global var - mocha: true // `describe', 'it' global var + mocha: true // `describe' and 'it' global vars }; const ENV = 'env';