-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
23bdff4
commit ca9daf3
Showing
11 changed files
with
154 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './run-tests'; | ||
export * from './related-tests'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
core/jest-testing/test/fixtures/component/__tests__/some_more_tests.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]} | ||
> | ||
</a> | ||
`); | ||
}); |
20 changes: 20 additions & 0 deletions
20
core/jest-testing/test/fixtures/component/link.react.controls.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]} | ||
> | ||
</a> | ||
`); | ||
}); |
14 changes: 14 additions & 0 deletions
14
core/jest-testing/test/fixtures/simple/__jest-tmp-1/jest.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
core/jest-testing/test/fixtures/simple/__jest-tmp-3/jest.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
core/jest-testing/test/tests/run-related/react-link.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({}); | ||
}); | ||
}); |
4 changes: 2 additions & 2 deletions
4
...esting/test/tests/react-component.test.ts → ...ng/test/tests/run/react-component.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
core/jest-testing/test/tests/small.test.ts → ...jest-testing/test/tests/run/small.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters