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(utils): support diff.printBasicPrototype, Adding possibility to get rid of prototypes from the output #6504

Closed
wants to merge 12 commits into from
8 changes: 8 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2297,6 +2297,7 @@ export default {
aIndicator: c.bold('--'),
bIndicator: c.bold('++'),
omitAnnotationLines: true,
printBasicPrototype: false,
} satisfies DiffOptions
```

Expand Down Expand Up @@ -2333,6 +2334,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
hi-ogawa marked this conversation as resolved.
Show resolved Hide resolved

### 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
2 changes: 2 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 @@ -51,6 +52,7 @@ export interface DiffOptionsNormalized {
includeChangeCounts: boolean
omitAnnotationLines: boolean
patchColor: DiffOptionsColor
printBasicPrototype: boolean
truncateThreshold: number
truncateAnnotation: string
truncateAnnotationColor: DiffOptionsColor
Expand Down
11 changes: 11 additions & 0 deletions test/config/fixtures/diff/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { expect, test } from 'vitest'

test("printBasicPrototype false", () => {
expect({
obj: { k: "foo" },
arr: [1, 2]
}).toEqual({
obj: { k: "bar" },
arr: [1, 3]
});
})
5 changes: 5 additions & 0 deletions test/config/fixtures/diff/diff.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { DiffOptions } from "vitest";

export default {
printBasicPrototype: false
} satisfies DiffOptions
7 changes: 7 additions & 0 deletions test/config/fixtures/diff/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {defineConfig} from 'vitest/config'

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

exports[`diff 1`] = `
[
"- Expected
+ Received

{
"arr": [
1,
- 3,
+ 2,
],
"obj": {
- "k": "bar",
+ "k": "foo",
},
}",
]
`;
15 changes: 15 additions & 0 deletions test/config/test/diff.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { stripVTControlCharacters } from 'node:util'
import { expect, test } from 'vitest'
import { runVitest } from '../../test-utils'

test('diff', async () => {
const { ctx } = await runVitest({
root: './fixtures/diff',
})
const errors = ctx!.state.getFiles().flatMap(f =>
f.tasks.flatMap(t => t.result?.errors ?? []),
)
expect(
errors.map(e => e.diff && stripVTControlCharacters(e.diff)),
).matchSnapshot()
})
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