Skip to content

Commit

Permalink
feat(transpilers): Remove side effects transpiler plugins (#1351)
Browse files Browse the repository at this point in the history
Remove side effects from all transpiler plugins.

* stryker-webpack-transpiler
* stryker-typescript
* stryker-babel-transpiler
  • Loading branch information
nicojs authored Feb 5, 2019
1 parent a7160f4 commit 9a8b539
Show file tree
Hide file tree
Showing 86 changed files with 1,081 additions and 785 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ reports
.tscache
.stryker-tmp
*.map
# Ignore heap dumps
packages/report.*.json

e2e/module/*.js
e2e/module/*.map
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"sinon-chai": "^3.2.0",
"source-map-support": "^0.5.6",
"tslint": "~5.12.0",
"typescript": "3.2.2",
"typescript": "^3.3.1",
"web-component-tester": "6.9.2"
},
"prettier": {
Expand Down
2 changes: 1 addition & 1 deletion packages/stryker-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@
},
"devDependencies": {
"surrial": "~0.1.1",
"typed-inject": "^0.1.1"
"typed-inject": "^0.2.0"
}
}
10 changes: 5 additions & 5 deletions packages/stryker-babel-transpiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@
"license": "Apache-2.0",
"dependencies": {
"babel-core": "6.26.3",
"minimatch": "~3.0.4"
"minimatch": "~3.0.4",
"stryker-api": "~0.23.0"
},
"devDependencies": {
"@stryker-mutator/test-helpers": "0.0.0",
"@types/babel-core": "~6.25.3",
"@types/glob": "~7.1.0",
"@types/minimatch": "~3.0.3",
"babel-cli": "~6.26.0",
"babel-plugin-transform-es2015-spread": "~6.22.0",
"babel-preset-es2015": "~6.24.1",
"glob": "~7.1.2",
"stryker-api": "^0.23.0"
"glob": "~7.1.2"
},
"peerDependencies": {
"babel-core": "^6.26.0 || ^7.0.0-bridge.0",
"stryker-api": ">=0.18.0 <0.24.0"
"babel-core": "^6.26.0 || ^7.0.0-bridge.0"
},
"initStrykerConfig": {
"babelrcFile": ".babelrc",
Expand Down
12 changes: 6 additions & 6 deletions packages/stryker-babel-transpiler/src/BabelConfigReader.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import * as fs from 'fs';
import * as path from 'path';
import { Config } from 'stryker-api/config';
import { CONFIG_KEY_FILE, CONFIG_KEY_OPTIONS } from './helpers/keys';
import { getLogger } from 'stryker-api/logging';
import { StrykerOptions } from 'stryker-api/core';

export default class BabelConfigReader {
private readonly log = getLogger(BabelConfigReader.name);

public readConfig(config: Config): babel.TransformOptions {
const babelConfig: babel.TransformOptions = config[CONFIG_KEY_OPTIONS] || this.getConfigFile(config) || {};
public readConfig(options: StrykerOptions): babel.TransformOptions {
const babelConfig: babel.TransformOptions = options[CONFIG_KEY_OPTIONS] || this.getConfigFile(options) || {};
this.log.debug(`babel config is: ${JSON.stringify(babelConfig, null, 2)}`);
return babelConfig;
}

private getConfigFile(config: Config): babel.TransformOptions | null {
if (typeof config[CONFIG_KEY_FILE] === 'string') {
const babelrcPath = path.resolve(config[CONFIG_KEY_FILE]);
private getConfigFile(options: StrykerOptions): babel.TransformOptions | null {
if (typeof options[CONFIG_KEY_FILE] === 'string') {
const babelrcPath = path.resolve(options[CONFIG_KEY_FILE]);
this.log.info(`Reading .babelrc file from path "${babelrcPath}"`);
if (fs.existsSync(babelrcPath)) {
try {
Expand Down
18 changes: 10 additions & 8 deletions packages/stryker-babel-transpiler/src/BabelTranspiler.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Transpiler, TranspilerOptions } from 'stryker-api/transpile';
import { File } from 'stryker-api/core';
import { Transpiler } from 'stryker-api/transpile';
import { File, StrykerOptions } from 'stryker-api/core';
import * as babel from 'babel-core';
import * as path from 'path';
import BabelConfigReader from './BabelConfigReader';
import { CONFIG_KEY_FILE } from './helpers/keys';
import { toJSFileName } from './helpers/helpers';
import { tokens, commonTokens } from 'stryker-api/plugin';

const KNOWN_EXTENSIONS = Object.freeze([
'.es6',
Expand All @@ -19,11 +20,12 @@ class BabelTranspiler implements Transpiler {
private readonly babelOptions: babel.TransformOptions;
private readonly projectRoot: string;

public constructor(options: TranspilerOptions) {
this.babelOptions = new BabelConfigReader().readConfig(options.config);
public static inject = tokens(commonTokens.options, commonTokens.produceSourceMaps);
public constructor(options: StrykerOptions, produceSourceMaps: boolean) {
this.babelOptions = new BabelConfigReader().readConfig(options);
this.projectRoot = this.determineProjectRoot(options);
if (options.produceSourceMaps) {
throw new Error(`Invalid \`coverageAnalysis\` "${options.config.coverageAnalysis}" is not supported by the stryker-babel-transpiler. Not able to produce source maps yet. Please set it to "off".`);
if (produceSourceMaps) {
throw new Error(`Invalid \`coverageAnalysis\` "${options.coverageAnalysis}" is not supported by the stryker-babel-transpiler. Not able to produce source maps yet. Please set it to "off".`);
}
}

Expand Down Expand Up @@ -61,8 +63,8 @@ class BabelTranspiler implements Transpiler {
}
}

private determineProjectRoot(options: TranspilerOptions): string {
const configFile = options.config[CONFIG_KEY_FILE];
private determineProjectRoot(options: StrykerOptions): string {
const configFile = options[CONFIG_KEY_FILE];
if (configFile) {
return path.dirname(configFile);
} else {
Expand Down
6 changes: 4 additions & 2 deletions packages/stryker-babel-transpiler/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { TranspilerFactory } from 'stryker-api/transpile';
import BabelTranspiler from './BabelTranspiler';
import { declareClassPlugin, PluginKind } from 'stryker-api/plugin';

TranspilerFactory.instance().register('babel', BabelTranspiler);
export const strykerPlugins = [
declareClassPlugin(PluginKind.Transpiler, 'babel', BabelTranspiler)
];
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import * as path from 'path';
import { File } from 'stryker-api/core';
import { Config } from 'stryker-api/config';
import { File, StrykerOptions } from 'stryker-api/core';
import { ProjectLoader } from '../helpers/projectLoader';
import BabelTranspiler from '../../src/BabelTranspiler';
import { expect } from 'chai';
import { factory } from '@stryker-mutator/test-helpers';

function describeIntegrationTest(projectName: string) {

const projectDir = path.resolve(__dirname, '..', '..', 'testResources', projectName);
let projectFiles: File[] = [];
let resultFiles: File[] = [];
let babelTranspiler: BabelTranspiler;
let config: Config;
let options: StrykerOptions;

beforeEach(async () => {
projectFiles = await ProjectLoader.getFiles(path.join(projectDir, 'source'));
resultFiles = await ProjectLoader.getFiles(path.join(projectDir, 'expectedResult'));
config = new Config();
config.set({ babelrcFile: path.join(projectDir, '.babelrc') });
babelTranspiler = new BabelTranspiler({ config, produceSourceMaps: false });
options = factory.strykerOptions();
options.babelrcFile = path.join(projectDir, '.babelrc');
babelTranspiler = new BabelTranspiler(options, /*produceSourceMaps:*/ false );
});

