Skip to content

Commit

Permalink
feat: support printBasicPrototype (cherry-pick vitest-dev#6504)
Browse files Browse the repository at this point in the history
Update index.ts

Adding possibility to get rid of printing prototype in diff output

Update types.ts

Update config documentation to include diff.printBasicPrototype

Added a new configuration option `printBasicPrototype` to the diff section. This allows users to set the pretty-format option for the diff output, with a default value of `true`.

Update index.ts

CR Fixes

test: add test

chore: type error

chore: lint

test: fix threshold test

chore: lint
  • Loading branch information
spamshaker authored and hi-ogawa committed Nov 13, 2024
1 parent 7f3e4c4 commit 7aa803d
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 6 deletions.
7 changes: 7 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2364,6 +2364,13 @@ Annotation that is output at the end of diff result if it's truncated.
Color of truncate annotation, default is output with no color.
#### diff.printBasicPrototype
- **Type**: `boolean`
- **Default**: `true`
Allows to set pretty-format option printBasicPrototype for diff output
### fakeTimers
- **Type:** `FakeTimerInstallOpts`
Expand Down
3 changes: 2 additions & 1 deletion packages/utils/src/diff/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,12 @@ function getFormatOptions(
formatOptions: PrettyFormatOptions,
options?: DiffOptions,
): PrettyFormatOptions {
const { compareKeys } = normalizeDiffOptions(options)
const { compareKeys, printBasicPrototype } = normalizeDiffOptions(options)

return {
...formatOptions,
compareKeys,
printBasicPrototype,
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/diff/normalizeDiffOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function getDefaultOptions(): DiffOptionsNormalized {
includeChangeCounts: false,
omitAnnotationLines: false,
patchColor: c.yellow,
printBasicPrototype: true,
truncateThreshold: DIFF_TRUNCATE_THRESHOLD_DEFAULT,
truncateAnnotation: '... Diff result is truncated',
truncateAnnotationColor: noColor,
Expand Down
3 changes: 3 additions & 0 deletions packages/utils/src/diff/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface DiffOptions {
includeChangeCounts?: boolean
omitAnnotationLines?: boolean
patchColor?: DiffOptionsColor
printBasicPrototype?: boolean
compareKeys?: CompareKeys
truncateThreshold?: number
truncateAnnotation?: string
Expand All @@ -43,6 +44,7 @@ export interface SerializedDiffOptions {
expand?: boolean
includeChangeCounts?: boolean
omitAnnotationLines?: boolean
printBasicPrototype?: boolean
truncateThreshold?: number
truncateAnnotation?: string
}
Expand All @@ -66,6 +68,7 @@ export interface DiffOptionsNormalized {
includeChangeCounts: boolean
omitAnnotationLines: boolean
patchColor: DiffOptionsColor
printBasicPrototype: boolean
truncateThreshold: number
truncateAnnotation: string
truncateAnnotationColor: DiffOptionsColor
Expand Down
10 changes: 10 additions & 0 deletions test/config/fixtures/diff/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,13 @@ test('large diff', () => {
y[29] = 3000;
expect(x).toEqual(y)
})

test("printBasicPrototype", () => {
expect({
obj: { k: "foo" },
arr: [1, 2]
}).toEqual({
obj: { k: "bar" },
arr: [1, 3]
});
})
9 changes: 8 additions & 1 deletion test/config/fixtures/diff/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import {defineConfig} from 'vitest/config'

export default defineConfig({})
export default defineConfig({
test: {
diff: {
// expand: false,
// printBasicPrototype: false,
}
}
})
32 changes: 30 additions & 2 deletions test/config/test/__snapshots__/diff.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`inline diff options: { expand: false } 1`] = `
exports[`inline diff options: { expand: false, printBasicPrototype: false } 1`] = `
[
"- Expected
+ Received
@@ -1,7 +1,7 @@
Array [
[
- 1000,
+ 0,
1,
Expand Down Expand Up @@ -36,6 +36,20 @@ exports[`inline diff options: { expand: false } 1`] = `
- 3000,
+ 29,
]",
"- Expected
+ Received
{
"arr": [
1,
- 3,
+ 2,
],
"obj": {
- "k": "bar",
+ "k": "foo",
},
}",
]
`;

Expand Down Expand Up @@ -79,5 +93,19 @@ exports[`inline diff options: undefined 1`] = `
- 3000,
+ 29,
]",
"- Expected
+ Received
Object {
"arr": Array [
1,
- 3,
+ 2,
],
"obj": Object {
- "k": "bar",
+ "k": "foo",
},
}",
]
`;
2 changes: 1 addition & 1 deletion test/config/test/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { runVitest } from '../../test-utils'

test.for([
[undefined],
[{ expand: false }],
[{ expand: false, printBasicPrototype: false }],
])(`inline diff options: %o`, async ([options]) => {
const { ctx } = await runVitest({
root: './fixtures/diff',
Expand Down
4 changes: 3 additions & 1 deletion test/config/test/failures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@ test('coverage.autoUpdate cannot update thresholds when configuration file doesn
})

test('boolean flag 100 should not crash CLI', async () => {
const { stderr } = await runVitestCli('--coverage.enabled', '--coverage.thresholds.100')
let { stderr } = await runVitestCli('--coverage.enabled', '--coverage.thresholds.100')
// non-zero coverage shows up, which is non-deterministic, so strip it.
stderr = stderr.replace(/\([0-9.]+%\) does/g, '(0%) does')

expect(stderr).toMatch('ERROR: Coverage for lines (0%) does not meet global threshold (100%)')
expect(stderr).toMatch('ERROR: Coverage for functions (0%) does not meet global threshold (100%)')
Expand Down

0 comments on commit 7aa803d

Please sign in to comment.