From 0a13f259b6c206f46b3e05b317d30b6e9c79dc24 Mon Sep 17 00:00:00 2001 From: atanasster Date: Sat, 20 Mar 2021 02:21:55 -0400 Subject: [PATCH] feat: jest tests collector initial --- .vscode/launch.json | 15 ++++++ core/jest-collector/.babelrc | 13 ++++++ core/jest-collector/.eslintignore | 2 + .../.fixtures/coverage/coverage-final.json | 2 + core/jest-collector/.fixtures/jest.config.js | 7 +++ core/jest-collector/.fixtures/sum.js | 1 + core/jest-collector/.fixtures/sum.test.js | 6 +++ core/jest-collector/LICENSE.md | 21 +++++++++ core/jest-collector/README.md | 13 ++++++ core/jest-collector/package.json | 30 ++++-------- core/jest-collector/rollup.config.js | 5 ++ core/jest-collector/src/collect_reporter.ts | 11 +++++ core/jest-collector/src/index.ts | 46 +++++++++++++++++++ core/jest-collector/test/sum.test.ts | 22 +++++++++ core/jest-collector/tsconfig.json | 13 ++++++ yarn.lock | 6 +-- 16 files changed, 189 insertions(+), 24 deletions(-) create mode 100644 core/jest-collector/.babelrc create mode 100644 core/jest-collector/.eslintignore create mode 100644 core/jest-collector/.fixtures/coverage/coverage-final.json create mode 100644 core/jest-collector/.fixtures/jest.config.js create mode 100644 core/jest-collector/.fixtures/sum.js create mode 100644 core/jest-collector/.fixtures/sum.test.js create mode 100644 core/jest-collector/LICENSE.md create mode 100644 core/jest-collector/README.md create mode 100644 core/jest-collector/rollup.config.js create mode 100644 core/jest-collector/src/collect_reporter.ts create mode 100644 core/jest-collector/src/index.ts create mode 100644 core/jest-collector/test/sum.test.ts create mode 100644 core/jest-collector/tsconfig.json diff --git a/.vscode/launch.json b/.vscode/launch.json index b0b4cdf15..68362f288 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -176,6 +176,21 @@ "program": "${workspaceFolder}/node_modules/jest/bin/jest" } }, + + { + "type": "node", + "request": "launch", + "name": "jest collector", + "program": "${workspaceFolder}/node_modules/.bin/jest", + "cwd": "${workspaceFolder}/core/jest-collector", + "args": ["sum"], + "console": "integratedTerminal", + "internalConsoleOptions": "neverOpen", + "disableOptimisticBPs": true, + "windows": { + "program": "${workspaceFolder}/node_modules/jest/bin/jest" + } + }, { "type": "node", "request": "launch", diff --git a/core/jest-collector/.babelrc b/core/jest-collector/.babelrc new file mode 100644 index 000000000..c64480144 --- /dev/null +++ b/core/jest-collector/.babelrc @@ -0,0 +1,13 @@ +{ + "presets": [ + "@babel/preset-typescript", + [ + "@babel/preset-env", + { + "targets": { + "node": "current" + } + } + ] + ] +} \ No newline at end of file diff --git a/core/jest-collector/.eslintignore b/core/jest-collector/.eslintignore new file mode 100644 index 000000000..d44b137bb --- /dev/null +++ b/core/jest-collector/.eslintignore @@ -0,0 +1,2 @@ +dist +test/fixtures/ \ No newline at end of file diff --git a/core/jest-collector/.fixtures/coverage/coverage-final.json b/core/jest-collector/.fixtures/coverage/coverage-final.json new file mode 100644 index 000000000..12a2385ea --- /dev/null +++ b/core/jest-collector/.fixtures/coverage/coverage-final.json @@ -0,0 +1,2 @@ +{"/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 new file mode 100644 index 000000000..1b61681ad --- /dev/null +++ b/core/jest-collector/.fixtures/jest.config.js @@ -0,0 +1,7 @@ +module.exports = { + preset: 'ts-jest', + transform: { + '^.+\\.(ts|tsx)?$': 'ts-jest', + '^.+\\.(js|jsx)$': 'babel-jest', + }, +}; diff --git a/core/jest-collector/.fixtures/sum.js b/core/jest-collector/.fixtures/sum.js new file mode 100644 index 000000000..bc5f04ddb --- /dev/null +++ b/core/jest-collector/.fixtures/sum.js @@ -0,0 +1 @@ +export const sum = (a, b) => a + b; diff --git a/core/jest-collector/.fixtures/sum.test.js b/core/jest-collector/.fixtures/sum.test.js new file mode 100644 index 000000000..088b38a7a --- /dev/null +++ b/core/jest-collector/.fixtures/sum.test.js @@ -0,0 +1,6 @@ +import { sum } from './sum'; +describe('testing sum', () => { + it('sum', () => { + expect(sum(2, 2)).toBe(4); + }); +}); diff --git a/core/jest-collector/LICENSE.md b/core/jest-collector/LICENSE.md new file mode 100644 index 000000000..a64cf9401 --- /dev/null +++ b/core/jest-collector/LICENSE.md @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Atanas Stoyanov + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/core/jest-collector/README.md b/core/jest-collector/README.md new file mode 100644 index 000000000..abe63bd63 --- /dev/null +++ b/core/jest-collector/README.md @@ -0,0 +1,13 @@ +# Table of contents + +# Overview + +Given a file name, retrives the jest tests and associated results. + +# Installation + +This package is usually installed as part of the @component-controls package, but you can also install it standalone: + +```bash +$ npm install @component-controls/jest-collector --save-dev +``` diff --git a/core/jest-collector/package.json b/core/jest-collector/package.json index bb8d0223c..0476241ba 100644 --- a/core/jest-collector/package.json +++ b/core/jest-collector/package.json @@ -1,10 +1,10 @@ { "name": "@component-controls/jest-collector", "version": "3.4.5", - "description": "Given a file, gets jest tests and their results", + "description": "Given a file, gets the associated jest tests and their results", "keywords": [ - "addon", - "viewport" + "jest", + "testing" ], "main": "dist/index.js", "module": "dist/index.esm.js", @@ -21,7 +21,7 @@ "fix": "yarn lint --fix", "lint": "yarn eslint . --ext mdx,ts,tsx", "prepare": "yarn build", - "test": "ccc -c test/.config && yarn jest && rm -rf public" + "test": "yarn jest" }, "homepage": "https://github.com/ccontrols/component-controls", "bugs": { @@ -33,30 +33,17 @@ "directory": "core/jest-collector" }, "license": "MIT", - "bin": { - "cc-jest": "./bin/index.js" - }, "dependencies": { - "@component-controls/store": "^3.4.5", - "@component-controls/webpack-compile": "^3.4.5", - "@component-controls/webpack-configs": "^3.4.5", - "@testing-library/jest-dom": "^5.11.4", - "@testing-library/react": "^10.4.9", - "@wojtekmaj/enzyme-adapter-react-17": "^0.3.1", - "enzyme": "^3.11.0", - "enzyme-to-json": "^3.5.0", "jest-cli": "^26.4.2", - "rc-util": "^5.0.7", - "react-test-renderer": "^16.13.1", + "babel-jest": "^26.4.4", + "istanbul-lib-coverage": "^3.0.0", "ts-jest": "^26.4.4" }, "devDependencies": { - "@types/enzyme": "^3.10.5", "@types/jest": "^26.0.10", + "@jest/reporters": "^26.0.10", + "@jest/test-result": "^26.0.10", "@types/react-dom": "^16.9.8", - "@types/react-test-renderer": "^16.9.3", - "@types/testing-library__jest-dom": "^5.9.2", - "jest": "^26.4.2", "react-dom": "^16.13.1", "typescript": "^4.0.5" }, @@ -67,6 +54,7 @@ "Atanas Stoyanov" ], "jest": { + "testEnvironment": "node", "preset": "ts-jest", "roots": [ "./test" diff --git a/core/jest-collector/rollup.config.js b/core/jest-collector/rollup.config.js new file mode 100644 index 000000000..ba1c61c86 --- /dev/null +++ b/core/jest-collector/rollup.config.js @@ -0,0 +1,5 @@ +import { config } from '../../rollup-config'; + +export default config({ + input: ['./src/index.ts'], +}); diff --git a/core/jest-collector/src/collect_reporter.ts b/core/jest-collector/src/collect_reporter.ts new file mode 100644 index 000000000..1f2004b03 --- /dev/null +++ b/core/jest-collector/src/collect_reporter.ts @@ -0,0 +1,11 @@ +import { Reporter, Context } from '@jest/reporters'; +import { AggregatedResult } from '@jest/test-result'; + +export default class CustomReporter implements Pick { + async onRunComplete( + _: Set, + results: AggregatedResult, + ): Promise { + console.log('data:', JSON.stringify(results.testResults, null, 2)); + } +} diff --git a/core/jest-collector/src/index.ts b/core/jest-collector/src/index.ts new file mode 100644 index 000000000..7514c4e97 --- /dev/null +++ b/core/jest-collector/src/index.ts @@ -0,0 +1,46 @@ +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 new file mode 100644 index 000000000..4bb8984e5 --- /dev/null +++ b/core/jest-collector/test/sum.test.ts @@ -0,0 +1,22 @@ +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/tsconfig.json b/core/jest-collector/tsconfig.json new file mode 100644 index 000000000..2f3cbc1bd --- /dev/null +++ b/core/jest-collector/tsconfig.json @@ -0,0 +1,13 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "module": "esnext", + "outDir": "dist", + "rootDir": "src", + "declaration": true, + "types": ["node", "jest"], + "typeRoots": ["../../node_modules/@types", "node_modules/@types"] + }, + "include": ["src/**/*"], + "exclude": ["node_modules/**", "test/**", ".fixtures/**"] +} diff --git a/yarn.lock b/yarn.lock index 1a270628c..f2d44c69f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2785,7 +2785,7 @@ "@jest/types" "^26.6.2" expect "^26.6.2" -"@jest/reporters@^26.6.2": +"@jest/reporters@^26.0.10", "@jest/reporters@^26.6.2": version "26.6.2" resolved "https://registry.yarnpkg.com/@jest/reporters/-/reporters-26.6.2.tgz#1f518b99637a5f18307bd3ecf9275f6882a667f6" integrity sha512-h2bW53APG4HvkOnVMo8q3QXa6pcaNt1HkwVsOPMBV6LD/q9oSpxNSYZQYkAnjdMjrJ86UuYeLo+aEZClV6opnw== @@ -2844,7 +2844,7 @@ "@jest/types" "^24.9.0" "@types/istanbul-lib-coverage" "^2.0.0" -"@jest/test-result@^26.6.2": +"@jest/test-result@^26.0.10", "@jest/test-result@^26.6.2": version "26.6.2" resolved "https://registry.yarnpkg.com/@jest/test-result/-/test-result-26.6.2.tgz#55da58b62df134576cc95476efa5f7949e3f5f18" integrity sha512-5O7H5c/7YlojphYNrK02LlDIV2GNPYisKwHm2QTKjNZeEzezCbwYs9swJySv2UfPMyZ0VdsmMv7jIlD/IKYQpQ== @@ -7723,7 +7723,7 @@ babel-helper-to-multiple-sequence-expressions@^0.5.0: resolved "https://registry.yarnpkg.com/babel-helper-to-multiple-sequence-expressions/-/babel-helper-to-multiple-sequence-expressions-0.5.0.tgz#a3f924e3561882d42fcf48907aa98f7979a4588d" integrity sha512-m2CvfDW4+1qfDdsrtf4dwOslQC3yhbgyBFptncp4wvtdrDHqueW7slsYv4gArie056phvQFhT2nRcGS4bnm6mA== -babel-jest@^26.6.3: +babel-jest@^26.4.4, babel-jest@^26.6.3: version "26.6.3" resolved "https://registry.yarnpkg.com/babel-jest/-/babel-jest-26.6.3.tgz#d87d25cb0037577a0c89f82e5755c5d293c01056" integrity sha512-pl4Q+GAVOHwvjrck6jKjvmGhnO3jHX/xuB9d27f+EJZ/6k+6nMuPjorrYp7s++bKKdANwzElBWnLWaObvTnaZA==