From ca9daf32a1dfb3ed126755bdb76f4e0c8e4153b6 Mon Sep 17 00:00:00 2001 From: atanasster Date: Sun, 21 Mar 2021 04:57:38 -0400 Subject: [PATCH] feat: run related tests to a file --- .vscode/launch.json | 2 +- core/jest-testing/src/index.ts | 1 + core/jest-testing/src/related-tests.ts | 45 +++++++++++++++++++ core/jest-testing/src/run-tests.ts | 9 ++-- .../component/__tests__/some_more_tests.js | 20 +++++++++ .../component/link.react.controls.test.js | 20 +++++++++ .../simple/__jest-tmp-1/jest.config.js | 14 ++++++ .../simple/__jest-tmp-3/jest.config.js | 14 ++++++ .../test/tests/run-related/react-link.test.ts | 29 ++++++++++++ .../tests/{ => run}/react-component.test.ts | 4 +- .../test/tests/{ => run}/small.test.ts | 4 +- 11 files changed, 154 insertions(+), 8 deletions(-) create mode 100644 core/jest-testing/src/related-tests.ts create mode 100644 core/jest-testing/test/fixtures/component/__tests__/some_more_tests.js create mode 100644 core/jest-testing/test/fixtures/component/link.react.controls.test.js create mode 100644 core/jest-testing/test/fixtures/simple/__jest-tmp-1/jest.config.js create mode 100644 core/jest-testing/test/fixtures/simple/__jest-tmp-3/jest.config.js create mode 100644 core/jest-testing/test/tests/run-related/react-link.test.ts rename core/jest-testing/test/tests/{ => run}/react-component.test.ts (91%) rename core/jest-testing/test/tests/{ => run}/small.test.ts (92%) diff --git a/.vscode/launch.json b/.vscode/launch.json index 44f94ae29..edac37cca 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -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, diff --git a/core/jest-testing/src/index.ts b/core/jest-testing/src/index.ts index 225295c7b..d00049f56 100644 --- a/core/jest-testing/src/index.ts +++ b/core/jest-testing/src/index.ts @@ -1 +1,2 @@ export * from './run-tests'; +export * from './related-tests'; diff --git a/core/jest-testing/src/related-tests.ts b/core/jest-testing/src/related-tests.ts new file mode 100644 index 000000000..2998427dd --- /dev/null +++ b/core/jest-testing/src/related-tests.ts @@ -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, +): Promise => { + 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; +}; diff --git a/core/jest-testing/src/run-tests.ts b/core/jest-testing/src/run-tests.ts index bb13beddd..53c1eafcb 100644 --- a/core/jest-testing/src/run-tests.ts +++ b/core/jest-testing/src/run-tests.ts @@ -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', @@ -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, @@ -62,7 +62,7 @@ export const run = async ( try { runResults = await runCLI( { - $0: testFile, + testRegex: testFile, silent: true, verbose: false, reporters: [], @@ -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: {}, diff --git a/core/jest-testing/test/fixtures/component/__tests__/some_more_tests.js b/core/jest-testing/test/fixtures/component/__tests__/some_more_tests.js new file mode 100644 index 000000000..d3d104d81 --- /dev/null +++ b/core/jest-testing/test/fixtures/component/__tests__/some_more_tests.js @@ -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( + Google, + ); + let tree = component.toJSON(); + expect(tree).toMatchInlineSnapshot(` + + Google + +`); +}); diff --git a/core/jest-testing/test/fixtures/component/link.react.controls.test.js b/core/jest-testing/test/fixtures/component/link.react.controls.test.js new file mode 100644 index 000000000..b20742e8b --- /dev/null +++ b/core/jest-testing/test/fixtures/component/link.react.controls.test.js @@ -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( + Google, + ); + let tree = component.toJSON(); + expect(tree).toMatchInlineSnapshot(` + + Google + +`); +}); diff --git a/core/jest-testing/test/fixtures/simple/__jest-tmp-1/jest.config.js b/core/jest-testing/test/fixtures/simple/__jest-tmp-1/jest.config.js new file mode 100644 index 000000000..5b0bf6075 --- /dev/null +++ b/core/jest-testing/test/fixtures/simple/__jest-tmp-1/jest.config.js @@ -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}" + ] +} + \ No newline at end of file diff --git a/core/jest-testing/test/fixtures/simple/__jest-tmp-3/jest.config.js b/core/jest-testing/test/fixtures/simple/__jest-tmp-3/jest.config.js new file mode 100644 index 000000000..5b0bf6075 --- /dev/null +++ b/core/jest-testing/test/fixtures/simple/__jest-tmp-3/jest.config.js @@ -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}" + ] +} + \ No newline at end of file diff --git a/core/jest-testing/test/tests/run-related/react-link.test.ts b/core/jest-testing/test/tests/run-related/react-link.test.ts new file mode 100644 index 000000000..1e33ee67b --- /dev/null +++ b/core/jest-testing/test/tests/run-related/react-link.test.ts @@ -0,0 +1,29 @@ +import path from 'path'; +import { runRelatedTests } from '../../../src'; + +type Await = T extends PromiseLike ? U : T; + +let results: Await>; +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({}); + }); +}); diff --git a/core/jest-testing/test/tests/react-component.test.ts b/core/jest-testing/test/tests/run/react-component.test.ts similarity index 91% rename from core/jest-testing/test/tests/react-component.test.ts rename to core/jest-testing/test/tests/run/react-component.test.ts index fc6dc18dc..3c8d20161 100644 --- a/core/jest-testing/test/tests/react-component.test.ts +++ b/core/jest-testing/test/tests/run/react-component.test.ts @@ -1,12 +1,12 @@ import path from 'path'; -import { run } from '../../src'; +import { run } from '../../../src'; type Await = T extends PromiseLike ? U : T; let results: Await>; beforeAll(async () => { results = await run( - path.resolve(__dirname, '../fixtures/component/Link.react.test.js'), + path.resolve(__dirname, '../../fixtures/component/Link.react.test.js'), ); }, 50000); diff --git a/core/jest-testing/test/tests/small.test.ts b/core/jest-testing/test/tests/run/small.test.ts similarity index 92% rename from core/jest-testing/test/tests/small.test.ts rename to core/jest-testing/test/tests/run/small.test.ts index a93e503e3..9d210cf53 100644 --- a/core/jest-testing/test/tests/small.test.ts +++ b/core/jest-testing/test/tests/run/small.test.ts @@ -1,12 +1,12 @@ import path from 'path'; -import { run } from '../../src'; +import { run } from '../../../src'; type Await = T extends PromiseLike ? U : T; let results: Await>; beforeAll(async () => { results = await run( - path.resolve(__dirname, '../fixtures/simple/sum.test.js'), + path.resolve(__dirname, '../../fixtures/simple/sum.test.js'), ); }, 50000);