Skip to content

Commit

Permalink
feat(json config): support json-file config (#1853)
Browse files Browse the repository at this point in the history
featuring JSON file stryker config
It can be the first step to introduce schema for stryker (+ its nice feature I guess)
  • Loading branch information
Bartosz Leoniak authored and simondel committed Nov 14, 2019
1 parent a653514 commit 49495ef
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
11 changes: 8 additions & 3 deletions packages/core/src/config/ConfigReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,14 @@ export default class ConfigReader {
throw new StrykerError('Invalid config file', e);
}
}
if (typeof configModule !== 'function') {
this.log.fatal('Config file must export a function!\n' + CONFIG_SYNTAX_HELP);
throw new StrykerError('Config file must export a function!');
if (typeof configModule !== 'function' && typeof configModule !== 'object') {
this.log.fatal('Config file must be an object or export a function!\n' + CONFIG_SYNTAX_HELP);
throw new StrykerError('Config file must export a function or be a JSON!');
}
if (typeof configModule === 'object') {
return (config: any) => {
config.set(configModule);
};
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe(ConfigReader.name, () => {

it('should report a fatal error', () => {
expect(() => sut.readConfig()).throws();
expect(testInjector.logger.fatal).to.have.been.calledWith(`Config file must export a function!
expect(testInjector.logger.fatal).to.have.been.calledWith(`Config file must be an object or export a function!
module.exports = function(config) {
config.set({
// your config
Expand All @@ -125,5 +125,18 @@ describe(ConfigReader.name, () => {
expect(() => sut.readConfig()).throws('Invalid config file. Inner error: SyntaxError: Unexpected identifier');
});
});

describe('with json config file', () => {
it('should read config file', () => {
sut = createSut({ configFile: 'testResources/config-reader/valid.json' });

result = sut.readConfig();

expect(result.valid).to.be.eq('config');
expect(result.should).to.be.eq('be');
expect(result.read).to.be.eq(true);
expect(testInjector.logger.warn).not.called;
});
});
});
});
5 changes: 5 additions & 0 deletions packages/core/testResources/config-reader/valid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"valid": "config",
"should": "be",
"read": true
}

0 comments on commit 49495ef

Please sign in to comment.