Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing config: do not overwrite user config #132

Merged
merged 2 commits into from
Jan 17, 2024
Merged
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
53 changes: 26 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,6 @@ export interface Options {
* @default {}
*/
solid: {


/**
* Removed unnecessary closing tags from template strings. More info here:
* https://github.com/solidjs/solid/blob/main/CHANGELOG.md#smaller-templates
Expand Down Expand Up @@ -270,7 +268,7 @@ function getExtension(filename: string): string {
const index = filename.lastIndexOf('.');
return index < 0 ? '' : filename.substring(index).replace(/\?.+$/, '');
}
function containsSolidField(fields) {
function containsSolidField(fields: Record<string, any>) {
const keys = Object.keys(fields);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
Expand All @@ -280,14 +278,19 @@ function containsSolidField(fields) {
}
return false;
}
function isJestDomInstalled() {
try {
// attempt to reference a file that will not throw error because expect is missing
require('@testing-library/jest-dom/dist/utils');
return true;
} catch (e) {
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 {
Expand Down Expand Up @@ -323,21 +326,17 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin {
? ['solid-js', 'solid-js/web', 'solid-js/store', 'solid-js/html', 'solid-js/h']
: [];

const test =
userConfig.mode === 'test'
? {
test: {
globals: true,
...(options.ssr ? {} : { environment: 'jsdom' }),
transformMode: {
[options.ssr ? 'ssr' : 'web']: [/\.[jt]sx?$/],
},
...(isJestDomInstalled()
? { setupFiles: [require.resolve('@testing-library/jest-dom/extend-expect.js')] }
: {}),
},
}
: {};
const test = (userConfig as any).test || {};

if (userConfig.mode === 'test') {
if (!test.environment && !options.ssr) {
test.environment = 'jsdom';
}
const jestDomImport = getJestDomExport(test);
if (jestDomImport) {
test.setupFiles = [...(test.setupFiles || []), require.resolve(jestDomImport)];
}
}

return {
/**
Expand All @@ -359,7 +358,7 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin {
exclude: solidPkgsConfig.optimizeDeps.exclude,
},
ssr: solidPkgsConfig.ssr,
...test,
...(test ? { test } : {}),
};
},

Expand Down