it('should be able to transpile the input files', async () => {
Expand Down
22 changes: 11 additions & 11 deletions packages/stryker-babel-transpiler/test/unit/BabelTranspilerSpec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import * as path from 'path';
import BabelTranspiler from '../../src/BabelTranspiler';
import { expect } from 'chai';
import { File } from 'stryker-api/core';
import { Config } from 'stryker-api/config';
import { File, StrykerOptions } from 'stryker-api/core';
import * as sinon from 'sinon';
import * as babel from 'babel-core';
import BabelConfigReader, * as babelConfigReaderModule from '../../src/BabelConfigReader';
import { Mock, mock } from '../helpers/mock';
import { factory } from '@stryker-mutator/test-helpers';

describe('BabelTranspiler', () => {
let sut: BabelTranspiler;
let sandbox: sinon.SinonSandbox;
let files: File[];
let transformStub: sinon.SinonStub;
let config: Config;
let options: StrykerOptions;
let babelConfigReaderMock: Mock<BabelConfigReader>;
let babelOptions: any;

Expand All @@ -23,7 +23,7 @@ describe('BabelTranspiler', () => {
sandbox = sinon.createSandbox();
sandbox.stub(babelConfigReaderModule, 'default').returns(babelConfigReaderMock);
transformStub = sandbox.stub(babel, 'transform');
config = new Config();
options = factory.strykerOptions();
files = [
new File(path.resolve('main.js'), 'const main = () => { sum(2); divide(2); }'),
new File(path.resolve('sum.js'), 'const sum = (number) => number + number;'),
Expand All @@ -37,26 +37,26 @@ describe('BabelTranspiler', () => {

function arrangeHappyFlow() {
babelConfigReaderMock.readConfig.returns(babelOptions);
sut = new BabelTranspiler({ config, produceSourceMaps: false });
sut = new BabelTranspiler(options, /*produceSourceMaps:*/ false);
}

it('should read babel config using the BabelConfigReader', () => {
arrangeHappyFlow();
expect(babelConfigReaderModule.default).calledWithNew;
expect(babelConfigReaderMock.readConfig).calledWith(config);
expect(babelConfigReaderMock.readConfig).calledWith(options);
});

it('should throw if `produceSourceMaps` was true and coverage analysis is "perTest"', () => {
config.coverageAnalysis = 'perTest';
expect(() => new BabelTranspiler({ produceSourceMaps: true, config })).throws('Invalid `coverageAnalysis` "perTest" is not supported by the stryker-babel-transpiler. Not able to produce source maps yet. Please set it to "off".');
options.coverageAnalysis = 'perTest';
expect(() => new BabelTranspiler(options, /*produceSourceMaps:*/ true)).throws('Invalid `coverageAnalysis` "perTest" is not supported by the stryker-babel-transpiler. Not able to produce source maps yet. Please set it to "off".');
});
});

describe('transpile', () => {

function arrangeHappyFlow(transformResult: babel.BabelFileResult & { ignored?: boolean } = { code: 'code' }) {
babelConfigReaderMock.readConfig.returns(babelOptions);
sut = new BabelTranspiler({ config, produceSourceMaps: false });
sut = new BabelTranspiler(options, /*produceSourceMaps:*/ false);
transformStub.returns(transformResult);
}

Expand All @@ -77,7 +77,7 @@ describe('BabelTranspiler', () => {
babelOptions.filename = 'override';
babelOptions.filenameRelative = 'override';
arrangeHappyFlow();
sut = new BabelTranspiler({ config, produceSourceMaps: false });
sut = new BabelTranspiler(options, /*produceSourceMaps:*/ false);
await sut.transpile([files[0]]);
expect(transformStub).calledWith(files[0].textContent, {
filename: files[0].name,
Expand Down Expand Up @@ -133,7 +133,7 @@ describe('BabelTranspiler', () => {
it('should return with an error when the babel transform fails', async () => {
const error = new Error('Syntax error');
transformStub.throws(error);
sut = new BabelTranspiler({ produceSourceMaps: false, config });
sut = new BabelTranspiler(options, /*produceSourceMaps:*/ false);
return expect(sut.transpile([new File('picture.js', 'S�L!##���XLDDDDDDDD\K�')])).rejectedWith(`Error while transpiling "picture.js": ${error.stack}`);
});
});
Expand Down
3 changes: 3 additions & 0 deletions packages/stryker-babel-transpiler/tsconfig.src.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"references": [
{
"path": "../stryker-api/tsconfig.src.json"
},
{
"path": "../stryker-test-helpers/tsconfig.src.json"
}
]
}
82 changes: 44 additions & 38 deletions packages/stryker-jest-runner/package.json
Original file line number Diff line number Diff line change
@@ -1,43 +1,49 @@
{
"name": "stryker-jest-runner",
"version": "1.3.0",
"description": "A plugin to use the jest test runner and framework in Stryker, the JavaScript mutation testing framework",
"main": "src/index.js",
"repository": {
"type": "git",
"url": "https://github.com/stryker-mutator/stryker.git"
},
"engines": {
"node": ">=6"
},
"keywords": [
"stryker",
"stryker-plugin",
"jest",
"stryker-test-runner"
],
"author": "Sander koenders <sanderkoenders@gmail.com>",
"contributors": [
"Maarten Mulders <mthmulders@users.noreply.github.com>",
"mshogren <m_shogren@yahoo.com>",
"Nico Jansen <jansennico@gmail.com>",
"Simon de Lang <simondelang@gmail.com>",
"Philipp Weissenbacher <philipp.weissenbacher@gmail.com>",
"Sander koenders <sanderkoenders@gmail.com>"
],
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/stryker-mutator/stryker/issues"
},
"homepage": "https://github.com/stryker-mutator/stryker/tree/master/packages/stryker-jest-runner#readme",
"devDependencies": {
"name": "stryker-jest-runner",
"version": "1.3.0",
"description": "A plugin to use the jest test runner and framework in Stryker, the JavaScript mutation testing framework",
"main": "src/index.js",
"scripts": {
"start": "tsc -w",
"clean": "rimraf \"+(test|src)/**/*+(.d.ts|.js|.map)\" .nyc_output reports coverage",
"test": "nyc --reporter=html --report-dir=reports/coverage --lines 80 --functions 80 --branches 75 npm run mocha",
"mocha": "mocha \"test/helpers/**/*.js\" \"test/unit/**/*.js\" && mocha --timeout 30000 \"test/helpers/**/*.js\" \"test/integration/**/*.js\" --exit"
},
"repository": {
"type": "git",
"url": "https://github.com/stryker-mutator/stryker.git"
},
"engines": {
"node": ">=6"
},
"keywords": [
"stryker",
"stryker-plugin",
"jest",
"stryker-test-runner"
],
"author": "Sander koenders <sanderkoenders@gmail.com>",
"contributors": [
"Maarten Mulders <mthmulders@users.noreply.github.com>",
"mshogren <m_shogren@yahoo.com>",
"Nico Jansen <jansennico@gmail.com>",
"Simon de Lang <simondelang@gmail.com>",
"Philipp Weissenbacher <philipp.weissenbacher@gmail.com>",
"Sander koenders <sanderkoenders@gmail.com>"
],
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/stryker-mutator/stryker/issues"
},
"homepage": "https://github.com/stryker-mutator/stryker/tree/master/packages/stryker-jest-runner#readme",
"devDependencies": {
"@stryker-mutator/test-helpers": "0.0.0",
"@types/semver": "~5.5.0",
"jest": "~23.6.0",
"react": "~16.7.0",
"react-dom": "~16.7.0",
"react-scripts": "~2.1.0",
"react-scripts-ts": "~3.1.0",
"@types/semver": "~5.5.0",
"jest": "~23.6.0",
"react": "~16.7.0",
"react-dom": "~16.7.0",
"react-scripts": "~2.1.0",
"react-scripts-ts": "~3.1.0",
"stryker-api": "^0.23.0"
},
"peerDependencies": {
Expand Down
Loading

0 comments on commit 9a8b539

Please sign in to comment.