Skip to content

Commit

Permalink
invert if
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel3108 committed Nov 15, 2024
1 parent 1f5c62e commit 6bf4bec
Showing 1 changed file with 60 additions and 60 deletions.
120 changes: 60 additions & 60 deletions packages/addons/vitest-addon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,66 @@ export default defineAddon({
`;
});

if (kit) {
if (!kit) {
sv.file(`vite.config.${ext}`, (content) => {
const { ast, generateCode } = parseScript(content);

// find `defineConfig` import declaration for "vite"
const importDecls = ast.body.filter((n) => n.type === 'ImportDeclaration');
const defineConfigImportDecl = importDecls.find(
(importDecl) =>
(importDecl.source.value === 'vite' || importDecl.source.value === 'vitest/config') &&
importDecl.importKind === 'value' &&
importDecl.specifiers?.some(
(specifier) =>
specifier.type === 'ImportSpecifier' && specifier.imported.name === 'defineConfig'
)
);

// we'll need to replace the "vite" import for a "vitest/config" import.
// if `defineConfig` is the only specifier in that "vite" import, remove the entire import declaration
if (defineConfigImportDecl?.specifiers?.length === 1) {
const idxToRemove = ast.body.indexOf(defineConfigImportDecl);
ast.body.splice(idxToRemove, 1);
} else {
// otherwise, just remove the `defineConfig` specifier
const idxToRemove = defineConfigImportDecl?.specifiers?.findIndex(
(s) => s.type === 'ImportSpecifier' && s.imported.name === 'defineConfig'
);
if (idxToRemove) defineConfigImportDecl?.specifiers?.splice(idxToRemove, 1);
}

const config = common.expressionFromString('defineConfig({})');
const defaultExport = exports.defaultExport(ast, config);

const test = object.create({
include: common.expressionFromString("['src/**/*.{test,spec}.{js,ts}']")
});

// uses the `defineConfig` helper
if (
defaultExport.value.type === 'CallExpression' &&
defaultExport.value.arguments[0]?.type === 'ObjectExpression'
) {
// if the previous `defineConfig` was aliased, reuse the alias for the "vitest/config" import
const importSpecifier = defineConfigImportDecl?.specifiers?.find(
(sp) => sp.type === 'ImportSpecifier' && sp.imported.name === 'defineConfig'
);
const defineConfigAlias = (importSpecifier?.local?.name ?? 'defineConfig') as string;
imports.addNamed(ast, 'vitest/config', { defineConfig: defineConfigAlias });

object.properties(defaultExport.value.arguments[0], { test });
} else if (defaultExport.value.type === 'ObjectExpression') {
// if the config is just an object expression, just add the property
object.properties(defaultExport.value, { test });
} else {
// unexpected config shape
log.warn('Unexpected vite config for vitest add-on. Could not update.');
}

return generateCode();
});
} else {
sv.devDependency('@testing-library/svelte', '^5.2.4');
sv.devDependency('@testing-library/jest-dom', '^6.6.3');
sv.devDependency('jsdom', '^25.0.1');
Expand Down Expand Up @@ -114,65 +173,6 @@ export default defineAddon({
array.push(workspaceArray, clientObjectExpression);
array.push(workspaceArray, serverObjectExpression);

return generateCode();
});
} else {
sv.file(`vite.config.${ext}`, (content) => {
const { ast, generateCode } = parseScript(content);

// find `defineConfig` import declaration for "vite"
const importDecls = ast.body.filter((n) => n.type === 'ImportDeclaration');
const defineConfigImportDecl = importDecls.find(
(importDecl) =>
(importDecl.source.value === 'vite' || importDecl.source.value === 'vitest/config') &&
importDecl.importKind === 'value' &&
importDecl.specifiers?.some(
(specifier) =>
specifier.type === 'ImportSpecifier' && specifier.imported.name === 'defineConfig'
)
);

// we'll need to replace the "vite" import for a "vitest/config" import.
// if `defineConfig` is the only specifier in that "vite" import, remove the entire import declaration
if (defineConfigImportDecl?.specifiers?.length === 1) {
const idxToRemove = ast.body.indexOf(defineConfigImportDecl);
ast.body.splice(idxToRemove, 1);
} else {
// otherwise, just remove the `defineConfig` specifier
const idxToRemove = defineConfigImportDecl?.specifiers?.findIndex(
(s) => s.type === 'ImportSpecifier' && s.imported.name === 'defineConfig'
);
if (idxToRemove) defineConfigImportDecl?.specifiers?.splice(idxToRemove, 1);
}

const config = common.expressionFromString('defineConfig({})');
const defaultExport = exports.defaultExport(ast, config);

const test = object.create({
include: common.expressionFromString("['src/**/*.{test,spec}.{js,ts}']")
});

// uses the `defineConfig` helper
if (
defaultExport.value.type === 'CallExpression' &&
defaultExport.value.arguments[0]?.type === 'ObjectExpression'
) {
// if the previous `defineConfig` was aliased, reuse the alias for the "vitest/config" import
const importSpecifier = defineConfigImportDecl?.specifiers?.find(
(sp) => sp.type === 'ImportSpecifier' && sp.imported.name === 'defineConfig'
);
const defineConfigAlias = (importSpecifier?.local?.name ?? 'defineConfig') as string;
imports.addNamed(ast, 'vitest/config', { defineConfig: defineConfigAlias });

object.properties(defaultExport.value.arguments[0], { test });
} else if (defaultExport.value.type === 'ObjectExpression') {
// if the config is just an object expression, just add the property
object.properties(defaultExport.value, { test });
} else {
// unexpected config shape
log.warn('Unexpected vite config for vitest add-on. Could not update.');
}

return generateCode();
});
}
Expand Down

0 comments on commit 6bf4bec

Please sign in to comment.