Skip to content

Commit d0fd9a9

Browse files
committed
Add e2e test for runCore
1 parent 79dbfd5 commit d0fd9a9

File tree

7 files changed

+121
-7
lines changed

7 files changed

+121
-7
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`runCore Jest programmatically 1`] = `
4+
Object {
5+
"numFailedTestSuites": 0,
6+
"numFailedTests": 0,
7+
"numPassedTestSuites": 1,
8+
"numPassedTests": 1,
9+
"numPendingTestSuites": 0,
10+
"numPendingTests": 0,
11+
"numRuntimeErrorTestSuites": 0,
12+
"numTodoTests": 0,
13+
"numTotalTestSuites": 1,
14+
"numTotalTests": 1,
15+
"openHandles": Array [],
16+
"snapshot": Object {
17+
"added": 0,
18+
"didUpdate": false,
19+
"failure": false,
20+
"filesAdded": 0,
21+
"filesRemoved": 0,
22+
"filesRemovedList": Array [],
23+
"filesUnmatched": 0,
24+
"filesUpdated": 0,
25+
"matched": 0,
26+
"total": 0,
27+
"unchecked": 0,
28+
"uncheckedKeysByFile": Array [],
29+
"unmatched": 0,
30+
"updated": 0,
31+
},
32+
"success": true,
33+
"testResults": Array [
34+
Object {
35+
"failureMessage": null,
36+
"leaks": false,
37+
"numFailingTests": 0,
38+
"numPassingTests": 1,
39+
"numPendingTests": 0,
40+
"numTodoTests": 0,
41+
"openHandles": Array [],
42+
"skipped": false,
43+
"snapshot": Object {
44+
"added": 0,
45+
"fileDeleted": false,
46+
"matched": 0,
47+
"unchecked": 0,
48+
"uncheckedKeys": Array [],
49+
"unmatched": 0,
50+
"updated": 0,
51+
},
52+
"testFilePath": "src/app.spec.js",
53+
"testResults": Array [
54+
Object {
55+
"ancestorTitles": Array [
56+
"jest-core",
57+
],
58+
"failureDetails": Array [],
59+
"failureMessages": Array [],
60+
"fullName": "jest-core should run this test",
61+
"invocations": 1,
62+
"location": null,
63+
"numPassingAsserts": 0,
64+
"retryReasons": Array [],
65+
"status": "passed",
66+
"title": "should run this test",
67+
},
68+
],
69+
},
70+
],
71+
"wasInterrupted": false,
72+
}
73+
`;

e2e/__tests__/runProgrammatically.test.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,40 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {resolve} from 'path';
8+
import {relative, resolve} from 'path';
9+
import type {AggregatedResult} from '@jest/test-result';
910
import {run} from '../Utils';
1011

1112
const dir = resolve(__dirname, '..', 'run-programmatically');
1213

13-
test('run Jest programmatically cjs', () => {
14+
test('runCLI Jest programmatically cjs', () => {
1415
const {stdout} = run('node cjs.js --version', dir);
1516
expect(stdout).toMatch(/\d{2}\.\d{1,2}\.\d{1,2}[-\S]*-dev$/);
1617
});
1718

18-
test('run Jest programmatically esm', () => {
19+
test('runCLI Jest programmatically esm', () => {
1920
const {stdout} = run('node index.js --version', dir);
2021
expect(stdout).toMatch(/\d{2}\.\d{1,2}\.\d{1,2}[-\S]*-dev$/);
2122
});
23+
24+
test('runCore Jest programmatically', () => {
25+
const {stdout} = run('node core.mjs', dir);
26+
const {startTime, ...results}: AggregatedResult = JSON.parse(
27+
stdout.split('==== results ====')[1],
28+
);
29+
30+
const prunedResults = {
31+
...results,
32+
33+
testResults: results.testResults.map(
34+
({testFilePath, perfStats, ...testFileData}) => ({
35+
...testFileData,
36+
testFilePath: relative(dir, testFilePath),
37+
testResults: testFileData.testResults.map(
38+
({duration, ...testResult}) => testResult,
39+
),
40+
}),
41+
),
42+
};
43+
expect(prunedResults).toMatchSnapshot();
44+
});

e2e/run-programmatically/cjs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77

88
// Running Jest like this is not officially supported,
99
// but it is common practice until there is a proper API as a substitute.
10-
require('jest').run(process.argv);
10+
require('jest').runCLI(process.argv);

e2e/run-programmatically/core.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import jest from 'jest';
2+
3+
const {globalConfig, configs} = await jest.readConfigs(process.argv, ['.']);
4+
const runConfig = Object.freeze({
5+
...globalConfig,
6+
collectCoverage: false,
7+
json: true,
8+
watch: false,
9+
});
10+
const {result} = await jest.runCore(runConfig, configs);
11+
console.log('==== results ====');
12+
console.log(JSON.stringify(result, null, 2));

e2e/run-programmatically/esm.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {run} from 'jest';
8+
import {runCLI} from 'jest';
99

1010
// Running Jest like this is not officially supported,
1111
// but it is common practice until there is a proper API as a substitute.
12-
run(process.argv);
12+
runCLI(process.argv);

e2e/run-programmatically/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
"name": "run-programmatically",
33
"version": "1.0.0",
44
"dependencies": {},
5-
"jest": {}
5+
"jest": {
6+
"testEnvironment": "node",
7+
"maxWorkers": 1
8+
}
69
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
describe('jest-core', () => {
2+
it('should run this test', () => {});
3+
});

0 commit comments

Comments
 (0)