Skip to content

Commit

Permalink
Merge pull request #132 from atk/testing-config-no-overwrite
Browse files Browse the repository at this point in the history
testing config: do not overwrite user config
  • Loading branch information
ryansolid authored Jan 17, 2024
2 parents 2b37a13 + bdac54f commit 05ef3a5
Showing 1 changed file with 26 additions and 27 deletions.
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

0 comments on commit 05ef3a5

Please sign in to comment.