-
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(plugin): add support for
declareValuePlugin
(#4490)
Add support for `declareValuePlugin`. With it, you can define a plugin as a value instead of using a factory method or class. ```js export const strykerPlugins = [declareValuePlugin(PluginKind.Ignorer, { shouldIgnore(path) { // tada } }); ```
- Loading branch information
Showing
17 changed files
with
215 additions
and
44 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
40 changes: 19 additions & 21 deletions
40
e2e/test/ignore-project/stryker-plugins/ignorers/console-ignorer.js
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,23 +1,21 @@ | ||
// @ts-check | ||
import { PluginKind, declareClassPlugin } from '@stryker-mutator/api/plugin'; | ||
import { PluginKind, declareValuePlugin } from '@stryker-mutator/api/plugin'; | ||
|
||
export class ConsoleIgnorer { | ||
/** | ||
* @param {import('@stryker-mutator/api/ignorer').NodePath} path | ||
*/ | ||
shouldIgnore(path) { | ||
if ( | ||
path.isExpressionStatement() && | ||
path.node.expression.type === 'CallExpression' && | ||
path.node.expression.callee.type === 'MemberExpression' && | ||
path.node.expression.callee.object.type === 'Identifier' && | ||
path.node.expression.callee.object.name === 'console' && | ||
path.node.expression.callee.property.type === 'Identifier' && | ||
path.node.expression.callee.property.name === 'log' | ||
) { | ||
return "We're not interested in console.log statements for now"; | ||
} | ||
return undefined; | ||
} | ||
} | ||
export const strykerPlugins = [declareClassPlugin(PluginKind.Ignorer, 'ConsoleIgnorer', ConsoleIgnorer)]; | ||
export const strykerPlugins = [ | ||
declareValuePlugin(PluginKind.Ignorer, 'ConsoleIgnorer', { | ||
shouldIgnore(path) { | ||
if ( | ||
path.isExpressionStatement() && | ||
path.node.expression.type === 'CallExpression' && | ||
path.node.expression.callee.type === 'MemberExpression' && | ||
path.node.expression.callee.object.type === 'Identifier' && | ||
path.node.expression.callee.object.name === 'console' && | ||
path.node.expression.callee.property.type === 'Identifier' && | ||
path.node.expression.callee.property.name === 'log' | ||
) { | ||
return "We're not interested in console.log statements for now"; | ||
} | ||
return undefined; | ||
}, | ||
}), | ||
]; |
5 changes: 3 additions & 2 deletions
5
e2e/test/ignore-project/stryker-plugins/ignorers/global.types.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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
/// <reference types="@stryker-mutator/api/ignore" /> | ||
import type babel from '@babel/core'; | ||
|
||
declare module '@stryker-mutator/api/ignorer' { | ||
declare module '@stryker-mutator/api/ignore' { | ||
// eslint-disable-next-line @typescript-eslint/no-empty-interface | ||
interface NodePath extends babel.NodePath {} | ||
export interface NodePath extends babel.NodePath {} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { testInjector } from '@stryker-mutator/test-helpers'; | ||
import { PluginKind } from '@stryker-mutator/api/plugin'; | ||
|
||
import { expect } from 'chai'; | ||
|
||
import { PluginLoader } from '../../../src/di/plugin-loader.js'; | ||
import { PluginCreator } from '../../../src/di/plugin-creator.js'; | ||
import { coreTokens } from '../../../src/di/index.js'; | ||
|
||
describe('Plugins integration', () => { | ||
describe('local plugins', () => { | ||
let pluginCreator: PluginCreator; | ||
|
||
beforeEach(async () => { | ||
const loader = testInjector.injector.injectClass(PluginLoader); | ||
const plugins = await loader.load(['./testResources/plugins/custom-plugins.js']); | ||
pluginCreator = testInjector.injector.provideValue(coreTokens.pluginsByKind, plugins.pluginsByKind).injectClass(PluginCreator); | ||
}); | ||
|
||
it('should be able to load a "ValuePlugin"', async () => { | ||
const plugin = pluginCreator.create(PluginKind.Ignorer, 'console.debug'); | ||
expect(plugin).ok; | ||
expect(plugin.shouldIgnore).a('function'); | ||
}); | ||
|
||
it('should be able to load a "FactoryPlugin"', async () => { | ||
const plugin = pluginCreator.create(PluginKind.TestRunner, 'lazy'); | ||
expect(plugin).ok; | ||
expect(plugin.capabilities).a('function'); | ||
}); | ||
|
||
it('should be able to load a "ClassPlugin"', async () => { | ||
const plugin = pluginCreator.create(PluginKind.Reporter, 'console'); | ||
expect(plugin).ok; | ||
expect(plugin.onMutationTestReportReady).a('function'); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// @ts-check | ||
import { PluginKind, commonTokens, declareClassPlugin, declareFactoryPlugin, declareValuePlugin } from '@stryker-mutator/api/plugin'; | ||
import { DryRunStatus, MutantRunStatus } from '@stryker-mutator/api/test-runner'; | ||
|
||
/** | ||
* @typedef {import('@stryker-mutator/api/test-runner').TestRunner} TestRunner | ||
* @typedef {import('@stryker-mutator/api/plugin').Injector} Injector | ||
*/ | ||
|
||
class MyReporter { | ||
static inject = [commonTokens.logger] /** @type {const} */; | ||
|
||
/** @param {import('@stryker-mutator/api/logging').Logger} logger */ | ||
constructor(logger) { | ||
this.logger = logger; | ||
} | ||
|
||
/** @param {Readonly<import('@stryker-mutator/api/core').schema.MutationTestResult>} result */ | ||
onMutationTestReportReady(result) { | ||
this.logger.info(`${result.files}`); | ||
} | ||
} | ||
|
||
/** | ||
* @param {Injector} _injector | ||
* @returns {TestRunner} | ||
*/1 | ||
function createLazyTestRunner(_injector) { | ||
return { | ||
capabilities() { | ||
return { reloadEnvironment: false }; | ||
}, | ||
|
||
async dryRun() { | ||
return { | ||
status: DryRunStatus.Complete, | ||
tests: [], | ||
} | ||
}, | ||
async mutantRun() { | ||
return { | ||
status: MutantRunStatus.Error, | ||
errorMessage: 'Not implemented', | ||
} | ||
} | ||
}; | ||
} | ||
createLazyTestRunner.inject = [commonTokens.injector]; | ||
|
||
export const strykerPlugins = [ | ||
declareClassPlugin(PluginKind.Reporter, 'console', MyReporter), | ||
declareFactoryPlugin(PluginKind.TestRunner, 'lazy', createLazyTestRunner), | ||
declareValuePlugin(PluginKind.Ignorer, 'console.debug', { | ||
shouldIgnore(path) { | ||
if ( | ||
path.isExpresssionStatement() && | ||
path.node.expression.type === 'CallExpression' && | ||
path.node.expression.callee.type === 'MemberExpression' && | ||
path.node.expression.callee.object.type === 'Identifier' && | ||
path.node.expression.callee.object.name === 'console' && | ||
path.node.expression.callee.property.type === 'Identifier' && | ||
path.node.expression.callee.property.name === 'debug' | ||
) { | ||
return 'ignoring console.debug'; | ||
} | ||
}, | ||
}), | ||
]; |
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
2 changes: 1 addition & 1 deletion
2
packages/instrumenter/test/unit/transformers/ignorer-bookkeeper.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