Skip to content

Commit d7186ec

Browse files
authored
Merge pull request #136 from atk/inline-solid-for-testing
inline solid-js for testing, fix jest-dom detect
2 parents a381ec3 + 05ace26 commit d7186ec

File tree

4 files changed

+146
-13
lines changed

4 files changed

+146
-13
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ if (import.meta.hot) {
188188
189189
- If one of your dependency spit out React code instead of Solid that means that they don't expose JSX properly. To get around it, you might want to manually exclude it from the [dependencies optimization](https://vitejs.dev/config/dep-optimization-options.html#optimizedeps-exclude)
190190
191-
- If you are trying to make [directives](https://www.solidjs.com/docs/latest/api#use%3A___) work and they somehow don't try setting the `options.typescript.onlyRemoveTypeImports` option to `true`
191+
- If you are trying to make [directives](https://www.solidjs.com/docs/latest/api#use%3A___) work, and they somehow don't try setting the `options.typescript.onlyRemoveTypeImports` option to `true`
192192
193193
## Migration from v1
194194
@@ -198,6 +198,10 @@ The main breaking change from previous version is that the package has been rena
198198
199199
For other breaking changes, check [the migration guide of vite](https://vitejs.dev/guide/migration.html).
200200
201+
# Testing
202+
203+
If you are using [vitest](https://vitest.dev/), this plugin already injects the necessary configuration for you. It even automatically detects if you have `@testing-library/jest-dom` installed in your project and automatically adds it to the `setupFiles`. All you need to add (if you want) is `globals`, `coverage`, and other testing configuration of your choice. If you can live without those, enjoy using vitest without the need to configure it yourself.
204+
201205
# Credits
202206
203207
- [solid-js](https://github.com/solidjs/solid)

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vite-plugin-solid",
3-
"version": "2.8.2",
3+
"version": "2.8.3",
44
"description": "solid-js integration plugin for vite 3/4/5",
55
"type": "module",
66
"files": [
@@ -65,8 +65,12 @@
6565
"vite": "^5.0.0"
6666
},
6767
"peerDependencies": {
68+
"@testing-library/jest-dom": "^5.16.6 || ^5.17.0 || ^6.*",
6869
"solid-js": "^1.7.2",
6970
"vite": "^3.0.0 || ^4.0.0 || ^5.0.0"
7071
},
72+
"peerDependencyMeta": {
73+
"@testing-library/jest-dom": { "optional": true }
74+
},
7175
"packageManager": "[email protected]"
7276
}

pnpm-lock.yaml

Lines changed: 121 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -280,12 +280,13 @@ function containsSolidField(fields: Record<string, any>) {
280280
}
281281

282282
function getJestDomExport(setupFiles: string[]) {
283-
return (setupFiles || []).some((path) => /jest-dom/.test(path))
283+
return setupFiles?.some((path) => /jest-dom/.test(path))
284284
? undefined
285285
: ['@testing-library/jest-dom/vitest', '@testing-library/jest-dom/extend-expect'].find(
286286
(path) => {
287287
try {
288-
require(path);
288+
require.resolve(path);
289+
return true;
289290
} catch (e) {
290291
return false;
291292
}
@@ -328,19 +329,24 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin {
328329

329330
const test = (userConfig as any).test || {};
330331

331-
// to simplify the processing of the config, we normalize the setupFiles to an array
332-
const userSetupFiles: string[] =
333-
typeof test.setupFiles === 'string' ? [test.setupFiles] : test.setupFiles || [];
334-
335332
if (userConfig.mode === 'test') {
333+
// to simplify the processing of the config, we normalize the setupFiles to an array
334+
const userSetupFiles: string[] =
335+
typeof test.setupFiles === 'string' ? [test.setupFiles] : test.setupFiles || [];
336+
336337
if (!test.environment && !options.ssr) {
337338
test.environment = 'jsdom';
338339
}
339340

340-
const jestDomImport = getJestDomExport(userSetupFiles);
341+
test.server = test.server || {};
342+
test.server.deps = test.server.deps || {};
343+
if (!test.server.deps.inline?.find((item: string | RegExp) => /solid-js/.test(item.toString()))) {
344+
test.server.deps.inline = [...(test.server.deps.inline || []), /solid-js/];
345+
}
341346

347+
const jestDomImport = getJestDomExport(userSetupFiles);
342348
if (jestDomImport) {
343-
test.setupFiles = [...userSetupFiles, require.resolve(jestDomImport)];
349+
test.setupFiles = [...userSetupFiles, jestDomImport];
344350
}
345351
}
346352

@@ -364,7 +370,7 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin {
364370
exclude: solidPkgsConfig.optimizeDeps.exclude,
365371
},
366372
ssr: solidPkgsConfig.ssr,
367-
...(test ? { test } : {}),
373+
...(test.server ? { test } : {}),
368374
};
369375
},
370376

0 commit comments

Comments
 (0)