-
Notifications
You must be signed in to change notification settings - Fork 250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(mocha 6): support all config formats #1511
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,6 @@ | |
}, | ||
"peerDependencies": { | ||
"@stryker-mutator/core": "^1.0.0", | ||
"mocha": ">= 2.3.3 < 6" | ||
"mocha": ">= 2.3.3 < 7" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,25 @@ | ||
import * as Mocha from 'mocha'; | ||
import * as multimatch from 'multimatch'; | ||
|
||
let loadOptions: undefined | ((argv?: string[] | string) => MochaOptions | undefined); | ||
|
||
try { | ||
/* | ||
* If read, object containing parsed arguments | ||
* @since 6.0.0' | ||
* @see https://mochajs.org/api/module-lib_cli_options.html#.loadOptions | ||
*/ | ||
loadOptions = require('mocha/lib/cli/options').loadOptions; | ||
} catch { | ||
// Mocha < 6 doesn't support `loadOptions` | ||
} | ||
|
||
/** | ||
* Wraps Mocha class and require for testability | ||
*/ | ||
export default class LibWrapper { | ||
public static Mocha = Mocha; | ||
public static require = require; | ||
public static multimatch = multimatch; | ||
public static loadOptions = loadOptions; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import { StrykerOptions } from '@stryker-mutator/api/core'; | ||
import MochaRunnerOptions, { mochaOptionsKey } from './MochaRunnerOptions'; | ||
import { tokens, commonTokens } from '@stryker-mutator/api/plugin'; | ||
import { Logger } from '@stryker-mutator/api/logging'; | ||
import { serializeArguments, filterConfig, mochaOptionsKey } from './utils'; | ||
import LibWrapper from './LibWrapper'; | ||
|
||
export default class MochaOptionsLoader { | ||
|
||
|
@@ -12,12 +13,27 @@ export default class MochaOptionsLoader { | |
public static inject = tokens(commonTokens.logger); | ||
constructor(private readonly log: Logger) { } | ||
|
||
public load(config: StrykerOptions): MochaRunnerOptions { | ||
const mochaOptions = Object.assign({}, config[mochaOptionsKey]) as MochaRunnerOptions; | ||
return Object.assign(this.loadMochaOptsFile(mochaOptions.opts), mochaOptions); | ||
public load(strykerOptions: StrykerOptions): MochaOptions { | ||
const mochaOptions = Object.assign({}, strykerOptions[mochaOptionsKey]) as MochaOptions; | ||
return Object.assign(this.loadMochaOptions(mochaOptions), mochaOptions); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use a spread operator for this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
} | ||
|
||
private loadMochaOptsFile(opts: false | string | undefined): MochaRunnerOptions { | ||
private loadMochaOptions(overrides: MochaOptions) { | ||
if (LibWrapper.loadOptions) { | ||
this.log.debug('Mocha > 6 detected. Using mocha\'s `%s` to load mocha options', LibWrapper.loadOptions.name); | ||
const args = serializeArguments(overrides); | ||
const rawConfig = LibWrapper.loadOptions(args) || {}; | ||
if (this.log.isTraceEnabled()) { | ||
this.log.trace(`Mocha: ${LibWrapper.loadOptions.name}([${args.map(arg => `'${arg}'`).join(',')}]) => ${JSON.stringify(rawConfig)}`); | ||
} | ||
return filterConfig(rawConfig); | ||
} else { | ||
this.log.debug('Mocha < 6 detected. Using custom logic to parse mocha options'); | ||
return this.loadMochaOptsFile(overrides.opts); | ||
} | ||
} | ||
|
||
private loadMochaOptsFile(opts: false | string | undefined): MochaOptions { | ||
switch (typeof opts) { | ||
case 'boolean': | ||
this.log.debug('Not reading additional mochaOpts from a file'); | ||
|
@@ -46,9 +62,9 @@ export default class MochaOptionsLoader { | |
return this.parseOptsFile(fs.readFileSync(optsFileName, 'utf8')); | ||
} | ||
|
||
private parseOptsFile(optsFileContent: string): MochaRunnerOptions { | ||
private parseOptsFile(optsFileContent: string): MochaOptions { | ||
const options = optsFileContent.split('\n').map(val => val.trim()); | ||
const mochaRunnerOptions: MochaRunnerOptions = Object.create(null); | ||
const mochaRunnerOptions: MochaOptions = Object.create(null); | ||
options.forEach(option => { | ||
const args = option.split(' ').filter(Boolean); | ||
if (args[0]) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
packages/mocha-runner/test/integration/MochaOptionsLoader.it.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import * as path from 'path'; | ||
import { testInjector } from '@stryker-mutator/test-helpers'; | ||
import MochaOptionsLoader from '../../src/MochaOptionsLoader'; | ||
import { expect } from 'chai'; | ||
import { mochaOptionsKey } from '../../src/utils'; | ||
|
||
describe(`${MochaOptionsLoader.name} integration`, () => { | ||
let sut: MochaOptionsLoader; | ||
const cwd = process.cwd(); | ||
|
||
beforeEach(() => { | ||
sut = createSut(); | ||
}); | ||
|
||
afterEach(() => { | ||
process.chdir(cwd); | ||
}); | ||
|
||
it('should support loading from ".mocharc.js"', () => { | ||
const configFile = resolveMochaConfig('.mocharc.js'); | ||
const actualConfig = actLoad({ config: configFile }); | ||
expect(actualConfig).deep.eq({ | ||
config: configFile, | ||
extension: ['js'], | ||
timeout: 2000, | ||
ui: 'bdd' | ||
}); | ||
}); | ||
|
||
it('should support loading from ".mocharc.json"', () => { | ||
const configFile = resolveMochaConfig('.mocharc.json'); | ||
const actualConfig = actLoad({ config: configFile }); | ||
expect(actualConfig).deep.eq({ | ||
config: configFile, | ||
extension: ['json', 'js'], | ||
timeout: 2000, | ||
ui: 'bdd' | ||
}); | ||
}); | ||
|
||
it('should support loading from ".mocharc.jsonc"', () => { | ||
const configFile = resolveMochaConfig('.mocharc.jsonc'); | ||
const actualConfig = actLoad({ config: configFile }); | ||
expect(actualConfig).deep.eq({ | ||
config: configFile, | ||
extension: ['jsonc', 'js'], | ||
timeout: 2000, | ||
ui: 'bdd' | ||
}); | ||
}); | ||
|
||
it('should support loading from ".mocharc.yml"', () => { | ||
const configFile = resolveMochaConfig('.mocharc.yml'); | ||
const actualConfig = actLoad({ config: configFile }); | ||
expect(actualConfig).deep.eq({ | ||
['async-only']: false, | ||
config: configFile, | ||
exclude: [ | ||
'/path/to/some/excluded/file' | ||
], | ||
extension: [ | ||
'yml', | ||
'js' | ||
], | ||
file: [ | ||
'/path/to/some/file', | ||
'/path/to/some/other/file' | ||
], | ||
require: [ | ||
'@babel/register' | ||
], | ||
timeout: 0, | ||
ui: 'bdd' | ||
}); | ||
}); | ||
|
||
it('should support loading from "package.json"', () => { | ||
const pkgFile = resolveMochaConfig('package.json'); | ||
const actualConfig = actLoad({ package: pkgFile }); | ||
expect(actualConfig).deep.eq({ | ||
['async-only']: true, | ||
extension: ['json', 'js'], | ||
package: pkgFile, | ||
timeout: 20, | ||
ui: 'tdd' | ||
}); | ||
}); | ||
|
||
it('should respect mocha default file order', () => { | ||
process.chdir(resolveMochaConfig('.')); | ||
const actualConfig = actLoad({}); | ||
expect(actualConfig).deep.eq({ | ||
['async-only']: true, | ||
extension: [ | ||
'js', | ||
'json' | ||
], | ||
timeout: 2000, | ||
ui: 'bdd' | ||
}); | ||
}); | ||
|
||
it('should support `no-config`, `no-opts` and `no-package` keys', () => { | ||
process.chdir(resolveMochaConfig('.')); | ||
const actualConfig = actLoad({ | ||
['no-config']: true, | ||
['no-package']: true, | ||
['no-opts']: true | ||
}); | ||
const expectedOptions = { | ||
extension: ['js'], | ||
['no-config']: true, | ||
['no-opts']: true, | ||
['no-package']: true, | ||
timeout: 2000, | ||
ui: 'bdd' | ||
}; | ||
expect(actualConfig).deep.eq(expectedOptions); | ||
}); | ||
|
||
function resolveMochaConfig(relativeName: string) { | ||
return path.resolve(__dirname, '..', '..', 'testResources', 'mocha-config', relativeName); | ||
} | ||
|
||
function actLoad(mochaConfig: { [key: string]: any }): MochaOptions { | ||
testInjector.options[mochaOptionsKey] = mochaConfig; | ||
return sut.load(testInjector.options); | ||
} | ||
|
||
function createSut() { | ||
return testInjector.injector.injectClass(MochaOptionsLoader); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use a spread operator for this
{ ...strykerOptions[mochaOptionsKey] }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done