Skip to content

Commit

Permalink
refactoring after code review
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Lohr committed Jan 17, 2024
1 parent fc23a15 commit bdac54f
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,20 @@ function containsSolidField(fields: Record<string, any>) {
return false;
}

function getJestDomExport(test: { setupFiles: string[] }) {
return (test.setupFiles || []).some((path) => /jest-dom/.test(path))
? undefined
: ['@testing-library/jest-dom/vitest', '@testing-library/jest-dom/extend-expect'].find(
(path) => {
try {
require(path);
} catch (e) {
return false;
}
},
);
}

export default function solidPlugin(options: Partial<Options> = {}): Plugin {
const filter = createFilter(options.include, options.exclude);

Expand Down Expand Up @@ -318,19 +332,7 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin {
if (!test.environment && !options.ssr) {
test.environment = 'jsdom';
}
if (!test.transformMode) {
test.transformMode = { [options.ssr ? 'ssr' : 'web']: [/\.[jt]sx?$/] };
}
const jestDomImport = [
'@testing-library/jest-dom/vitest',
'@testing-library/jest-dom/extend-expect',
].find((path) => {
try {
require(path);
} catch (e) {
return false;
}
});
const jestDomImport = getJestDomExport(test);
if (jestDomImport) {
test.setupFiles = [...(test.setupFiles || []), require.resolve(jestDomImport)];
}
Expand Down

0 comments on commit bdac54f

Please sign in to comment.