Skip to content

Commit 80c672b

Browse files
authored
Fix configuration loading (ArtiomTr#321)
* Used c12 for configuration loading * Rebuild * Removed cfgn dependency * Fixed tests
1 parent 058d339 commit 80c672b

File tree

7 files changed

+8159
-94
lines changed

7 files changed

+8159
-94
lines changed

__mocks__/c12/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const loadConfig = jest.fn();

dist/index.js

Lines changed: 2215 additions & 48 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 5924 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"@actions/core": "^1.9.1",
2121
"@actions/exec": "^1.1.1",
2222
"@actions/github": "^5.0.3",
23-
"cfgn": "^0.1.3",
2423
"fs-extra": "^10.0.0",
2524
"markdown-table": "^2.0.0",
2625
"micromatch": "^4.0.4",
@@ -38,6 +37,7 @@
3837
"@types/semver": "^7.3.9",
3938
"@typescript-eslint/eslint-plugin": "^4.12.0",
4039
"@typescript-eslint/parser": "^4.12.0",
40+
"c12": "^0.2.13",
4141
"esbuild": "^0.12.24",
4242
"eslint": "^7.17.0",
4343
"eslint-config-prettier": "^7.1.0",

src/utils/parseJestConfig.ts

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
1-
import { resolve } from 'path';
1+
import { loadConfig } from 'c12';
22

3-
import { parseConfigurations, PossibleConfiguration } from 'cfgn';
3+
export const parseJestConfig = async (
4+
workingDirectory: string
5+
): Promise<unknown> => {
6+
const { config } = await loadConfig({
7+
cwd: workingDirectory,
8+
name: 'jest',
9+
});
410

5-
// These constants are taken from "jest-config" module, file "constants.js"
6-
const JEST_CONFIG_BASE_NAME = 'jest.config';
7-
const JEST_CONFIG_EXT_ORDER = Object.freeze([
8-
'.js',
9-
'.ts',
10-
'.mjs',
11-
'.cjs',
12-
'.json',
13-
]);
14-
15-
export const parseJestConfig = (workingDirectory: string): Promise<unknown> => {
16-
const possibleJestConfigs: PossibleConfiguration[] = JEST_CONFIG_EXT_ORDER.map(
17-
(extension) => ({
18-
path: resolve(
19-
workingDirectory,
20-
JEST_CONFIG_BASE_NAME.concat(extension)
21-
),
22-
})
23-
);
24-
25-
return parseConfigurations(possibleJestConfigs);
11+
return config;
2612
};

tests/run.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as allCore from '@actions/core';
22
import * as all from '@actions/github';
33
import { getOctokit } from '@actions/github';
4+
import { loadConfig } from 'c12';
45
import { mocked } from 'ts-jest/utils';
56

67
import { Annotation } from '../src/annotations/Annotation';
@@ -215,6 +216,7 @@ const getOptionsMock = mocked(getOptions);
215216
const getCoverageMock = mocked(getCoverage);
216217
const switchBranchMock = mocked(switchBranch);
217218
const createReportMock = mocked(createReport);
219+
const loadConfigMock = mocked(loadConfig);
218220

219221
(getOctokit as jest.Mock<any, any>).mockReturnValue({
220222
rest: {
@@ -232,12 +234,16 @@ beforeEach(() => {
232234
getCoverageMock.mockClear();
233235
createReportMock.mockClear();
234236
(setFailed as jest.Mock).mockClear();
237+
loadConfigMock.mockClear();
235238

236239
getOptionsMock.mockResolvedValue(defaultOptions);
237240
getCoverageMock.mockResolvedValue(standardReport);
238241
createReportMock.mockReturnValue({
239242
runReport: {} as TestRunReport,
240243
} as SummaryReport);
244+
loadConfigMock.mockResolvedValue({
245+
config: {},
246+
});
241247
clearContextMock();
242248
});
243249

0 commit comments

Comments
 (0)