Skip to content

Commit

Permalink
feat: add option to exclude files from the tests glob
Browse files Browse the repository at this point in the history
Fixes: #32
  • Loading branch information
thetutlage committed Nov 22, 2023
1 parent a9f9d55 commit dad63db
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/config_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export class ConfigManager {

const baseConfig: NormalizedBaseConfig = {
cwd: this.#config.cwd ?? process.cwd(),
exclude: this.#config.exclude || [],
filters: Object.assign({}, this.#config.filters ?? {}, cliFilters),
importer: this.#config.importer ?? DEFAULTS.importer,
refiner: this.#config.refiner ?? new Refiner(),
Expand Down
3 changes: 2 additions & 1 deletion src/files_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ export class FilesManager {
* Returns a collection of files from the user defined
* glob or the implementation function
*/
async getFiles(cwd: string, files: TestFiles): Promise<URL[]> {
async getFiles(cwd: string, files: TestFiles, excludes: string[]): Promise<URL[]> {
if (Array.isArray(files) || typeof files === 'string') {
const testFiles = await fastGlob(files, {
absolute: true,
onlyFiles: true,
cwd: cwd,
ignore: excludes,
})
return testFiles.map((file) => pathToFileURL(file))
}
Expand Down
2 changes: 1 addition & 1 deletion src/planner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class Planner {
* files glob and apply the files filter
*/
async #collectFiles(files: TestFiles) {
let filesURLs = await this.#fileManager.getFiles(this.#config.cwd, files)
let filesURLs = await this.#fileManager.getFiles(this.#config.cwd, files, this.#config.exclude)
if (this.#config.filters.files && this.#config.filters.files.length) {
filesURLs = this.#fileManager.grep(filesURLs, this.#config.filters.files)
}
Expand Down
9 changes: 9 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ export type BaseConfig = {
* Global hooks to execute on teardown
*/
teardown?: TeardownHookHandler[]

/**
* An array of directories to exclude when searching
* for test files.
*
* For example, if you search for test files inside the entire
* project, you might want to exclude "node_modules"
*/
exclude?: string[]
}

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/config_manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const USER_DEFINED_CONFIG_DATASET: [Config, Config][] = [
cwd: process.cwd(),
files: [],
filters: {},
exclude: [],
forceExit: false,
refiner: new Refiner(),
reporters: {
Expand All @@ -50,6 +51,7 @@ const USER_DEFINED_CONFIG_DATASET: [Config, Config][] = [
cwd: process.cwd(),
files: ['tests/unit/**.spec.ts'],
filters: {},
exclude: [],
forceExit: false,
refiner: new Refiner(),
reporters: {
Expand All @@ -72,6 +74,7 @@ const USER_DEFINED_CONFIG_DATASET: [Config, Config][] = [
cwd: process.cwd(),
files: ['tests/unit/**.spec.ts'],
filters: {},
exclude: [],
forceExit: false,
refiner: new Refiner(),
reporters: {
Expand All @@ -95,6 +98,7 @@ const USER_DEFINED_CONFIG_DATASET: [Config, Config][] = [
cwd: process.cwd(),
files: ['tests/unit/**.spec.ts'],
filters: {},
exclude: [],
forceExit: false,
refiner: new Refiner(),
reporters: {
Expand All @@ -118,6 +122,7 @@ const USER_DEFINED_CONFIG_DATASET: [Config, Config][] = [
cwd: process.cwd(),
files: ['tests/unit/**.spec.ts'],
filters: {},
exclude: [],
forceExit: false,
refiner: new Refiner(),
reporters: {
Expand Down Expand Up @@ -148,6 +153,7 @@ const USER_DEFINED_CONFIG_DATASET: [Config, Config][] = [
tags: ['@integration', '~@slow'],
tests: ['users list'],
},
exclude: [],
forceExit: false,
refiner: new Refiner(),
reporters: {
Expand Down Expand Up @@ -179,6 +185,7 @@ const USER_DEFINED_CONFIG_DATASET: [Config, Config][] = [
tags: ['@integration', '~@slow'],
tests: ['users list'],
},
exclude: [],
forceExit: true,
refiner: new Refiner(),
reporters: {
Expand All @@ -204,6 +211,7 @@ const USER_DEFINED_CONFIG_DATASET: [Config, Config][] = [
cwd: process.cwd(),
files: [],
filters: {},
exclude: [],
forceExit: false,
refiner: new Refiner(),
reporters: {
Expand All @@ -228,6 +236,7 @@ const USER_DEFINED_CONFIG_DATASET: [Config, Config][] = [
cwd: process.cwd(),
files: [],
filters: {},
exclude: [],
forceExit: false,
refiner: new Refiner(),
reporters: {
Expand All @@ -253,6 +262,7 @@ const USER_DEFINED_CONFIG_DATASET_WITH_CLI_ARGS: [Config, CLIArgs, Config][] = [
cwd: process.cwd(),
files: [],
filters: {},
exclude: [],
forceExit: false,
refiner: new Refiner(),
reporters: {
Expand All @@ -277,6 +287,7 @@ const USER_DEFINED_CONFIG_DATASET_WITH_CLI_ARGS: [Config, CLIArgs, Config][] = [
filters: {
tags: ['@slow'],
},
exclude: [],
forceExit: false,
refiner: new Refiner(),
reporters: {
Expand All @@ -300,6 +311,7 @@ const USER_DEFINED_CONFIG_DATASET_WITH_CLI_ARGS: [Config, CLIArgs, Config][] = [
cwd: process.cwd(),
files: ['tests/unit/**.spec.ts'],
filters: {},
exclude: [],
forceExit: false,
refiner: new Refiner(),
reporters: {
Expand Down Expand Up @@ -337,6 +349,7 @@ const USER_DEFINED_CONFIG_DATASET_WITH_CLI_ARGS: [Config, CLIArgs, Config][] = [
},
],
filters: {},
exclude: [],
forceExit: false,
refiner: new Refiner(),
reporters: {
Expand All @@ -361,6 +374,7 @@ const USER_DEFINED_CONFIG_DATASET_WITH_CLI_ARGS: [Config, CLIArgs, Config][] = [
cwd: process.cwd(),
files: ['tests/unit/**.spec.ts'],
filters: {},
exclude: [],
forceExit: false,
refiner: new Refiner(),
reporters: {
Expand Down Expand Up @@ -398,6 +412,7 @@ const USER_DEFINED_CONFIG_DATASET_WITH_CLI_ARGS: [Config, CLIArgs, Config][] = [
},
],
filters: {},
exclude: [],
forceExit: false,
refiner: new Refiner(),
reporters: {
Expand All @@ -422,6 +437,7 @@ const USER_DEFINED_CONFIG_DATASET_WITH_CLI_ARGS: [Config, CLIArgs, Config][] = [
cwd: process.cwd(),
files: ['tests/unit/**.spec.ts'],
filters: {},
exclude: [],
forceExit: false,
refiner: new Refiner(),
reporters: {
Expand Down Expand Up @@ -453,6 +469,7 @@ const USER_DEFINED_CONFIG_DATASET_WITH_CLI_ARGS: [Config, CLIArgs, Config][] = [
tags: ['@slow'],
tests: ['users list'],
},
exclude: [],
forceExit: false,
refiner: new Refiner(),
reporters: {
Expand Down Expand Up @@ -485,6 +502,7 @@ const USER_DEFINED_CONFIG_DATASET_WITH_CLI_ARGS: [Config, CLIArgs, Config][] = [
tags: ['@integration', '~@slow'],
tests: ['users'],
},
exclude: [],
forceExit: true,
refiner: new Refiner(),
reporters: {
Expand Down Expand Up @@ -517,6 +535,7 @@ const USER_DEFINED_CONFIG_DATASET_WITH_CLI_ARGS: [Config, CLIArgs, Config][] = [
tags: ['@integration', '~@slow'],
groups: ['customers'],
},
exclude: [],
forceExit: true,
refiner: new Refiner(),
reporters: {
Expand Down Expand Up @@ -549,6 +568,7 @@ const USER_DEFINED_CONFIG_DATASET_WITH_CLI_ARGS: [Config, CLIArgs, Config][] = [
tags: ['@integration', '~@slow'],
files: ['*'],
},
exclude: [],
forceExit: true,
refiner: new Refiner(),
reporters: {
Expand Down Expand Up @@ -583,6 +603,7 @@ const USER_DEFINED_CONFIG_DATASET_WITH_CLI_ARGS: [Config, CLIArgs, Config][] = [
tests: ['users list'],
suites: ['unit', 'functional'],
},
exclude: [],
forceExit: true,
refiner: new Refiner(),
reporters: {
Expand All @@ -609,6 +630,7 @@ const USER_DEFINED_CONFIG_DATASET_WITH_CLI_ARGS: [Config, CLIArgs, Config][] = [
cwd: process.cwd(),
files: [],
filters: {},
exclude: [],
forceExit: false,
refiner: new Refiner(),
reporters: {
Expand All @@ -635,6 +657,7 @@ const USER_DEFINED_CONFIG_DATASET_WITH_CLI_ARGS: [Config, CLIArgs, Config][] = [
cwd: process.cwd(),
files: [],
filters: {},
exclude: [],
forceExit: true,
refiner: new Refiner(),
reporters: {
Expand All @@ -661,6 +684,7 @@ const USER_DEFINED_CONFIG_DATASET_WITH_CLI_ARGS: [Config, CLIArgs, Config][] = [
cwd: process.cwd(),
files: [],
filters: {},
exclude: [],
forceExit: true,
refiner: new Refiner(),
reporters: {
Expand All @@ -687,6 +711,7 @@ const USER_DEFINED_CONFIG_DATASET_WITH_CLI_ARGS: [Config, CLIArgs, Config][] = [
cwd: process.cwd(),
files: [],
filters: {},
exclude: [],
forceExit: false,
refiner: new Refiner(),
reporters: {
Expand Down
46 changes: 36 additions & 10 deletions tests/files_manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ test.describe('Files manager | grep', () => {
test.describe('Files manager | getFiles', () => {
test('get files for the glob pattern', async () => {
const cwd = new URL('../', import.meta.url)
const files = await new FilesManager().getFiles(fileURLToPath(cwd), ['tests/**/*.spec.ts'])
const files = await new FilesManager().getFiles(fileURLToPath(cwd), ['tests/**/*.spec.ts'], [])

await wrapAssertions(() => {
assert.deepEqual(files, [
Expand All @@ -119,10 +119,11 @@ test.describe('Files manager | getFiles', () => {

test('get files from multiple glob patterns', async () => {
const cwd = new URL('../', import.meta.url)
const files = await new FilesManager().getFiles(fileURLToPath(cwd), [
'tests/**/*.spec.ts',
'modules/**/*.ts',
])
const files = await new FilesManager().getFiles(
fileURLToPath(cwd),
['tests/**/*.spec.ts', 'modules/**/*.ts'],
[]
)

await wrapAssertions(() => {
assert.deepEqual(files, [
Expand All @@ -140,16 +141,41 @@ test.describe('Files manager | getFiles', () => {
})
})

test('get files from a custom implementation', async () => {
test('ignore files using the exclude pattern', async () => {
const cwd = new URL('../', import.meta.url)
const files = await new FilesManager().getFiles(fileURLToPath(cwd), () => {
return [
const files = await new FilesManager().getFiles(
fileURLToPath(cwd),
['tests/**/*.spec.ts', 'modules/**/*.ts'],
['modules/**']
)

await wrapAssertions(() => {
assert.deepEqual(files, [
new URL('tests/base_reporter.spec.ts', cwd),
new URL('tests/cli_parser.spec.ts', cwd),
new URL('tests/config_manager.spec.ts', cwd),
new URL('tests/core.spec.ts', cwd),
new URL('tests/files_manager.spec.ts', cwd),
new URL('modules/core/main.ts', cwd),
]
new URL('tests/planner.spec.ts', cwd),
new URL('tests/runner.spec.ts', cwd),
])
})
})

test('get files from a custom implementation', async () => {
const cwd = new URL('../', import.meta.url)
const files = await new FilesManager().getFiles(
fileURLToPath(cwd),
() => {
return [
new URL('tests/cli_parser.spec.ts', cwd),
new URL('tests/config_manager.spec.ts', cwd),
new URL('tests/files_manager.spec.ts', cwd),
new URL('modules/core/main.ts', cwd),
]
},
[]
)

await wrapAssertions(() => {
assert.deepEqual(files, [
Expand Down

0 comments on commit dad63db

Please sign in to comment.