Skip to content

Commit 9a2c18a

Browse files
authored
fix(vitest): now import defineConfig from vitest/config (#703)
* manage alias * tooling improvement * working & test ok * add utils imports.find * add utils imports.remove * add changeset * linting is always a nice friend around. * Huuuuuu, looks great
1 parent 262ac80 commit 9a2c18a

File tree

15 files changed

+199
-37
lines changed

15 files changed

+199
-37
lines changed

.changeset/eager-schools-allow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'sv': patch
3+
---
4+
5+
fix(vitest): now import `defineConfig` from `vitest/config`

packages/addons/_tests/vitest/test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { execSync } from 'node:child_process';
22
import { setupTest } from '../_setup/suite.ts';
33
import vitest from '../../vitest-addon/index.ts';
4+
import path from 'node:path';
5+
import fs from 'node:fs';
46

57
const { test, variants } = setupTest({ vitest }, { browser: false });
68

@@ -12,4 +14,10 @@ test.concurrent.for(variants)('core - %s', async (variant, { expect, ...ctx }) =
1214
expect(() => execSync('pnpm exec playwright install chromium', { cwd })).not.toThrow();
1315

1416
expect(() => execSync('pnpm test', { cwd, stdio: 'pipe' })).not.toThrow();
17+
18+
const ext = variant.includes('ts') ? 'ts' : 'js';
19+
const viteFile = path.resolve(cwd, `vite.config.${ext}`);
20+
const viteContent = fs.readFileSync(viteFile, 'utf8');
21+
22+
expect(viteContent).toContain(`vitest/config`);
1523
});

packages/addons/vitest-addon/index.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { dedent, defineAddon, defineAddonOptions, log } from '@sveltejs/cli-core';
2-
import { array, exports, functions, object } from '@sveltejs/cli-core/js';
1+
import { dedent, defineAddon, defineAddonOptions } from '@sveltejs/cli-core';
2+
import { array, imports, object, vite } from '@sveltejs/cli-core/js';
33
import { parseJson, parseScript } from '@sveltejs/cli-core/parsers';
44

55
const options = defineAddonOptions()
@@ -96,6 +96,7 @@ export default defineAddon({
9696
`;
9797
});
9898
}
99+
99100
sv.file(viteConfigFile, (content) => {
100101
const { ast, generateCode } = parseScript(content);
101102

@@ -125,19 +126,9 @@ export default defineAddon({
125126
}
126127
});
127128

128-
const defineConfigFallback = functions.createCall({ name: 'defineConfig', args: [] });
129-
const { value: defineWorkspaceCall } = exports.createDefault(ast, {
130-
fallback: defineConfigFallback
131-
});
132-
if (defineWorkspaceCall.type !== 'CallExpression') {
133-
log.warn('Unexpected vite config. Could not update.');
134-
}
129+
const viteConfig = vite.getConfig(ast);
135130

136-
const vitestConfig = functions.getArgument(defineWorkspaceCall, {
137-
index: 0,
138-
fallback: object.create({})
139-
});
140-
const testObject = object.property(vitestConfig, {
131+
const testObject = object.property(viteConfig, {
141132
name: 'test',
142133
fallback: object.create({
143134
expect: {
@@ -154,6 +145,17 @@ export default defineAddon({
154145
if (componentTesting) array.append(workspaceArray, clientObjectExpression);
155146
if (unitTesting) array.append(workspaceArray, serverObjectExpression);
156147

148+
// Manage imports
149+
const importName = 'defineConfig';
150+
const { statement, alias } = imports.find(ast, { name: importName, from: 'vite' });
151+
if (statement) {
152+
// Switch the import from 'vite' to 'vitest/config' (keeping the alias)
153+
imports.addNamed(ast, { imports: { defineConfig: alias }, from: 'vitest/config' });
154+
155+
// Remove the old import
156+
imports.remove(ast, { name: importName, from: 'vite', statement });
157+
}
158+
157159
return generateCode();
158160
});
159161
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import { namedOne as namedOneAlias, namedOneAliasFound } from 'package';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { imports, type AstTypes } from '@sveltejs/cli-core/js';
2+
3+
export function run(ast: AstTypes.Program): void {
4+
imports.addNamed(ast, { from: 'package', imports: { namedOne: 'namedOneAlias' }, isType: false });
5+
6+
const result = imports.find(ast, { name: 'namedOne', from: 'package' });
7+
8+
if (result) {
9+
imports.addNamed(ast, {
10+
imports: [result.alias + 'Found'],
11+
from: 'package'
12+
});
13+
}
14+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { n1, n2 } from 'p1';
2+
import { n3 } from 'p3';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import { n1 } from 'p1';
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { imports, type AstTypes } from '@sveltejs/cli-core/js';
2+
3+
export function run(ast: AstTypes.Program): void {
4+
imports.remove(ast, { name: 'n2', from: 'p1' });
5+
imports.remove(ast, { name: 'n3', from: 'p3' });
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { sveltekit } from '@sveltejs/kit/vite';
2+
import { defineConfig as MyConf } from 'vite';
3+
4+
export default MyConf({
5+
plugins: [sveltekit()]
6+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import myPlugin from 'my-plugin';
2+
import { sveltekit } from '@sveltejs/kit/vite';
3+
import { defineConfig as MyConf } from 'vite';
4+
5+
export default MyConf({ plugins: [sveltekit(), myPlugin()] });

0 commit comments

Comments
 (0)