Skip to content

Commit

Permalink
fix(gcc): avoid using singleton test options instance
Browse files Browse the repository at this point in the history
This was interfering with tests because the expected options and resolved
options were the same instance.
  • Loading branch information
Kevin Schmidt committed Feb 3, 2020
1 parent 42197d9 commit 0149230
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
4 changes: 2 additions & 2 deletions plugins/gcc/options-test.js
Original file line number Diff line number Diff line change
@@ -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: '*'
};
});
4 changes: 2 additions & 2 deletions plugins/gcc/test-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand All @@ -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');
Expand Down
58 changes: 44 additions & 14 deletions test/plugins/gcc/test-writer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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, '')
Expand All @@ -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);
});
});
Expand All @@ -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);
});
});
Expand Down

0 comments on commit 0149230

Please sign in to comment.