From 0149230fbbab8050693f062da150f4f1b7847f23 Mon Sep 17 00:00:00 2001 From: Kevin Schmidt Date: Mon, 3 Feb 2020 11:24:56 -0700 Subject: [PATCH] fix(gcc): avoid using singleton test options instance This was interfering with tests because the expected options and resolved options were the same instance. --- plugins/gcc/options-test.js | 4 +- plugins/gcc/test-writer.js | 4 +- test/plugins/gcc/test-writer.test.js | 58 +++++++++++++++++++++------- 3 files changed, 48 insertions(+), 18 deletions(-) diff --git a/plugins/gcc/options-test.js b/plugins/gcc/options-test.js index 3c5e601..9e0f485 100644 --- a/plugins/gcc/options-test.js +++ b/plugins/gcc/options-test.js @@ -1,9 +1,9 @@ 'use strict'; /* eslint camelcase: "off" */ -module.exports = { +module.exports = () => ({ compilation_level: 'SIMPLE', dependency_mode: 'PRUNE_LEGACY', summary_detail_level: 3, jscomp_off: '*' -}; +}); diff --git a/plugins/gcc/test-writer.js b/plugins/gcc/test-writer.js index 5ee08ac..8cb84f5 100644 --- a/plugins/gcc/test-writer.js +++ b/plugins/gcc/test-writer.js @@ -18,7 +18,7 @@ var mocks = {}; const resolver = function(pack, projectDir) { if (pack.build) { - var testDir = getTestDir(); + var testDir = getTestDir(pack); mocks[pack.name] = path.resolve(projectDir, testDir, '**.mock.js'); } @@ -28,7 +28,7 @@ const resolver = function(pack, projectDir) { const _getOptions = function(pack, dir, options) { // the compiler options are not defined in camelcase /* eslint camelcase: "off" */ - var opts = require('./options-test'); + var opts = require('./options-test')(); opts.js = options.js ? options.js.slice() : []; opts.output_manifest = path.join(dir, 'gcc-test-manifest'); opts.js_output_file = path.join(dir, pack.name + '-test.min.js'); diff --git a/test/plugins/gcc/test-writer.test.js b/test/plugins/gcc/test-writer.test.js index d7c6ba5..1e990ae 100644 --- a/test/plugins/gcc/test-writer.test.js +++ b/test/plugins/gcc/test-writer.test.js @@ -8,6 +8,7 @@ const rimraf = require('rimraf'); const path = require('path'); describe('gcc test writer', function() { + var projectDir = process.cwd(); var outputDir = path.join(process.cwd(), '.test'); var file = path.join(outputDir, 'gcc-test-args'); @@ -16,51 +17,80 @@ describe('gcc test writer', function() { rimraf.sync(file); }); + var hideWarningsFor = [ + '/a-thing/', + '/another-thing/' + ]; + var pack = { name: 'thing' }; var getExpected = () => { - var expected = require('../../../plugins/gcc/options-test'); - expected.output_manifest = 'gcc-test-manifest'; + var expected = require('../../../plugins/gcc/options-test')(); + expected.js = []; + expected.output_manifest = mapOutputDir('gcc-test-manifest'); + expected.js_output_file = mapOutputDir(pack.name + '-test.min.js'); + expected.hide_warnings_for = []; return expected; }; + var mapProjectDir = (dir) => path.join(projectDir, dir); + + var mapOutputDir = (dir) => path.join(outputDir, dir); it('should handle empty options', () => { var expected = getExpected(); - expected.js = ['test/**.js']; expect(test._getOptions(pack, outputDir, {})).to.deep.equal(expected); }); it('should handle undefined test directories', () => { var p = Object.assign(pack, {directories: {}}); var expected = getExpected(); - expected.js = ['test/**.js']; expect(test._getOptions(p, outputDir, {})).to.deep.equal(expected); }); it('should handle explicit test directories', () => { - var p = Object.assign(pack, {directories: {test: 'foo'}}); + var p = Object.assign(pack, { + directories: { + test: 'foo' + }, + build: {} + }); + var expected = getExpected(); - expected.js = ['foo/**.js']; - expect(test._getOptions(p, outputDir, {})).to.deep.equal(expected); + expected.js = ['foo/**.mock.js', 'foo/**.test.js'].map(mapProjectDir); + + return test.resolver(p, '').then(() => { + expect(test._getOptions(p, outputDir, {})).to.deep.equal(expected); + }); }); it('should append test js to source js', () => { var expected = getExpected(); - expected.js = ['original.js', 'test/**.js']; + expected.js = ['original.js']; expect(test._getOptions(pack, outputDir, { - js: ['origina.js'] + js: ['original.js'] })).to.deep.equal(expected); }); + it('should add hide_warnings_for to options', () => { + var expected = getExpected(); + expected.hide_warnings_for = hideWarningsFor; + var result = test._getOptions(pack, outputDir, { + hide_warnings_for: hideWarningsFor + }); + expect(result).to.deep.equal(expected); + }); + it('should resolve mocks for project dependencies', () => { - var p = Object.assign(pack, {build: {}}); + var p = Object.assign(pack, { + build: {}, + directories: {} + }); var p2 = { name: 'dependency', - build: { - } + build: {} }; return test.resolver(p, '') @@ -69,7 +99,7 @@ describe('gcc test writer', function() { }) .then(() => { var expected = getExpected(); - expected.js = ['test/**.js', 'test/**.mock.js', '../dependency/test/**mock.js']; + expected.js = ['test/**.mock.js', 'test/**.test.js', '../dependency/test/**.mock.js'].map(mapProjectDir); expect(test._getOptions(p, outputDir, {})).to.deep.equal(expected); }); }); @@ -86,7 +116,7 @@ describe('gcc test writer', function() { }) .then(() => { var expected = getExpected(); - expected.js = ['test/**.js'], + expected.js = ['test/**.mock.js', 'test/**.test.js'].map(mapProjectDir), expect(test._getOptions(p, outputDir, {})).to.deep.equal(expected); }); });