Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/load referenced config from packagejson #14044

Merged
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
3210659
Loads .json config file when jest key has a path
rafaelrabelos Apr 1, 2023
398dc06
Adds feature explaining section for docs
rafaelrabelos Apr 1, 2023
d0fd07d
Adds changelog info
rafaelrabelos Apr 1, 2023
2225077
Update changelog with PR info
rafaelrabelos Apr 1, 2023
0948287
Update CHANGELOG.md
SimenB Apr 5, 2023
b6a58ff
Merge branch 'main' into feat/load_referenced_config_from_packagejson
SimenB Apr 5, 2023
c539a99
fix non callable expressions
rafaelrabelos Apr 5, 2023
0a21122
Fixed formatting issues in Configuration.md
rafaelrabelos Apr 5, 2023
111ce11
Refactor to optimize readFileAsync calls
rafaelrabelos Apr 6, 2023
a06ed19
Merge branch 'main' into feat/load_referenced_config_from_packagejson
SimenB Apr 19, 2023
6fa9dde
Merge branch 'main' into feat/load_referenced_config_from_packagejson
rafaelrabelos Aug 9, 2023
26f5427
Lint fixing for resolveConfigPath
rafaelrabelos Aug 9, 2023
b3379dc
Fixes code style issues from prettier
rafaelrabelos Aug 9, 2023
dd4238b
Avoid multiple configuration files
rafaelrabelos Aug 11, 2023
517e302
Remove unecessary try-catch
rafaelrabelos Aug 11, 2023
8f326a2
Refactor for "jest" key catch on parse errors
rafaelrabelos Aug 11, 2023
e50fda7
adds test case for load config file from jest key
rafaelrabelos Aug 11, 2023
1f3b8f0
Adds fs dependency
rafaelrabelos Aug 11, 2023
94e23f2
Test and fix file resolution
rafaelrabelos Aug 12, 2023
8fcfa68
Improves doc example
rafaelrabelos Aug 12, 2023
d174794
Update docs/Configuration.md
rafaelrabelos Aug 13, 2023
a551494
Merge branch 'main' into feat/load_referenced_config_from_packagejson
SimenB Aug 13, 2023
fce78ca
Remove AAA comments from tests
rafaelrabelos Aug 16, 2023
6149986
make resolveJestKey types well defined
rafaelrabelos Aug 16, 2023
ddbb1c8
Merge branch 'feat/load_referenced_config_from_packagejson' of https:…
rafaelrabelos Aug 16, 2023
af8716b
Aditional typing refactor
rafaelrabelos Aug 16, 2023
78c0ba5
Test case for when jest key is not a valid file
rafaelrabelos Aug 16, 2023
b552e19
Optmize Parse calls
rafaelrabelos Aug 17, 2023
ab6d71f
Merge branch 'main' into feat/load_referenced_config_from_packagejson
SimenB Nov 27, 2023
00167af
do not support directory
SimenB Nov 27, 2023
886420c
bring test back, and simplify
SimenB Nov 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Features

- `[jest-config]` Loads `.json` config file from provided path in package.json ([#14044](https://github.com/facebook/jest/pull/14044))

### Fixes

### Chore & Maintenance
Expand Down
10 changes: 10 additions & 0 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ Alternatively Jest's configuration can be defined through the `"jest"` key in th
}
```

Also Jest's configuration json file can be referenced through the `"jest"` key in the `package.json` of your project:

```json title="package.json"
{
"name": "my-project",
"jest": "<path_to_config_file.json>"
rafaelrabelos marked this conversation as resolved.
Show resolved Hide resolved
}
```

## Options

:::info
Expand Down Expand Up @@ -1177,6 +1186,7 @@ export default config;
```

We hope to support Prettier v3 seamlessly out of the box in a future version of Jest. See [this](https://github.com/jestjs/jest/issues/14305) tracking issue.

</details>

### `projects` \[array&lt;string | ProjectConfig&gt;]
Expand Down
51 changes: 51 additions & 0 deletions packages/jest-config/src/__tests__/resolveConfigPath.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,57 @@ describe.each(JEST_CONFIG_EXT_ORDER.slice(0))(
);
});

