Skip to content
This repository has been archived by the owner on Feb 19, 2021. It is now read-only.

Commit

Permalink
fix(jest-config) Fallback to default Jest config if it's not overriden (
Browse files Browse the repository at this point in the history
  • Loading branch information
mthmulders authored and nicojs committed Apr 6, 2018
1 parent 636b1fe commit 72b542a
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/JestConfigEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions src/configLoaders/DefaultJestConfigLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
13 changes: 13 additions & 0 deletions test/integration/JestConfigEditorSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } });
Expand Down
14 changes: 7 additions & 7 deletions test/unit/configLoaders/DefaultConfigLoaderSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"name": "example-project",
"version": "0.0.0",
"description": "A testResource for jest-test-runner"
}
20 changes: 20 additions & 0 deletions testResources/exampleProjectWithDefaultJestConfig/src/Add.js
Original file line number Diff line number Diff line change
@@ -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;
};
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 72b542a

Please sign in to comment.