Skip to content

Commit

Permalink
feat: jest tests collector initial
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Mar 20, 2021
1 parent efd6282 commit 0a13f25
Show file tree
Hide file tree
Showing 16 changed files with 189 additions and 24 deletions.
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 13 additions & 0 deletions core/jest-collector/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"presets": [
"@babel/preset-typescript",
[
"@babel/preset-env",
{
"targets": {
"node": "current"
}
}
]
]
}
2 changes: 2 additions & 0 deletions core/jest-collector/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
test/fixtures/
2 changes: 2 additions & 0 deletions core/jest-collector/.fixtures/coverage/coverage-final.json
Original file line number Diff line number Diff line change
@@ -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"}
}
7 changes: 7 additions & 0 deletions core/jest-collector/.fixtures/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
preset: 'ts-jest',
transform: {
'^.+\\.(ts|tsx)?$': 'ts-jest',
'^.+\\.(js|jsx)$': 'babel-jest',
},
};
1 change: 1 addition & 0 deletions core/jest-collector/.fixtures/sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const sum = (a, b) => a + b;
6 changes: 6 additions & 0 deletions core/jest-collector/.fixtures/sum.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { sum } from './sum';
describe('testing sum', () => {
it('sum', () => {
expect(sum(2, 2)).toBe(4);
});
});
21 changes: 21 additions & 0 deletions core/jest-collector/LICENSE.md
Original file line number Diff line number Diff line change
@@ -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.
13 changes: 13 additions & 0 deletions core/jest-collector/README.md
Original file line number Diff line number Diff line change
@@ -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
```
30 changes: 9 additions & 21 deletions core/jest-collector/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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": {
Expand All @@ -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"
},
Expand All @@ -67,6 +54,7 @@
"Atanas Stoyanov"
],
"jest": {
"testEnvironment": "node",
"preset": "ts-jest",
"roots": [
"./test"
Expand Down
5 changes: 5 additions & 0 deletions core/jest-collector/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { config } from '../../rollup-config';

export default config({
input: ['./src/index.ts'],
});
11 changes: 11 additions & 0 deletions core/jest-collector/src/collect_reporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Reporter, Context } from '@jest/reporters';
import { AggregatedResult } from '@jest/test-result';

export default class CustomReporter implements Pick<Reporter, 'onRunComplete'> {
async onRunComplete(
_: Set<Context>,
results: AggregatedResult,
): Promise<void> {
console.log('data:', JSON.stringify(results.testResults, null, 2));
}
}
46 changes: 46 additions & 0 deletions core/jest-collector/src/index.ts
Original file line number Diff line number Diff line change
@@ -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<string, CoverageSummary>;
}
export const run = async (
testFile: string,
testFolder: string,
): Promise<JestResults> => {
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;
};
22 changes: 22 additions & 0 deletions core/jest-collector/test/sum.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import path from 'path';
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('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);
});
13 changes: 13 additions & 0 deletions core/jest-collector/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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/**"]
}
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand Down Expand Up @@ -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==
Expand Down Expand Up @@ -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==
Expand Down

0 comments on commit 0a13f25

Please sign in to comment.