From 72b542a668a5def76c499a6658517cb38e16ac84 Mon Sep 17 00:00:00 2001 From: Maarten Mulders Date: Fri, 6 Apr 2018 10:54:04 +0200 Subject: [PATCH] fix(jest-config) Fallback to default Jest config if it's not overriden (#42) --- src/JestConfigEditor.ts | 4 +- src/configLoaders/DefaultJestConfigLoader.ts | 6 +-- test/integration/JestConfigEditorSpec.ts | 13 +++++ .../configLoaders/DefaultConfigLoaderSpec.ts | 14 +++--- .../package.json | 6 +++ .../src/Add.js | 20 ++++++++ .../src/__tests__/AddSpec.js | 50 +++++++++++++++++++ 7 files changed, 99 insertions(+), 14 deletions(-) create mode 100644 testResources/exampleProjectWithDefaultJestConfig/package.json create mode 100644 testResources/exampleProjectWithDefaultJestConfig/src/Add.js create mode 100644 testResources/exampleProjectWithDefaultJestConfig/src/__tests__/AddSpec.js diff --git a/src/JestConfigEditor.ts b/src/JestConfigEditor.ts index 75b32a4..36dfbbd 100644 --- a/src/JestConfigEditor.ts +++ b/src/JestConfigEditor.ts @@ -13,10 +13,10 @@ export default class JestConfigEditor implements ConfigEditor { // If there is no Jest property on the Stryker config create it strykerConfig.jest = strykerConfig.jest || {}; - // When no project is set set it to 'default' + // When no project is set, set it to 'default' strykerConfig.jest.project = strykerConfig.jest.project || DEFAULT_PROJECT_NAME; - // When no config property is set load the configuration with the project type + // When no config property is set, load the configuration with the project type strykerConfig.jest.config = strykerConfig.jest.config || this.getConfigLoader(strykerConfig.jest.project).loadConfig(); // Override some of the config properties to optimise Jest for Stryker diff --git a/src/configLoaders/DefaultJestConfigLoader.ts b/src/configLoaders/DefaultJestConfigLoader.ts index a77edfb..243ec24 100644 --- a/src/configLoaders/DefaultJestConfigLoader.ts +++ b/src/configLoaders/DefaultJestConfigLoader.ts @@ -17,11 +17,7 @@ export default class DefaultJestConfigLoader implements JestConfigLoader { } public loadConfig(): JestConfiguration { - const jestConfig = this.readConfigFromJestConfigFile() || this.readConfigFromPackageJson(); - - if (!jestConfig) { - throw new Error('Could not read Jest configuration, please provide a jest.config.js file or a jest config in your package.json'); - } + const jestConfig = this.readConfigFromJestConfigFile() || this.readConfigFromPackageJson() || {}; return jestConfig; } diff --git a/test/integration/JestConfigEditorSpec.ts b/test/integration/JestConfigEditorSpec.ts index 8df5cd7..19524c2 100644 --- a/test/integration/JestConfigEditorSpec.ts +++ b/test/integration/JestConfigEditorSpec.ts @@ -109,6 +109,19 @@ describe('Integration JestConfigEditor', () => { }); }); + it('should load the default Jest configuration if there is no package.json config or jest.config.js', () => { + getProjectRootStub.returns(path.join(process.cwd(), 'testResources', 'exampleProjectWithDefaultJestConfig')); + + jestConfigEditor.edit(config); + + expect(config.jest.config).to.deep.equal({ + bail: false, + collectCoverage: false, + testResultsProcessor: undefined, + verbose: false, + }); + }); + it('should return with an error when an invalid project is specified', () => { const project = 'invalidProject'; config.set({ jest: { project } }); diff --git a/test/unit/configLoaders/DefaultConfigLoaderSpec.ts b/test/unit/configLoaders/DefaultConfigLoaderSpec.ts index 5ae0b4f..1f97207 100644 --- a/test/unit/configLoaders/DefaultConfigLoaderSpec.ts +++ b/test/unit/configLoaders/DefaultConfigLoaderSpec.ts @@ -38,13 +38,6 @@ describe('DefaultJestConfigLoader', () => { }); }); - it('should return an error when no Jest configuration is present in neither jest.config.js or package.json', () => { - requireStub.throws(Error('ENOENT: no such file or directory, open jest.config.js')); - fsStub.readFileSync.returns('{ "name": "dummy", "version": "0.0.1", "description": "Dummy package.json without jest property"}'); - - expect(() => defaultConfigLoader.loadConfig()).to.throw(Error, 'Could not read Jest configuration, please provide a jest.config.js file or a jest config in your package.json'); - }); - it('should fallback and load the Jest configuration from the package.json when jest.config.js is not present in the project', () => { requireStub.throws(Error('ENOENT: no such file or directory, open package.json')); const config = defaultConfigLoader.loadConfig(); @@ -54,6 +47,13 @@ describe('DefaultJestConfigLoader', () => { exampleProperty: 'examplePackageJsonValue' }); }); + + it('should load the default Jest configuration if there is no package.json config or jest.config.js', () => { + requireStub.throws(Error('ENOENT: no such file or directory, open package.json')); + fsStub.readFileSync.returns('{ }'); // note, no `jest` key here! + const config = defaultConfigLoader.loadConfig(); + expect(config).to.deep.equal({}); + }); }); interface FsStub { diff --git a/testResources/exampleProjectWithDefaultJestConfig/package.json b/testResources/exampleProjectWithDefaultJestConfig/package.json new file mode 100644 index 0000000..eaecbe6 --- /dev/null +++ b/testResources/exampleProjectWithDefaultJestConfig/package.json @@ -0,0 +1,6 @@ +{ + "private": true, + "name": "example-project", + "version": "0.0.0", + "description": "A testResource for jest-test-runner" +} diff --git a/testResources/exampleProjectWithDefaultJestConfig/src/Add.js b/testResources/exampleProjectWithDefaultJestConfig/src/Add.js new file mode 100644 index 0000000..58d562e --- /dev/null +++ b/testResources/exampleProjectWithDefaultJestConfig/src/Add.js @@ -0,0 +1,20 @@ +exports.add = function(num1, num2) { + return num1 + num2; +}; + +exports.addOne = function(number) { + number++; + return number; +}; + +exports.negate = function(number) { + return -number; +}; + +exports.isNegativeNumber = function(number) { + var isNegative = false; + if(number < 0){ + isNegative = true; + } + return isNegative; +}; \ No newline at end of file diff --git a/testResources/exampleProjectWithDefaultJestConfig/src/__tests__/AddSpec.js b/testResources/exampleProjectWithDefaultJestConfig/src/__tests__/AddSpec.js new file mode 100644 index 0000000..c31f82d --- /dev/null +++ b/testResources/exampleProjectWithDefaultJestConfig/src/__tests__/AddSpec.js @@ -0,0 +1,50 @@ +var add = require('../Add').add; +var addOne = require('../Add').addOne; +var isNegativeNumber = require('../Add').isNegativeNumber; +var negate = require('../Add').negate; + +describe('Add', function() { + it('should be able to add two numbers', function() { + var num1 = 2; + var num2 = 5; + var expected = num1 + num2; + + var actual = add(num1, num2); + + expect(actual).toBe(expected); + }); + + it('should be able to add one to a number', function() { + var number = 2; + var expected = 3; + + var actual = addOne(number); + + expect(actual).toBe(expected); + }); + + it('should be able negate a number', function() { + var number = 2; + var expected = -2; + + var actual = negate(number); + + expect(actual).toBe(expected); + }); + + it('should be able to recognize a negative number', function() { + var number = -2; + + var isNegative = isNegativeNumber(number); + + expect(isNegative).toBe(true); + }); + + it('should be able to recognize that 0 is not a negative number', function() { + var number = 0; + + var isNegative = isNegativeNumber(number); + + expect(isNegative).toBe(false); + }); +}); \ No newline at end of file