Skip to content

Commit

Permalink
fat(jest-runner): pass stryker mutated config via filesystem
Browse files Browse the repository at this point in the history
Turns out that we can also make projects play nice with stryker-modified jest configs. There are two requirements however - they need to have rootDir set in jest CLI args and we need to make sure that the filename ends in .json so that require doesn't trip over
  • Loading branch information
swist committed Mar 7, 2021
1 parent 455e170 commit 46f9cc6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { join } from 'path';
import { writeFile } from 'fs/promises';

import { jestWrapper } from '../utils';
import { JestRunResult } from '../jest-run-result';
Expand All @@ -7,17 +8,21 @@ import { JestTestAdapter, RunSettings } from './jest-test-adapter';

export class JestGreaterThan25TestAdapter implements JestTestAdapter {
public async run({ jestConfig, projectRoot, fileNameUnderTest, testNamePattern, jestConfigPath }: RunSettings): Promise<JestRunResult> {
const config = jestConfigPath ? join(projectRoot, jestConfigPath) : JSON.stringify(jestConfig);
const config = JSON.stringify(jestConfig);
const mutatedConfigPath = jestConfigPath ? join(projectRoot, jestConfigPath + '.stryker-jest-config.json') : '';
if (mutatedConfigPath) await writeFile(mutatedConfigPath, config, { flag: 'w+' });
const rootDir = jestConfig.rootDir ?? projectRoot;

const result = await jestWrapper.runCLI(
{
$0: 'stryker',
_: fileNameUnderTest ? [fileNameUnderTest] : [],
findRelatedTests: !!fileNameUnderTest,
config,
config: mutatedConfigPath || config,
runInBand: true,
silent: true,
testNamePattern,
rootDir,
},
[projectRoot]
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { join } from 'path';
import { writeFile } from 'fs/promises';

import jest from 'jest';

Expand All @@ -11,15 +12,19 @@ import { RunSettings, JestTestAdapter } from './jest-test-adapter';
* It has a lot of `any` typings here, since the installed typings are not in sync.
*/
export class JestLessThan25TestAdapter implements JestTestAdapter {
public run({ jestConfig, projectRoot, fileNameUnderTest, testNamePattern, jestConfigPath }: RunSettings): Promise<JestRunResult> {
const config = jestConfigPath ? join(projectRoot, jestConfigPath) : JSON.stringify(jestConfig);
public async run({ jestConfig, projectRoot, fileNameUnderTest, testNamePattern, jestConfigPath }: RunSettings): Promise<JestRunResult> {
const config = JSON.stringify(jestConfig);
const mutatedConfigPath = jestConfigPath ? join(projectRoot, jestConfigPath + '.stryker-jest-config.json') : '';
if (mutatedConfigPath) await writeFile(mutatedConfigPath, config, { flag: 'w+' });

return jest.runCLI(
{
...(fileNameUnderTest && { _: [fileNameUnderTest], findRelatedTests: true }),
config,
config: mutatedConfigPath || config,
runInBand: true,
silent: true,
testNamePattern,
rootDir: projectRoot,
} as any,
[projectRoot]
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { join } from 'path';
import fs from 'fs';

import { testInjector } from '@stryker-mutator/test-helpers';
import { expect } from 'chai';
Expand All @@ -12,6 +13,7 @@ import { jestWrapper } from '../../../src/utils/jest-wrapper';
describe(JestGreaterThan25TestAdapter.name, () => {
let sut: JestGreaterThan25TestAdapter;
let runCLIStub: sinon.SinonStub;
let fsWriteFileStub: sinon.SinonStub;

const projectRoot = '/path/to/project';
const fileNameUnderTest = '/path/to/file';
Expand All @@ -24,7 +26,8 @@ describe(JestGreaterThan25TestAdapter.name, () => {
config: jestConfig,
result: 'testResult',
});

fsWriteFileStub = sinon.stub(fs.promises, 'writeFile');
fsWriteFileStub.resolves({});
sut = testInjector.injector.injectClass(JestGreaterThan25TestAdapter);
});

Expand All @@ -46,28 +49,36 @@ describe(JestGreaterThan25TestAdapter.name, () => {
runInBand: true,
silent: true,
testNamePattern: undefined,
rootDir: projectRoot,
},
[projectRoot]
);
});
});
describe('when jestConfigPath provided', () => {
it('should pass the config path instead jest config flag', async () => {
const strykerConfigPath = join(projectRoot, jestConfigPath + '.stryker-jest-config.json');
it('should pass the stryker modified jest config path instead jest config flag', async () => {
await sut.run({ jestConfig, projectRoot, fileNameUnderTest, jestConfigPath });

expect(runCLIStub).calledWith(
{
$0: 'stryker',
_: [fileNameUnderTest],
config: join(projectRoot, jestConfigPath),
config: strykerConfigPath,
findRelatedTests: true,
runInBand: true,
silent: true,
testNamePattern: undefined,
rootDir: projectRoot,
},
[projectRoot]
);
});

it('should write the config to the json config with stryker suffix in the filename', async () => {
await sut.run({ jestConfig, projectRoot, fileNameUnderTest, jestConfigPath });
expect(fsWriteFileStub).calledWith(strykerConfigPath, JSON.stringify({ rootDir: projectRoot }));
});
});
it('should call the runCLI method with the --findRelatedTests flag', async () => {
await sut.run({ jestConfig, projectRoot, fileNameUnderTest });
Expand All @@ -81,6 +92,7 @@ describe(JestGreaterThan25TestAdapter.name, () => {
runInBand: true,
silent: true,
testNamePattern: undefined,
rootDir: projectRoot,
},
[projectRoot]
);
Expand All @@ -98,6 +110,7 @@ describe(JestGreaterThan25TestAdapter.name, () => {
runInBand: true,
silent: true,
testNamePattern: 'Foo should bar',
rootDir: projectRoot,
},
[projectRoot]
);
Expand Down

0 comments on commit 46f9cc6

Please sign in to comment.