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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "test-inline-snapshot-indent",
"private": true
}
23 changes: 23 additions & 0 deletions packages/cli/snap-tests/test-inline-snapshot-indent/snap.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
> vp test run -u src/inline-snapshot.test.ts # write inline snapshot via --update (regression test for #1553)
RUN <cwd>

✓ src/inline-snapshot.test.ts (1 test) <variable>ms

Snapshots 1 written
Test Files 1 passed (1)
Tests 1 passed (1)
Start at <date>
Duration <variable>ms (transform <variable>ms, setup <variable>ms, import <variable>ms, tests <variable>ms, environment <variable>ms)


> cat src/inline-snapshot.test.ts # snapshot must use 2-space indentation, not tabs
import { describe, expect, it } from 'vite-plus/test';

describe('inline snapshot indentation', () => {
it('writes multiline snapshots using the surrounding file indentation style', () => {
expect('alpha\nbeta').toMatchInlineSnapshot(`
"alpha
beta"
`);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { describe, expect, it } from 'vite-plus/test';

describe('inline snapshot indentation', () => {
it('writes multiline snapshots using the surrounding file indentation style', () => {
expect('alpha\nbeta').toMatchInlineSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"ignoredPlatforms": ["win32"],
"commands": [
"vp test run -u src/inline-snapshot.test.ts # write inline snapshot via --update (regression test for #1553)",
"cat src/inline-snapshot.test.ts # snapshot must use 2-space indentation, not tabs"
]
}
21 changes: 21 additions & 0 deletions packages/test/__tests__/build-artifacts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@ describe('build artifacts', () => {
});
});

/**
* `convertTabsToSpaces()` in build.ts must not touch tabs inside string
* literals. Upstream `@vitest/snapshot` decides multi-line snapshot
* indentation via `indent.includes("\t")` — where `"\t"` is a literal
* tab byte in the bundled source. A blanket tab→spaces rewrite turned
* this into `indent.includes(" ")`, so every 2-space indent matched
* and the tab-appending branch always ran, producing tab-indented
* snapshots in 2-space files.
*
* See: https://github.com/voidzero-dev/vite-plus/issues/1553
*/
describe('snapshot indent check (regression test for #1553)', () => {
const snapshotIndexPath = path.join(distDir, '@vitest/snapshot/index.js');

it('preserves the literal tab byte inside the indent.includes string', () => {
const content = fs.readFileSync(snapshotIndexPath, 'utf-8');
expect(content).toContain('indent.includes("\t")');
expect(content).not.toMatch(/indent\.includes\(" "\)/);
});
});

/**
* Third-party packages that call `expect.extend()` internally
* (e.g., @testing-library/jest-dom) break under npm override because
Expand Down
18 changes: 13 additions & 5 deletions packages/test/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1365,18 +1365,26 @@ async function patchVitestCoreResolver() {
}

/**
* Convert tabs to spaces in all JS files in dist/ for consistent formatting.
* This allows our patching code to use space-based patterns instead of tabs.
* Convert leading tabs to spaces in all JS files in dist/ for consistent
* formatting. This allows our patching code to use space-based patterns
* instead of tabs.
*
* Only leading whitespace is rewritten — tabs inside string or template
* literals are semantically meaningful (e.g. `indent.includes("\t")` in
* @vitest/snapshot picks the snapshot indent style by checking for a
* literal tab byte) and must be preserved.
*
* See: https://github.com/voidzero-dev/vite-plus/issues/1553
*/
async function convertTabsToSpaces() {
console.log('\nConverting tabs to spaces in dist/...');
console.log('\nConverting leading tabs to spaces in dist/...');

let convertedCount = 0;

for await (const file of fsGlob(resolve(distDir, '**/*.js'))) {
const content = await readFile(file, 'utf-8');
if (content.includes('\t')) {
const converted = content.replace(/\t/g, ' ');
const converted = content.replace(/^\t+/gm, (match) => ' '.repeat(match.length));
if (converted !== content) {
await writeFile(file, converted);
convertedCount++;
}
Expand Down
Loading