diff --git a/.vscode/launch.json b/.vscode/launch.json index 68362f288..5abdd2397 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -180,9 +180,9 @@ { "type": "node", "request": "launch", - "name": "jest collector", + "name": "jest testing", "program": "${workspaceFolder}/node_modules/.bin/jest", - "cwd": "${workspaceFolder}/core/jest-collector", + "cwd": "${workspaceFolder}/core/jest-testing", "args": ["sum"], "console": "integratedTerminal", "internalConsoleOptions": "neverOpen", diff --git a/core/jest-collector/.fixtures/coverage/coverage-final.json b/core/jest-collector/.fixtures/coverage/coverage-final.json deleted file mode 100644 index 12a2385ea..000000000 --- a/core/jest-collector/.fixtures/coverage/coverage-final.json +++ /dev/null @@ -1,2 +0,0 @@ -{"/Users/atanasster/component-controls/core/jest-collector/.fixtures/sum.js": {"path":"/Users/atanasster/component-controls/core/jest-collector/.fixtures/sum.js","statementMap":{"0":{"start":{"line":1,"column":19},"end":{"line":1,"column":34}},"1":{"start":{"line":1,"column":29},"end":{"line":1,"column":34}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":1,"column":19},"end":{"line":1,"column":20}},"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":34}},"line":1}},"branchMap":{},"s":{"0":1,"1":1},"f":{"0":1},"b":{},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"de6aab5068cfadfdde2f76341945f3919765007d"} -} diff --git a/core/jest-collector/.fixtures/jest.config.js b/core/jest-collector/.fixtures/jest.config.js deleted file mode 100644 index 1b61681ad..000000000 --- a/core/jest-collector/.fixtures/jest.config.js +++ /dev/null @@ -1,7 +0,0 @@ -module.exports = { - preset: 'ts-jest', - transform: { - '^.+\\.(ts|tsx)?$': 'ts-jest', - '^.+\\.(js|jsx)$': 'babel-jest', - }, -}; diff --git a/core/jest-collector/src/index.ts b/core/jest-collector/src/index.ts deleted file mode 100644 index 7514c4e97..000000000 --- a/core/jest-collector/src/index.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { runCLI } from 'jest'; -import { Config } from '@jest/types'; -import { AssertionResult } from '@jest/test-result'; -import { FileCoverage, CoverageSummary } from 'istanbul-lib-coverage'; - -export interface JestResults { - /** - * test results - */ - testResults: AssertionResult[]; - /** - * coverage summary data, by file - */ - coverage: Record; -} -export const run = async ( - testFile: string, - testFolder: string, -): Promise => { - const runResults = await runCLI( - ({ - $0: testFile, - roots: ['.'], - silent: true, - verbose: false, - reporters: [], - collectCoverage: true, - coverageReporters: ['none'], - watchman: false, - } as unknown) as Config.Argv, - [testFolder], - ); - const cov = runResults.results.coverageMap; - const result: JestResults = { - testResults: runResults.results.testResults[0].testResults, - coverage: {}, - }; - if (cov) { - Object.keys(cov.data).forEach(file => { - const fc = cov.data[file] as FileCoverage; - result.coverage[file] = fc.toSummary(); - }); - } - - return result; -}; diff --git a/core/jest-collector/test/sum.test.ts b/core/jest-collector/test/sum.test.ts deleted file mode 100644 index 4bb8984e5..000000000 --- a/core/jest-collector/test/sum.test.ts +++ /dev/null @@ -1,22 +0,0 @@ -import path from 'path'; -import { run } from '../src'; - -type Await = T extends PromiseLike ? U : T; - -let results: Await>; -beforeAll(async () => { - results = await run('sum.test.js', path.resolve(__dirname, '../.fixtures')); -}, 50000); -test('testResults ', () => { - expect(results.testResults[0]).toMatchObject({ - ancestorTitles: ['testing sum'], - failureDetails: [], - failureMessages: [], - fullName: 'testing sum sum', - location: null, - numPassingAsserts: 0, - status: 'passed', - title: 'sum', - }); - console.log(results.coverage); -}); diff --git a/core/jest-collector/.babelrc b/core/jest-testing/.babelrc similarity index 100% rename from core/jest-collector/.babelrc rename to core/jest-testing/.babelrc diff --git a/core/jest-collector/.eslintignore b/core/jest-testing/.eslintignore similarity index 100% rename from core/jest-collector/.eslintignore rename to core/jest-testing/.eslintignore diff --git a/core/jest-testing/.fixtures/__jest-1/jest.config.js b/core/jest-testing/.fixtures/__jest-1/jest.config.js new file mode 100644 index 000000000..1e4c36481 --- /dev/null +++ b/core/jest-testing/.fixtures/__jest-1/jest.config.js @@ -0,0 +1,12 @@ +module.exports = { + "preset": "ts-jest", + "transform": { + "^.+\\.(ts|tsx)?$": "ts-jest", + "^.+\\.(js|jsx)$": "babel-jest" + }, + "collectCoverageFrom": [ + "**/*.{js,jsx,tsx,ts}", + "!**/jest.config.js" + ] +} + \ No newline at end of file diff --git a/core/jest-testing/.fixtures/__jest-2/jest.config.js b/core/jest-testing/.fixtures/__jest-2/jest.config.js new file mode 100644 index 000000000..9b89ae9e6 --- /dev/null +++ b/core/jest-testing/.fixtures/__jest-2/jest.config.js @@ -0,0 +1,8 @@ +module.exports = { + "preset": "ts-jest", + "transform": { + "^.+\\.(ts|tsx)?$": "ts-jest", + "^.+\\.(js|jsx)$": "babel-jest" + } +} + \ No newline at end of file diff --git a/core/jest-testing/.fixtures/__jest-3/jest.config.js b/core/jest-testing/.fixtures/__jest-3/jest.config.js new file mode 100644 index 000000000..9b89ae9e6 --- /dev/null +++ b/core/jest-testing/.fixtures/__jest-3/jest.config.js @@ -0,0 +1,8 @@ +module.exports = { + "preset": "ts-jest", + "transform": { + "^.+\\.(ts|tsx)?$": "ts-jest", + "^.+\\.(js|jsx)$": "babel-jest" + } +} + \ No newline at end of file diff --git a/core/jest-testing/.fixtures/__jest-4/jest.config.js b/core/jest-testing/.fixtures/__jest-4/jest.config.js new file mode 100644 index 000000000..9b89ae9e6 --- /dev/null +++ b/core/jest-testing/.fixtures/__jest-4/jest.config.js @@ -0,0 +1,8 @@ +module.exports = { + "preset": "ts-jest", + "transform": { + "^.+\\.(ts|tsx)?$": "ts-jest", + "^.+\\.(js|jsx)$": "babel-jest" + } +} + \ No newline at end of file diff --git a/core/jest-testing/.fixtures/__jest-5/jest.config.js b/core/jest-testing/.fixtures/__jest-5/jest.config.js new file mode 100644 index 000000000..1e4c36481 --- /dev/null +++ b/core/jest-testing/.fixtures/__jest-5/jest.config.js @@ -0,0 +1,12 @@ +module.exports = { + "preset": "ts-jest", + "transform": { + "^.+\\.(ts|tsx)?$": "ts-jest", + "^.+\\.(js|jsx)$": "babel-jest" + }, + "collectCoverageFrom": [ + "**/*.{js,jsx,tsx,ts}", + "!**/jest.config.js" + ] +} + \ No newline at end of file diff --git a/core/jest-collector/.fixtures/sum.js b/core/jest-testing/.fixtures/sum.js similarity index 100% rename from core/jest-collector/.fixtures/sum.js rename to core/jest-testing/.fixtures/sum.js diff --git a/core/jest-collector/.fixtures/sum.test.js b/core/jest-testing/.fixtures/sum.test.js similarity index 100% rename from core/jest-collector/.fixtures/sum.test.js rename to core/jest-testing/.fixtures/sum.test.js diff --git a/core/jest-collector/LICENSE.md b/core/jest-testing/LICENSE.md similarity index 100% rename from core/jest-collector/LICENSE.md rename to core/jest-testing/LICENSE.md diff --git a/core/jest-collector/README.md b/core/jest-testing/README.md similarity index 100% rename from core/jest-collector/README.md rename to core/jest-testing/README.md diff --git a/core/jest-collector/package.json b/core/jest-testing/package.json similarity index 86% rename from core/jest-collector/package.json rename to core/jest-testing/package.json index 0476241ba..a586ce195 100644 --- a/core/jest-collector/package.json +++ b/core/jest-testing/package.json @@ -1,7 +1,7 @@ { - "name": "@component-controls/jest-collector", + "name": "@component-controls/jest-testing", "version": "3.4.5", - "description": "Given a file, gets the associated jest tests and their results", + "description": "Given a test file, extracts the tests, test results and coverage data", "keywords": [ "jest", "testing" @@ -30,13 +30,12 @@ "repository": { "type": "git", "url": "https://github.com/ccontrols/component-controls.git", - "directory": "core/jest-collector" + "directory": "core/jest-testing" }, "license": "MIT", "dependencies": { "jest-cli": "^26.4.2", "babel-jest": "^26.4.4", - "istanbul-lib-coverage": "^3.0.0", "ts-jest": "^26.4.4" }, "devDependencies": { diff --git a/core/jest-collector/rollup.config.js b/core/jest-testing/rollup.config.js similarity index 100% rename from core/jest-collector/rollup.config.js rename to core/jest-testing/rollup.config.js diff --git a/core/jest-testing/src/index.ts b/core/jest-testing/src/index.ts new file mode 100644 index 000000000..48d0176d4 --- /dev/null +++ b/core/jest-testing/src/index.ts @@ -0,0 +1,96 @@ +import fs from 'fs'; +import path from 'path'; +import { runCLI } from 'jest'; +import { Config } from '@jest/types'; +import { AssertionResult, AggregatedResult } from '@jest/test-result'; +import { FileCoverage, CoverageSummary } from 'istanbul-lib-coverage'; + +export const getUniqueFolder = (folder: string): string => { + let fnId = 1; + const fName = () => path.resolve(folder, `__jest-${fnId}`); + while (fs.existsSync(fName())) { + fnId += 1; + } + const folderName = fName(); + fs.mkdirSync(folderName); + return folderName; +}; +export interface JestResults { + /** + * test results + */ + testResults: AssertionResult[]; + /** + * coverage summary data, by file + */ + coverage: Record; +} +export const run = async ( + testFilePath: string, + jestConfig?: Partial, +): Promise => { + const testFolder = path.dirname(testFilePath); + const testFile = path.basename(testFilePath); + const configFolder = getUniqueFolder(testFolder); + const configFile = path.resolve(configFolder, 'jest.config.js'); + fs.writeFileSync( + configFile, + `module.exports = ${JSON.stringify( + { + 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}', + ], + }, + null, + 2, + )} + `, + 'utf8', + ); + let runResults: { + results: AggregatedResult; + globalConfig: Config.GlobalConfig; + }; + try { + runResults = await runCLI( + { + $0: testFile, + silent: true, + verbose: false, + reporters: [], + coverage: true, + coverageReporters: ['none'], + watchman: false, + ...jestConfig, + } as Config.Argv, + [configFolder], + ); + } catch (err) { + console.error(err); + return undefined; + } finally { + fs.unlinkSync(configFile); + fs.rmdirSync(configFolder, { recursive: true }); + } + const cov = runResults.results.coverageMap; + const result: JestResults = { + testResults: runResults.results.testResults[0].testResults, + coverage: {}, + }; + if (cov) { + Object.keys(cov.data).forEach(file => { + const fc = cov.data[file] as FileCoverage; + result.coverage[path.relative(testFolder, file)] = fc.toSummary(); + }); + } + + return result; +}; diff --git a/core/jest-testing/test/sum.test.ts b/core/jest-testing/test/sum.test.ts new file mode 100644 index 000000000..430b85773 --- /dev/null +++ b/core/jest-testing/test/sum.test.ts @@ -0,0 +1,57 @@ +import path from 'path'; +import { run } from '../src'; + +type Await = T extends PromiseLike ? U : T; + +let results: Await>; +beforeAll(async () => { + results = await run(path.resolve(__dirname, '../.fixtures/sum.test.js')); +}, 50000); + +describe('small test', () => { + it('testResults ', () => { + expect(results?.testResults[0]).toMatchObject({ + ancestorTitles: ['testing sum'], + failureDetails: [], + failureMessages: [], + fullName: 'testing sum sum', + location: null, + numPassingAsserts: 0, + status: 'passed', + title: 'sum', + }); + }); + + it('coverage ', () => { + expect(results?.coverage).toMatchObject({ + 'sum.js': { + data: { + lines: { + total: 1, + covered: 1, + skipped: 0, + pct: 100, + }, + functions: { + total: 1, + covered: 1, + skipped: 0, + pct: 100, + }, + statements: { + total: 2, + covered: 2, + skipped: 0, + pct: 100, + }, + branches: { + total: 0, + covered: 0, + skipped: 0, + pct: 100, + }, + }, + }, + }); + }); +}); diff --git a/core/jest-collector/tsconfig.json b/core/jest-testing/tsconfig.json similarity index 100% rename from core/jest-collector/tsconfig.json rename to core/jest-testing/tsconfig.json