microsoft / TypeScript-Sublime-Plugin

IO wrapper around TypeScript language services, allowing for easy consumption by editor plugins
Apache License 2.0
1.72k stars 235 forks source link

Exclude test files from compilation but include for plugin processing #605

Closed ernieturner closed 7 years ago

ernieturner commented 7 years ago

My tsconfig file currently looks like this

{
    "compilerOptions": {
        "alwaysStrict": true,
        "noImplicitAny": true,
        "noImplicitThis": true,
        "strictNullChecks": true,
        "noUnusedParameters": true,
        "noEmitOnError": true,
        "noUnusedLocals": true,
        "declaration": false,
        "target": "es2015",
        "baseUrl": ".",
        "paths": {
            "*": [
                "src/*"
            ]
        },
        "moduleResolution": "node",
        "lib": ["es6", "dom"]
    },
    "exclude": [
        "node_modules",
        "dist",
        "**/tests/*.test.ts",
        "src/tests"
    ]
}

And my code is setup where my unit test files are located in a tests directory as a sibling of the source file, e.g.

/lib
   /tests
      Utils.test.ts
  Utils.ts

When I compile my code the current setup works find and ignores all unit test files during compilation. However, when I open the unit test files in Sublime with this configuration it can't find any of the global types that it should find from jasmine/karma/etc.

screen shot 2017-05-04 at 3 31 09 pm

If I remove the unit test file from the exclude list in tsconfig, then it all starts working again.

Is there a way to exclude files from TS compilation but still have the plugin process them correctly?

billti commented 7 years ago

You will need a separate tsconfig.json that includes your test files along with the necessary compiler options and files to build the appropriate context. They are excluded in the above project, so TypeScript will effectively treat them as standalone files when opened.

fossecode commented 4 years ago

It the problem only applies for jest, you can add this to your jest config:

"jest": {
    "testPathIgnorePatterns": [
      "<rootDir>/build/",
      "<rootDir>/node_modules/"
    ]
}
AverageHelper commented 4 years ago

For more info on working with multiple tsconfig.json files, see this StackOverflow answer. Basically you'd make use of the "extends" field so as to not duplicate your compiler options overmuch.

You may need to change how you run tsc just a tad:

tsc --project tsconfig.production.json # or whatever you call your other tsconfig