Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ Toggle individual features (all enabled by default):
| `replaceHomeDir` | `true` | `os.homedir()` paths → `<HOME>` |
| `transformWin32Path` | `true` | Convert `D:\\foo\\bar` to `/d/foo/bar` |
| `transformCLR` | `true` | Normalize ANSI color escape codes |
| `normalizeTime` | `true` | Normalize durations in seconds: `1.5s` → `Xs`, `12 s` → `X s` |
| `replaceDuration` | `true` | Replace durations in seconds: `1.5s` → `Xs`, `12 s` → `X s` |
| `escapeDoubleQuotes` | `true` | Escape `"` to `\"` |
| `escapeEOL` | `true` | Normalize `\r\n` to `\n` |
| `addDoubleQuotes` | `true` | Wrap output in double quotes |
Expand Down
6 changes: 3 additions & 3 deletions e2e/normalizeTime.test.ts → e2e/replaceDuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ test.each([
[false, false, '\u001b[1m\u001b[33m1.5\u001b[39m\u001b[22m s'],
] as const)(
'should independently control color (%s) and time (%s) normalization',
(transformCLR, normalizeTime, expected) => {
(transformCLR, replaceDuration, expected) => {
const serializer = createSnapshotSerializer({
features: { transformCLR, normalizeTime, addDoubleQuotes: false },
features: { transformCLR, replaceDuration, addDoubleQuotes: false },
});
expect(
serializer.serialize('\u001b[1m\u001b[33m1.5\u001b[39m\u001b[22m s'),
).toBe(expected);
expect(serializer.serialize('built in 1.5s')).toBe(
normalizeTime ? 'built in Xs' : 'built in 1.5s',
replaceDuration ? 'built in Xs' : 'built in 1.5s',
);
},
);
Expand Down
8 changes: 4 additions & 4 deletions src/createSnapshotSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
normalizeCLR,
normalizeCodeToPosix,
normalizePathToPosix,
normalizeTime,
replaceDuration,
} from './normalize';
import { replacePnpmInnerPath } from './pnpm';
import type { PathMatcher, SnapshotSerializerOptions } from './types';
Expand Down Expand Up @@ -41,7 +41,7 @@ export function createSnapshotSerializer(
escapeDoubleQuotes = true,
escapeEOL = true,
transformCLR = true,
normalizeTime: normalizeTimeEnabled = true,
replaceDuration: replaceDurationEnabled = true,
} = features;

function createPathMatchers(): PathMatcher[] {
Expand Down Expand Up @@ -110,8 +110,8 @@ export function createSnapshotSerializer(
replaced = normalizeCLR(replaced);
}

if (normalizeTimeEnabled) {
replaced = normalizeTime(replaced);
if (replaceDurationEnabled) {
replaced = replaceDuration(replaced);
}

if (escapeDoubleQuotes) {
Expand Down
12 changes: 6 additions & 6 deletions src/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
normalizeCLR,
normalizeCodeToPosix,
normalizePathToPosix,
normalizeTime,
replaceDuration,
} from './normalize';

describe('normalizePathToPosix', () => {
Expand Down Expand Up @@ -141,10 +141,10 @@ describe('normalizeCLR', () => {
});
});

describe('normalizeTime', () => {
describe('replaceDuration', () => {
test('should preserve long numeric strings without time units', () => {
for (const input of ['0'.repeat(50_000), '0.'.repeat(25_000)]) {
expect(normalizeTime(input)).toBe(input);
expect(replaceDuration(input)).toBe(input);
}
});

Expand All @@ -155,7 +155,7 @@ describe('normalizeTime', () => {
['built in 1500 ms', 'built in 1500 ms'],
['received 3 msgs', 'received 3 msgs'],
])('should preserve units and color markers in %s', (input, expected) => {
expect(normalizeTime(input)).toBe(expected);
expect(replaceDuration(input)).toBe(expected);
});

test('should replace time values with "X" followed by time unit', () => {
Expand Down Expand Up @@ -201,7 +201,7 @@ describe('normalizeTime', () => {
];

for (const { input, expected } of testCases) {
expect(normalizeTime(input)).toBe(expected);
expect(replaceDuration(input)).toBe(expected);
}
});

Expand Down Expand Up @@ -246,7 +246,7 @@ describe('normalizeTime', () => {
];

for (const { input, expected } of testCases) {
expect(normalizeTime(input)).toBe(expected);
expect(replaceDuration(input)).toBe(expected);
}
});
});
2 changes: 1 addition & 1 deletion src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const normalizeCLR = (str: string): string => {
.replace(/\u001b\[([0-9;]*)m/g, '<CLR=$1>');
};

export const normalizeTime = (str: string): string => {
export const replaceDuration = (str: string): string => {
// Preserve raw ANSI codes and normalized color markers between the value
// and unit so time normalization works independently of color normalization.
const unit = /(?:<\/CLR>|<CLR=[0-9;]*>|\u001b\[[0-9;]*m)*\s?s\b/y;
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ export interface Features {
transformCLR?: boolean;
/**
* `built in 1.5s` -> `built in Xs`
* Normalize durations in seconds independently of `transformCLR`.
* Replace durations in seconds independently of `transformCLR`.
* @default true
*/
normalizeTime?: boolean;
replaceDuration?: boolean;
/**
* `""` -> `\"\"`
* @default true
Expand Down
Loading