diff --git a/docs/docs/10-reference.md b/docs/docs/10-reference.md index 6ea524f633..fc41d81814 100644 --- a/docs/docs/10-reference.md +++ b/docs/docs/10-reference.md @@ -109,6 +109,8 @@ Options: - Configure your dev server. See the section below for all options. - **`buildOptions.*`** - Configure your build. See the section below for all options. +- **`testOptions.*`** + - Configure your tests. See the section below for all options. #### `config.installOptions` @@ -223,6 +225,26 @@ Options: - **`buildOptions.webModulesUrl`** | `string` | Default: `web_modules` - Rename your web modules directory. +#### `config.testOptions` + +`object` (options) + +Settings that determine how the Snowpack test environment behaves. + +Example: + +```js +testOptions: { + files: ['my-test-dir/*.test.js']; +} +``` + +Options: + +- **`testOptions.files`** | `string[]` | Default: `["__tests__/**/*", "**/*.@(spec|test).*"]` + - The location of all test files. + - All matching test files are scanned for installable dependencies during development, but excluded from both scanning and building in your final build. + #### `config.proxy` `object` (path: options) diff --git a/snowpack/src/commands/build.ts b/snowpack/src/commands/build.ts index 0babcdb3d3..bf8ee03962 100644 --- a/snowpack/src/commands/build.ts +++ b/snowpack/src/commands/build.ts @@ -372,7 +372,7 @@ export async function command(commandOptions: CommandOptions) { // 0. Find all source files. for (const [mountedDir, mountEntry] of Object.entries(config.mount)) { const allFiles = glob.sync(`**/*`, { - ignore: config.exclude, + ignore: [...config.exclude, ...config.testOptions.files], cwd: mountedDir, absolute: true, nodir: true, diff --git a/snowpack/src/commands/install.ts b/snowpack/src/commands/install.ts index d5ac897202..dd1af50a38 100644 --- a/snowpack/src/commands/install.ts +++ b/snowpack/src/commands/install.ts @@ -27,7 +27,7 @@ export async function getInstallTargets( if (scannedFiles) { installTargets.push(...(await scanImportsFromFiles(scannedFiles, config))); } else { - installTargets.push(...(await scanImports(config))); + installTargets.push(...(await scanImports(process.env.NODE_ENV === 'test', config))); } return installTargets; } diff --git a/snowpack/src/config.ts b/snowpack/src/config.ts index c348360e71..5524b63c54 100644 --- a/snowpack/src/config.ts +++ b/snowpack/src/config.ts @@ -31,14 +31,14 @@ import { } from './util'; const CONFIG_NAME = 'snowpack'; -const ALWAYS_EXCLUDE = ['**/node_modules/**/*', '**/.types/**/*']; +const ALWAYS_EXCLUDE = ['**/node_modules/**/*', '**/web_modules/**/*', '**/.types/**/*']; // default settings const DEFAULT_CONFIG: Partial = { - exclude: ['__tests__/**/*', '**/*.@(spec|test).*'], plugins: [], alias: {}, scripts: {}, + exclude: [], installOptions: {}, devOptions: { secure: false, @@ -61,6 +61,9 @@ const DEFAULT_CONFIG: Partial = { sourceMaps: false, watch: false, }, + testOptions: { + files: ['__tests__/**/*', '**/*.@(spec|test).*'], + }, experiments: { ssr: false, }, @@ -149,6 +152,12 @@ const configSchema = { ssr: {type: 'boolean'}, }, }, + testOptions: { + type: 'object', + properties: { + files: {type: 'array', items: {type: 'string'}}, + }, + }, experiments: { type: ['object'], properties: { diff --git a/snowpack/src/scan-imports.ts b/snowpack/src/scan-imports.ts index 5da6a31ff5..f4ee2f63f3 100644 --- a/snowpack/src/scan-imports.ts +++ b/snowpack/src/scan-imports.ts @@ -226,12 +226,16 @@ export function scanDepList(depList: string[], cwd: string): InstallTarget[] { .reduce((flat, item) => flat.concat(item), []); } -export async function scanImports(config: SnowpackConfig): Promise { +export async function scanImports( + includeTests: boolean, + config: SnowpackConfig, +): Promise { await initESModuleLexer; + const ignore = includeTests ? config.exclude : [...config.exclude, ...config.testOptions.files]; const includeFileSets = await Promise.all( Object.keys(config.mount).map((fromDisk) => { return glob.sync(`**/*`, { - ignore: config.exclude.concat(['**/web_modules/**/*']), + ignore, cwd: fromDisk, absolute: true, nodir: true, diff --git a/snowpack/src/types/snowpack.ts b/snowpack/src/types/snowpack.ts index 638b7a22f6..82303d05f0 100644 --- a/snowpack/src/types/snowpack.ts +++ b/snowpack/src/types/snowpack.ts @@ -150,6 +150,9 @@ export interface SnowpackConfig { sourceMaps: boolean; watch: boolean; }; + testOptions: { + files: string[]; + }; /** EXPERIMENTAL - This section is experimental and not yet finalized. May change across minor versions. */ experiments: { /** (EXPERIMENTAL) If true, "snowpack build" should build your site for SSR. */