Skip to content

Exclude .d.ts files #55

Description

@meyfa

Problem statement

The default set of test extensions includes .ts, which also matches any .d.ts file. This causes an error at load time.

Show error message
# ts-node-test\\node_modules\\ts-node\\src\\index.ts:1085
#           throw new TypeError(
#                 ^
# TypeError: Unable to require file: test\\foo.d.ts
# This is usually the result of a faulty configuration or import. Make sure there is a `.js`, `.json` or other executable extension with loader attached before `ts-node` available.
#     at getOutput (ts-node-test\\node_modules\\ts-node\\src\\index.ts:1085:17)
#     at Object.compile (ts-node-test\\node_modules\\ts-node\\src\\index.ts:1433:41)
#     at transformSource (ts-node-test\\node_modules\\ts-node\\src\\esm.ts:400:37)
#     at ts-node-test\\node_modules\\ts-node\\src\\esm.ts:278:53
#     at async addShortCircuitFlag (ts-node-test\\node_modules\\ts-node\\src\\esm.ts:409:15)
#     at async nextLoad (node:internal/modules/esm/loader:163:22)
#     at async ESMLoader.load (node:internal/modules/esm/loader:605:20)
#     at async ESMLoader.moduleProvider (node:internal/modules/esm/loader:457:11)
#     at async link (node:internal/modules/esm/module_job:68:21)
# Subtest: test\\foo.d.ts
not ok 1 - test\\foo.d.ts
  ---
  duration_ms: 889.4078
  failureType: 'testCodeFailure'
  exitCode: 1
  error: 'test failed'
  code: 'ERR_TEST_FAILURE'
  ...

Proposed solution

I see two options:

  1. Hard-code a rule to not include .d.ts files in the match
  2. Add generic syntax for excluding extensions, and configure the default set of extensions to exclude .d.ts.

The latter would be preferred. An example how this could look:

const DEFAULT_TEST_EXTENSIONS = ['.js', '.mjs', '.cjs', '.ts', '.mts', '.cts', '!.d.ts', '!.d.mts', '!.d.cts']

Unfortunately, this doesn't play nice with TypeScript 5.0's allowArbitraryExtensions feature. Yet, this would likely be fine, as type declaration files for non-JS code inside of the test directory would be a rare occasion. Affected users could simply add e.g. !.d.css.ts to their TEST_EXTENSIONS environment variable.

Disadvantage: This increases parsing complexity significantly, and brings us near the realm of glob matching, without actually giving us glob matching.

Alternatives

I'm open for alternative suggestions.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions