Skip to content

Commit

Permalink
feat: run related tests to a file
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Mar 21, 2021
1 parent 23bdff4 commit ca9daf3
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
"name": "jest testing",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"cwd": "${workspaceFolder}/core/jest-testing",
"args": ["component"],
"args": ["react-link"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
Expand Down
1 change: 1 addition & 0 deletions core/jest-testing/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './run-tests';
export * from './related-tests';
45 changes: 45 additions & 0 deletions core/jest-testing/src/related-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import path from 'path';
import { Config } from '@jest/types';
import { sync as globSync } from 'glob';
import { run, JestResults } from './run-tests';

export type RelatedTest = {
/**
* full name of the test file
*/
testFileName: string;
} & JestResults;

export type RelatedTests = RelatedTest[];

export const runRelatedTests = async (
fileName: string,
jestConfig?: Partial<Config.Argv>,
): Promise<RelatedTests> => {
const name = path.basename(fileName).split('.')[0];
const fileDir = path.dirname(fileName);
const globs = [
`${fileDir}${path.sep}${name}*.@(test|spec).@(j|t)s?(x)`,
`${fileDir}${path.sep}__tests__${path.sep}**${path.sep}*.@(j|t)s?(x)`,
];
const testFiles = globs.reduce(
(acc: string[], match) => [...acc, ...globSync(match, { nocase: true })],
[],
);
const results: RelatedTests = await Promise.all(
testFiles.map(async testFile => {
const rootDir = path.relative(
path.resolve(path.dirname(testFile), 'config'),
fileDir,
);
const result = await run(testFile, { rootDir, ...jestConfig });

return {
testFileName: path.relative(fileDir, testFile),
...result,
} as RelatedTest;
}),
);

return results;
};
9 changes: 6 additions & 3 deletions core/jest-testing/src/run-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const run = async (
configFile,
`module.exports = ${JSON.stringify(
{
rootDir: '..',
rootDir: jestConfig?.rootDir || '..',
preset: 'ts-jest',
transform: {
'^.+\\.(ts|tsx)?$': 'ts-jest',
Expand All @@ -46,7 +46,7 @@ export const run = async (
collectCoverageFrom: [
'**/*.{js,jsx,tsx,ts}',
'!**/jest.config.js',
'!**/*.{test,spec}.{js,ts}',
'!**/*.{test,spec}.{js,jsx,tsx,ts}',
],
},
null,
Expand All @@ -62,7 +62,7 @@ export const run = async (
try {
runResults = await runCLI(
{
$0: testFile,
testRegex: testFile,
silent: true,
verbose: false,
reporters: [],
Expand All @@ -81,6 +81,9 @@ export const run = async (
fs.rmdirSync(configFolder, { recursive: true });
}
const cov = runResults.results.coverageMap;
if (runResults.results.testResults.length !== 1) {
throw `More than one test results returned ${runResults.results.testResults.length}`;
}
const result: JestResults = {
testResults: runResults.results.testResults[0].testResults,
coverage: {},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';
import renderer from 'react-test-renderer';
import Link from '../Link.react';

test('tests in __tests__ folder', () => {
const component = renderer.create(
<Link page="http://www.google.com">Google</Link>,
);
let tree = component.toJSON();
expect(tree).toMatchInlineSnapshot(`
<a
className="normal"
href="http://www.google.com"
onMouseEnter={[Function]}
onMouseLeave={[Function]}
>
Google
</a>
`);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';
import renderer from 'react-test-renderer';
import Link from './Link.react';

test('tests in same folder', () => {
const component = renderer.create(
<Link page="http://www.google.com">Google</Link>,
);
let tree = component.toJSON();
expect(tree).toMatchInlineSnapshot(`
<a
className="normal"
href="http://www.google.com"
onMouseEnter={[Function]}
onMouseLeave={[Function]}
>
Google
</a>
`);
});
14 changes: 14 additions & 0 deletions core/jest-testing/test/fixtures/simple/__jest-tmp-1/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
"rootDir": "..",
"preset": "ts-jest",
"transform": {
"^.+\\.(ts|tsx)?$": "ts-jest",
"^.+\\.(js|jsx)$": "babel-jest"
},
"collectCoverageFrom": [
"**/*.{js,jsx,tsx,ts}",
"!**/jest.config.js",
"!**/*.{test,spec}.{js,ts}"
]
}

14 changes: 14 additions & 0 deletions core/jest-testing/test/fixtures/simple/__jest-tmp-3/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
"rootDir": "..",
"preset": "ts-jest",
"transform": {
"^.+\\.(ts|tsx)?$": "ts-jest",
"^.+\\.(js|jsx)$": "babel-jest"
},
"collectCoverageFrom": [
"**/*.{js,jsx,tsx,ts}",
"!**/jest.config.js",
"!**/*.{test,spec}.{js,ts}"
]
}

29 changes: 29 additions & 0 deletions core/jest-testing/test/tests/run-related/react-link.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import path from 'path';
import { runRelatedTests } from '../../../src';

type Await<T> = T extends PromiseLike<infer U> ? U : T;

let results: Await<ReturnType<typeof runRelatedTests>>;
beforeAll(async () => {
results = await runRelatedTests(
path.resolve(__dirname, '../../fixtures/component/Link.react.js'),
);
}, 100000);

describe('react link related tests', () => {
it('testResults ', () => {
const files = results.map(({ testFileName }) => testFileName);
expect(files).toMatchObject([
'link.react.controls.test.js',
'Link.react.test.js',
'__tests__/some_more_tests.js',
]);
});

it('sub-folder coverage ', () => {
const result = results.find(
({ testFileName }) => testFileName === '__tests__/some_more_tests.js',
);
expect(result?.coverage).toMatchObject({});
});
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import path from 'path';
import { run } from '../../src';
import { run } from '../../../src';

type Await<T> = T extends PromiseLike<infer U> ? U : T;

let results: Await<ReturnType<typeof run>>;
beforeAll(async () => {
results = await run(
path.resolve(__dirname, '../fixtures/component/Link.react.test.js'),
path.resolve(__dirname, '../../fixtures/component/Link.react.test.js'),
);
}, 50000);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import path from 'path';
import { run } from '../../src';
import { run } from '../../../src';

type Await<T> = T extends PromiseLike<infer U> ? U : T;

let results: Await<ReturnType<typeof run>>;
beforeAll(async () => {
results = await run(
path.resolve(__dirname, '../fixtures/simple/sum.test.js'),
path.resolve(__dirname, '../../fixtures/simple/sum.test.js'),
);
}, 50000);

Expand Down

0 comments on commit ca9daf3

Please sign in to comment.