Skip to content

Commit

Permalink
add testFiles support (#1196)
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott authored Oct 8, 2020
1 parent 1faf063 commit f269fa6
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 6 deletions.
22 changes: 22 additions & 0 deletions docs/docs/10-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion snowpack/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion snowpack/src/commands/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
13 changes: 11 additions & 2 deletions snowpack/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SnowpackConfig> = {
exclude: ['__tests__/**/*', '**/*.@(spec|test).*'],
plugins: [],
alias: {},
scripts: {},
exclude: [],
installOptions: {},
devOptions: {
secure: false,
Expand All @@ -61,6 +61,9 @@ const DEFAULT_CONFIG: Partial<SnowpackConfig> = {
sourceMaps: false,
watch: false,
},
testOptions: {
files: ['__tests__/**/*', '**/*.@(spec|test).*'],
},
experiments: {
ssr: false,
},
Expand Down Expand Up @@ -149,6 +152,12 @@ const configSchema = {
ssr: {type: 'boolean'},
},
},
testOptions: {
type: 'object',
properties: {
files: {type: 'array', items: {type: 'string'}},
},
},
experiments: {
type: ['object'],
properties: {
Expand Down
8 changes: 6 additions & 2 deletions snowpack/src/scan-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<InstallTarget[]> {
export async function scanImports(
includeTests: boolean,
config: SnowpackConfig,
): Promise<InstallTarget[]> {
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,
Expand Down
3 changes: 3 additions & 0 deletions snowpack/src/types/snowpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down

1 comment on commit f269fa6

@vercel
Copy link

@vercel vercel bot commented on f269fa6 Oct 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.