-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
fix(jest-config): accept snapshotFormat.compareKeys
option
#12387
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
import {createHash} from 'crypto'; | ||
import path from 'path'; | ||
import semver = require('semver'); | ||
import type {SnapshotFormat} from '@jest/schemas'; | ||
import type {Config} from '@jest/types'; | ||
import {escapeStrForRegex} from 'jest-regex-util'; | ||
import Defaults from '../Defaults'; | ||
|
@@ -1918,3 +1919,20 @@ describe('updateSnapshot', () => { | |
Defaults.ci = defaultCiConfig; | ||
}); | ||
}); | ||
|
||
describe('snapshotFormat', () => { | ||
it('should accept custom `compareKeys`', async () => { | ||
const compareKeys = () => 0; | ||
|
||
const {options} = await normalize( | ||
{ | ||
ci: false, | ||
rootDir: '/root/', | ||
snapshotFormat: {compareKeys} as SnapshotFormat, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Those casts could be removed with such a patch: diff --git a/packages/jest-config/src/ValidConfig.ts b/packages/jest-config/src/ValidConfig.ts
index db7ea5fe6d..1a4348d5dc 100644
--- a/packages/jest-config/src/ValidConfig.ts
+++ b/packages/jest-config/src/ValidConfig.ts
@@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/
-import type {SnapshotFormat} from '@jest/schemas';
import type {Config} from '@jest/types';
import {replacePathSepForRegex} from 'jest-regex-util';
import {multipleValidOptions} from 'jest-validate';
@@ -113,8 +112,8 @@ const initialOptions: Config.InitialOptions = {
slowTestThreshold: 5,
snapshotFormat: {
...PRETTY_FORMAT_DEFAULTS,
- compareKeys: () => {},
- } as SnapshotFormat,
+ compareKeys: () => 0,
+ },
snapshotResolver: '<rootDir>/snapshotResolver.js',
snapshotSerializers: ['my-serializer-module'],
testEnvironment: 'jest-environment-node',
diff --git a/packages/jest-config/src/__tests__/normalize.test.ts b/packages/jest-config/src/__tests__/normalize.test.ts
index fb229b4e33..7b858656ee 100644
--- a/packages/jest-config/src/__tests__/normalize.test.ts
+++ b/packages/jest-config/src/__tests__/normalize.test.ts
@@ -9,7 +9,6 @@
import {createHash} from 'crypto';
import path from 'path';
import semver = require('semver');
-import type {SnapshotFormat} from '@jest/schemas';
import type {Config} from '@jest/types';
import {escapeStrForRegex} from 'jest-regex-util';
import Defaults from '../Defaults';
@@ -1928,11 +1927,11 @@ describe('snapshotFormat', () => {
{
ci: false,
rootDir: '/root/',
- snapshotFormat: {compareKeys} as SnapshotFormat,
+ snapshotFormat: {compareKeys},
},
{} as Config.Argv,
);
- expect((options.snapshotFormat as any).compareKeys).toBe(compareKeys);
+ expect(options.snapshotFormat.compareKeys).toBe(compareKeys);
});
});
diff --git a/packages/jest-schemas/src/index.ts b/packages/jest-schemas/src/index.ts
index b3f0a32eeb..09a31cff5b 100644
--- a/packages/jest-schemas/src/index.ts
+++ b/packages/jest-schemas/src/index.ts
@@ -10,6 +10,9 @@ import {Static, Type} from '@sinclair/typebox';
const RawSnapshotFormat = Type.Partial(
Type.Object({
callToJSON: Type.Readonly(Type.Boolean()),
+ compareKeys: Type.Readonly(
+ Type.Function([Type.String(), Type.String()], Type.Number()),
+ ),
escapeRegex: Type.Readonly(Type.Boolean()),
escapeString: Type.Readonly(Type.Boolean()),
highlight: Type.Readonly(Type.Boolean()), But I'm not sure if I should apply it because it feels like maybe |
||
}, | ||
{} as Config.Argv, | ||
); | ||
|
||
expect((options.snapshotFormat as any).compareKeys).toBe(compareKeys); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Am I proud of this fix? No.
Does it work? Yes.
I'm afraid that I won't have time to work on the improved fix though - this is somewhat problematic because this thing is handled through a very generic validation function and this case here is very specific.
If you have any suggestions on the improved design that could be implemented quickly I could take a stab at this but I'm worried that an improved architecture around this might not be a change for a couple of lines of code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably the long-run plan is to use recently added schemas for validation purposes? This would IMHO go outside of the scope of this PR as it's a bigger refactor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct 🙂