@stencil/vitest 1.7.1 → 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
@@ -213,8 +213,8 @@ export default {
213
213
  writeFileSync(join(testDir, 'vitest.config.ts'), vitestConfig);
214
214
  writeFileSync(join(testDir, 'package.json'), JSON.stringify({ name: 'test', type: 'module' }));
215
215
  const result = await runCLIInDir(testDir, ['--watch', '--verbose'], 2000);
216
- // Should add watch ignore patterns (screenshot and test file patterns)
217
- expect(result.stdout).toMatch(/Added.*screenshot patterns and.*test file patterns/);
216
+ // Should add watch ignore patterns (screenshot, test file, and generated file patterns)
217
+ expect(result.stdout).toMatch(/Added.*screenshot.*test file.*generated file patterns/);
218
218
  });
219
219
  it('should merge user watchIgnoredRegex with screenshot patterns', async () => {
220
220
  const stencilConfig = `
@@ -383,8 +383,8 @@ export default {
383
383
  writeFileSync(join(testDir, 'vitest.config.ts'), vitestConfig);
384
384
  writeFileSync(join(testDir, 'package.json'), JSON.stringify({ name: 'test', type: 'module' }));
385
385
  const result = await runCLIInDir(testDir, ['--watch', '--verbose'], 2000);
386
- // Should report both screenshot and test file pattern counts
387
- expect(result.stdout).toMatch(/Added.*screenshot patterns and.*test file patterns/);
386
+ // Should report screenshot, test file, and generated file pattern counts
387
+ expect(result.stdout).toMatch(/Added.*screenshot.*test file.*generated file patterns/);
388
388
  });
389
389
  });
390
390
  describe('temp file cleanup', () => {
@@ -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) => {
@@ -449,10 +453,19 @@ async function createTemporaryStencilConfig(userSpecifiedConfig, vitestConfigPat
449
453
  const userConfig = await jiti.import(userConfigPath);
450
454
  // Extract the actual config object
451
455
  const actualConfig = userConfig.config || userConfig.default?.config || userConfig.default || userConfig;
456
+ // Patterns for Stencil-generated files that shouldn't trigger rebuilds
457
+ const stencilGeneratedPatterns = [
458
+ /readme\.md$/i, // Stencil generates readme.md files in --prod mode
459
+ ];
452
460
  // Merge with watchIgnoredRegex
453
461
  const mergedConfig = {
454
462
  ...actualConfig,
455
- watchIgnoredRegex: [...(actualConfig?.watchIgnoredRegex || []), ...screenshotPatterns, ...testFilePatterns],
463
+ watchIgnoredRegex: [
464
+ ...(actualConfig?.watchIgnoredRegex || []),
465
+ ...screenshotPatterns,
466
+ ...testFilePatterns,
467
+ ...stencilGeneratedPatterns,
468
+ ],
456
469
  };
457
470
  // Create temp file as sibling of stencil.config so tsconfig.json can be found
458
471
  // Stencil looks for tsconfig.json relative to the config file location
@@ -483,7 +496,7 @@ async function createTemporaryStencilConfig(userSpecifiedConfig, vitestConfigPat
483
496
  writeFileSync(tempConfigPath, tempConfigContent, 'utf-8');
484
497
  if (verbose) {
485
498
  log(`Created temporary stencil config at ${tempConfigPath}`);
486
- log(`Added ${screenshotPatterns.length} screenshot patterns and ${testFilePatterns.length} test file patterns to watch ignore`);
499
+ log(`Added ${screenshotPatterns.length} screenshot, ${testFilePatterns.length} test file, and ${stencilGeneratedPatterns.length} generated file patterns to watch ignore`);
487
500
  }
488
501
  return tempConfigPath;
489
502
  }
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.1",
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.1"
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",