@stencil/vitest 1.7.2 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -357,6 +357,38 @@ The `stencil-test` CLI supports most of Stencil's CLI options and all of Vitest
357
357
  - For full Stencil CLI options, see [Stencil CLI docs](https://stenciljs.com/docs/cli).
358
358
  - For full Vitest CLI options, see [Vitest CLI docs](https://vitest.dev/guide/cli.html).
359
359
 
360
+ ### Global Variables
361
+
362
+ The `stencil-test` CLI exposes global variables that can be accessed in your tests to check which CLI flags were used:
363
+
364
+ | Global | Type | Description |
365
+ | ------------------- | --------- | -------------------------------------- |
366
+ | `__STENCIL_PROD__` | `boolean` | `true` when `--prod` flag is passed |
367
+ | `__STENCIL_SERVE__` | `boolean` | `true` when `--serve` flag is passed |
368
+ | `__STENCIL_PORT__` | `string` | Port number when `--port` is specified |
369
+
370
+ ```tsx
371
+ if (__STENCIL_PROD__) {
372
+ console.log('Running tests against production build');
373
+ }
374
+
375
+ if (__STENCIL_SERVE__) {
376
+ const baseUrl = `http://localhost:${__STENCIL_PORT__ || '3333'}`;
377
+ }
378
+ ```
379
+
380
+ #### TypeScript Support
381
+
382
+ Add to your `tsconfig.json` for type definitions:
383
+
384
+ ```json
385
+ {
386
+ "compilerOptions": {
387
+ "types": ["@stencil/vitest/globals"]
388
+ }
389
+ }
390
+ ```
391
+
360
392
  ## License
361
393
 
362
394
  MIT
@@ -207,6 +207,10 @@ function runTests() {
207
207
  env: {
208
208
  ...process.env,
209
209
  NODE_ENV: 'test',
210
+ // Expose CLI flags to tests via environment variables
211
+ STENCIL_PROD: args.prod ? 'true' : '',
212
+ STENCIL_SERVE: args.serve ? 'true' : '',
213
+ STENCIL_PORT: args.port || '',
210
214
  },
211
215
  });
212
216
  vitestProcess.on('exit', (code) => {
package/dist/config.js CHANGED
@@ -124,6 +124,23 @@ function generateCoverageExcludes(testIncludes, srcDir) {
124
124
  function applyStencilDefaults(config, stencilConfig) {
125
125
  // Start with the user's config
126
126
  const result = { ...config };
127
+ // Expose STENCIL_* env vars to all test environments
128
+ // These are set by the stencil-test CLI when spawning vitest
129
+ const stencilEnvDefines = {
130
+ __STENCIL_PROD__: JSON.stringify(process.env.STENCIL_PROD === 'true'),
131
+ __STENCIL_SERVE__: JSON.stringify(process.env.STENCIL_SERVE === 'true'),
132
+ __STENCIL_PORT__: JSON.stringify(process.env.STENCIL_PORT || ''),
133
+ };
134
+ if (!result.define) {
135
+ result.define = stencilEnvDefines;
136
+ }
137
+ else {
138
+ // Merge with existing defines, user's defines take precedence
139
+ result.define = {
140
+ ...stencilEnvDefines,
141
+ ...result.define,
142
+ };
143
+ }
127
144
  // Add esbuild JSX config if not present
128
145
  if (!result.esbuild) {
129
146
  result.esbuild = {
@@ -307,6 +324,22 @@ function enhanceTestConfig(testConfig, stencilConfig) {
307
324
  */
308
325
  function enhanceProject(project, stencilConfig) {
309
326
  const enhanced = { ...project };
327
+ // Expose STENCIL_* env vars at the project level for browser tests
328
+ // This ensures the defines are available in all project types
329
+ const stencilEnvDefines = {
330
+ __STENCIL_PROD__: JSON.stringify(process.env.STENCIL_PROD === 'true'),
331
+ __STENCIL_SERVE__: JSON.stringify(process.env.STENCIL_SERVE === 'true'),
332
+ __STENCIL_PORT__: JSON.stringify(process.env.STENCIL_PORT || ''),
333
+ };
334
+ if (!enhanced.define) {
335
+ enhanced.define = stencilEnvDefines;
336
+ }
337
+ else {
338
+ enhanced.define = {
339
+ ...stencilEnvDefines,
340
+ ...enhanced.define,
341
+ };
342
+ }
310
343
  // Get output directories from Stencil config
311
344
  const outputDirs = getStencilOutputDirs(stencilConfig);
312
345
  const defaultExcludes = ['**/node_modules/**', ...outputDirs.map((dir) => `**/${dir}/**`)];
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Global variables injected by stencil-test CLI via Vite's define config.
3
+ *
4
+ * Usage in tests:
5
+ * ```typescript
6
+ * if (__STENCIL_PROD__) {
7
+ * // Running with --prod flag
8
+ * }
9
+ * ```
10
+ *
11
+ * To get TypeScript support, add to your tsconfig.json:
12
+ * ```json
13
+ * {
14
+ * "compilerOptions": {
15
+ * "types": ["@stencil/vitest/globals"]
16
+ * }
17
+ * }
18
+ * ```
19
+ */
20
+
21
+ declare const __STENCIL_PROD__: boolean;
22
+ declare const __STENCIL_SERVE__: boolean;
23
+ declare const __STENCIL_PORT__: string;
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "type": "git",
5
5
  "url": "https://github.com/stenciljs/vitest"
6
6
  },
7
- "version": "1.7.2",
7
+ "version": "1.8.0",
8
8
  "description": "First-class testing utilities for Stencil design systems with Vitest",
9
9
  "license": "MIT",
10
10
  "type": "module",
@@ -43,6 +43,9 @@
43
43
  "./environments/stencil": {
44
44
  "types": "./dist/environments/stencil.d.ts",
45
45
  "import": "./dist/environments/stencil.js"
46
+ },
47
+ "./globals": {
48
+ "types": "./dist/globals.d.ts"
46
49
  }
47
50
  },
48
51
  "files": [
@@ -97,7 +100,7 @@
97
100
  "dependencies": {
98
101
  "jiti": "^2.6.1",
99
102
  "local-pkg": "^1.1.2",
100
- "vitest-environment-stencil": "1.7.2"
103
+ "vitest-environment-stencil": "1.8.0"
101
104
  },
102
105
  "devDependencies": {
103
106
  "@eslint/js": "^9.39.2",
@@ -122,7 +125,7 @@
122
125
  "node": "^20.0.0 || ^22.0.0 || >=24.0.0"
123
126
  },
124
127
  "scripts": {
125
- "build": "tsc && tsc -p tsconfig.bin.json",
128
+ "build": "tsc && tsc -p tsconfig.bin.json && cp src/globals.d.ts dist/globals.d.ts",
126
129
  "dev": "tsc --watch",
127
130
  "test:e2e": "pnpm --filter test-project test",
128
131
  "test:unit": "vitest run",