Skip to content

Commit

Permalink
Fix configuration loading (#321)
Browse files Browse the repository at this point in the history
* Used c12 for configuration loading

* Rebuild

* Removed cfgn dependency

* Fixed tests
  • Loading branch information
ArtiomTr committed Oct 21, 2022
1 parent 058d339 commit 80c672b
Show file tree
Hide file tree
Showing 7 changed files with 8,159 additions and 94 deletions.
1 change: 1 addition & 0 deletions __mocks__/c12/index.ts
@@ -0,0 +1 @@
export const loadConfig = jest.fn();
2,263 changes: 2,215 additions & 48 deletions dist/index.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/index.js.map

Large diffs are not rendered by default.

5,943 changes: 5,924 additions & 19 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -20,7 +20,6 @@
"@actions/core": "^1.9.1",
"@actions/exec": "^1.1.1",
"@actions/github": "^5.0.3",
"cfgn": "^0.1.3",
"fs-extra": "^10.0.0",
"markdown-table": "^2.0.0",
"micromatch": "^4.0.4",
Expand All @@ -38,6 +37,7 @@
"@types/semver": "^7.3.9",
"@typescript-eslint/eslint-plugin": "^4.12.0",
"@typescript-eslint/parser": "^4.12.0",
"c12": "^0.2.13",
"esbuild": "^0.12.24",
"eslint": "^7.17.0",
"eslint-config-prettier": "^7.1.0",
Expand Down
32 changes: 9 additions & 23 deletions src/utils/parseJestConfig.ts
@@ -1,26 +1,12 @@
import { resolve } from 'path';
import { loadConfig } from 'c12';

import { parseConfigurations, PossibleConfiguration } from 'cfgn';
export const parseJestConfig = async (
workingDirectory: string
): Promise<unknown> => {
const { config } = await loadConfig({
cwd: workingDirectory,
name: 'jest',
});

// These constants are taken from "jest-config" module, file "constants.js"
const JEST_CONFIG_BASE_NAME = 'jest.config';
const JEST_CONFIG_EXT_ORDER = Object.freeze([
'.js',
'.ts',
'.mjs',
'.cjs',
'.json',
]);

export const parseJestConfig = (workingDirectory: string): Promise<unknown> => {
const possibleJestConfigs: PossibleConfiguration[] = JEST_CONFIG_EXT_ORDER.map(
(extension) => ({
path: resolve(
workingDirectory,
JEST_CONFIG_BASE_NAME.concat(extension)
),
})
);

return parseConfigurations(possibleJestConfigs);
return config;
};
6 changes: 6 additions & 0 deletions tests/run.test.ts
@@ -1,6 +1,7 @@
import * as allCore from '@actions/core';
import * as all from '@actions/github';
import { getOctokit } from '@actions/github';
import { loadConfig } from 'c12';
import { mocked } from 'ts-jest/utils';

import { Annotation } from '../src/annotations/Annotation';
Expand Down Expand Up @@ -215,6 +216,7 @@ const getOptionsMock = mocked(getOptions);
const getCoverageMock = mocked(getCoverage);
const switchBranchMock = mocked(switchBranch);
const createReportMock = mocked(createReport);
const loadConfigMock = mocked(loadConfig);

(getOctokit as jest.Mock<any, any>).mockReturnValue({
rest: {
Expand All @@ -232,12 +234,16 @@ beforeEach(() => {
getCoverageMock.mockClear();
createReportMock.mockClear();
(setFailed as jest.Mock).mockClear();
loadConfigMock.mockClear();

getOptionsMock.mockResolvedValue(defaultOptions);
getCoverageMock.mockResolvedValue(standardReport);
createReportMock.mockReturnValue({
runReport: {} as TestRunReport,
} as SummaryReport);
loadConfigMock.mockResolvedValue({
config: {},
});
clearContextMock();
});

Expand Down

0 comments on commit 80c672b

Please sign in to comment.