test('file path from "jest" key', () => {
// Arrange
rafaelrabelos marked this conversation as resolved.
Show resolved Hide resolved
const anyFileName = `anyJestConfigfile${extension}`;
const relativePackageJsonPath = 'a/b/c/package.json';
const relativeAnyFilePath = `a/b/c/conf/${anyFileName}`;
const absolutePackageJsonPath = path.resolve(
DIR,
relativePackageJsonPath,
);
const absoluteAnyFilePath = path.resolve(DIR, relativeAnyFilePath);

writeFiles(DIR, {
'a/b/c/package.json': `{ "jest": "conf/${anyFileName}" }`,
});
writeFiles(DIR, {[relativeAnyFilePath]: ''});

// Act
const result = resolveConfigPath(
path.dirname(absolutePackageJsonPath),
DIR,
);

// Assert
expect(result).toBe(absoluteAnyFilePath);
});

test('not a file path from "jest" key', () => {
// Arrange
const anyFileName = `anyJestConfigfile${extension}`;
const relativePackageJsonPath = 'a/b/c/package.json';
const relativeAnyFilePath = `a/b/c/conf/${anyFileName}`;
const absolutePackageJsonPath = path.resolve(
DIR,
relativePackageJsonPath,
);

writeFiles(DIR, {
'a/b/c/package.json': '{ "jest": {"verbose": true} }',
});
writeFiles(DIR, {[relativeAnyFilePath]: ''});

// Act
const result = resolveConfigPath(
path.dirname(absolutePackageJsonPath),
DIR,
);

// Assert
expect(result).toBe(absolutePackageJsonPath);
});

test(`directory path with "${extension}"`, () => {
const relativePackageJsonPath = 'a/b/c/package.json';
const absolutePackageJsonPath = path.resolve(
Expand Down
30 changes: 25 additions & 5 deletions packages/jest-config/src/resolveConfigPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,15 @@ const resolveConfigPathByTraversing = (
).filter(isFile);

const packageJson = findPackageJson(pathToResolve);
if (packageJson && hasPackageJsonJestKey(packageJson)) {
configFiles.push(packageJson);

if (packageJson) {
const packageContent = fs.readFileSync(packageJson, 'utf8');
const packagePath = path.dirname(packageJson);
const resolvedKey = resolveJestKey(packageContent, packagePath);

if (hasPackageJsonJestKey(packageContent)) {
configFiles.push(resolvedKey || packageJson);
}
}

if (!skipMultipleConfigError && configFiles.length > 1) {
Expand Down Expand Up @@ -111,10 +118,23 @@ const findPackageJson = (pathToResolve: string) => {
return undefined;
};

const hasPackageJsonJestKey = (packagePath: string) => {
const content = fs.readFileSync(packagePath, 'utf8');
const resolveJestKey = (packageContent: any, packagePath: string) => {
rafaelrabelos marked this conversation as resolved.
Show resolved Hide resolved
if (hasPackageJsonJestKey(packageContent)) {
const {jest} = JSON.parse(packageContent);
rafaelrabelos marked this conversation as resolved.
Show resolved Hide resolved

if (jest && typeof jest === 'string') {
const resolvedConfigFile = path.resolve(packagePath, jest);

return isFile(resolvedConfigFile) ? resolvedConfigFile : undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the path provided isn't a file, we should throw an error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, when jest key could not be resolved as a valid file path, the package.Json is assumed. That because jest key can be a path or a config object, so i need to allow that function to return for a second flow option.
I did a new test case to avoid future issues for those who changes that code. It makes sense?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That because jest key can be a path or a config object

My point is that if you in your config define "jest": "./some-path" (i.e. it is a string), and the path it resolves to is not a file, then Jest should error. Not ignore the entry.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, guess i understood the point: if path is not a config object and it is an string there is no option to it to not be a file, be a file or throw an error. Working on it for the next push commit

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, exactly 👍

}
}

return undefined;
};

const hasPackageJsonJestKey = (packageContent: any) => {
rafaelrabelos marked this conversation as resolved.
Show resolved Hide resolved
try {
return 'jest' in JSON.parse(content);
return 'jest' in JSON.parse(packageContent);
} catch {
// If package is not a valid JSON
return false;
Expand Down
1 change: 1 addition & 0 deletions website/versioned_docs/version-29.6/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,7 @@ export default config;
```

We hope to support Prettier v3 seamlessly out of the box in a future version of Jest. See [this](https://github.com/jestjs/jest/issues/14305) tracking issue.

</details>

### `projects` \[array&lt;string | ProjectConfig&gt;]
Expand Down