-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mocha 6): support all config formats (#1511)
Add support for all mocha's configuration formats: * Arguments specified on command-line * Configuration file (.mocharc.js, .mocharc.yml, etc.) 1. .mocharc.js 2. .mocharc.yaml 3. .mocharc.yml 4. .mocharc.jsonc 5. .mocharc.json * mocha property of package.json * mocha.opts This works by using mocha 6's [`loadOptions`](https://mochajs.org/api/module-lib_cli_options.html#.loadOptions) function if it is available. If not (mocha < 6), use old parsing logic. See https://mochajs.org/#configuring-mocha-nodejs
- Loading branch information
Showing
20 changed files
with
550 additions
and
146 deletions.
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
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.