Setting up tsconfig with spec/test folder

Related searches

Say I put my code under src and tests under spec:

+ spec
+ --- classA.spec.ts
+ src
+ --- classA.ts
+ --- classB.ts
+ --- index.ts
+ tsconfig.json

I want to only transpile src to the dist folder. Since index.ts is the entry point of my package, my tsconfig.json look like this:

{
  "compileOptions": {
    "module": "commonjs"
    "outDir": "dist"
  },
  "files": {
    "src/index.ts",
    "typings/main.d.ts"
  }
}

However, this tsconfig.json does not include the test files so I could not resolve dependencies in them.

On the other hand, if I include the test files into tsconfig.json then they are also transpiled to dist folder.

How do I solve this problem?


I ended up defining multiple config files and use extends to simplify them.

Say I have two files: tsconfig.json and tsconfig.build.json

// tsconfig.json
{
  ...
  "exclude": [...]
}

// tsconfig.build.json
{
  ...
  "files": [ "typings/index.d.ts", "src/index.ts" ]
}

This way, I can have fine control on what to build (using tsc -p tsconfig.build.json) and what the ts language service (IDE) handles.

UPDATE: now as my projects grow, I ended up having more config files. I use the "extend" feature that is now available in TypeScript:

// tsconfig.base.json
{
  // your common settings. Mostly "compilerOptions".
  // Do not include "files" and "include" here,
  // let individual config handles that.
  // You can use "exclude" here, but with "include",
  // It's pretty much not necessary.
}

// tsconfig.json
{
  // This is used by `ts language service` and testing.
  // Includes source and test files.
  "extends": "./tsconfig.base.json",
  "atom": { ... },
  "compilerOptions": {
    // I set outDir to place all test build in one place,
    // and avoid accidentally running `tsc` littering test build to my `src` folder.
    "outDir": "out/spec"  
  }
  "include": [ ... ]
}

// tsconfig.commonjs.json or tsconfig.systemjs.json or tsconfig.global.json etc
{
  "extends": "./tsconfig.base.json",
  "compilerOptions": {
    // for some build this does not apply
    "declaration": true/false,
    "outDir": "dist/<cjs, sys, global, etc>",
    "sourceRoot": "..."
  },
  // Only point to typings and the start of your source, e.g. `src/index.ts`
  "files": [ ... ],
  "include": [ ... ]
 }

tsconfig.json for project with `src` and `tests`, test/ --compilers ts:ts-node/register --recursive". In tsconfig.json, do "include": [ " src/**/*.ts" ],. See Setting up tsconfig with spec/test folder. Create tsconfig.json. Before you start using TypeScript, make sure you have installed it. To use conveniently, install it as global dependency so that you can use tsc command from console window. Then use 'tsc --init' command to create tsconfig.json file in your project’s root folder. Create tsconfig.json.


This is somewhat dependent on whatever testing framework you're using but I like to use ts-node to compile my test files. Using mocha, your npm test script might look like:

"mocha": "mocha test/ --compilers ts:ts-node/register --recursive"

In your tsconfig.json, make sure to remove the rootDir option.

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "noImplicitAny": false,
        "removeComments": true,
        "sourceMap": true,
        "outDir": "lib"
    },
    "include": [
        "src/**/*.ts"
    ],
    "exclude": [
        "node_modules",
        "lib",
        "typings/**"
    ]
}

When you try to run typescript with rootDir set to src or whatever the base folder for your application code is, it'll disallow any compilation in a directory that sits outside, such a tests. Using ts-node, you can easily keep everything separate without having to have separate TypeScript configuration files.

Recommend in the doc usage of tsconfig.json and tsconfig.build , I am not sure if this is a configuration error or an issue with ts-jest. You have a tsconfig which exclude all spec and test files, so your IDE, which is using expected ts-jest config:init to get jest types wired up and not double run tests. references, you MUST have a tsconfig.json in each sub-projects folder. Setting up tsconfig with spec/test folder (2) I ended up defining multiple config files and use extends to simplify them. Say I have two files: tsconfig.json and tsconfig.build.json


I think you should not use 'files' option in your config. Instead you can exclude unwanted files and have it like this:

{ 
    "compilerOptions": { 
        "module": "commonjs", 
        "outDir": "dist"
    },
    "exclude": [
        "node_modules",
        "dist",
        "typings/browser.d.ts",
        "typings/browser/**"
    ]
} 

This will preserve your original structure in the 'dist' folder without mixing tests and app js files:

--dist
----spec
-------....
----src
-------....

Handbook - tsconfig.json, The most important part is to do setting up so that test cases written in Place Source JS/TS files in src folder and tests typescript files in tests folder. where we are matching ts/tsx files ending with test/spec under tests folder. Using your layout, I can use 2 separate tsconfig files (I was trying with 1 global tsconfig at the root.) However 'test', which has a reference to files in 'lib' will auto-generate its own folder in the outDir. The tsconfig in 'lib' will compile files directly to outDir (unless I add reference paths to any of those files I guess.)


Writing unit tests in TypeScript. In this story, we would be using , "mocha": "mocha test/ --compilers ts:ts-node/register --recursive". In tsconfig.json, do "include": [ "src/**/*.ts" ],. See Setting up tsconfig with spec/test folder. Project� The glob patterns Jest uses to detect test files. By default it looks for .js, .jsx, .ts and .tsx files inside of __tests__ folders, as well as any files with a suffix of .test or .spec (e.g. Component.test.js or Component.spec.js). It will also find files called test.js or spec.js. See the micromatch package for details of the patterns you can


tsconfig.json for project with `src` and `tests`, According to the tsconfig schema: "If no 'files' property is present in a tsconfig.json, the compiler defaults to including all files the containing directory and subdirectories. When a 'files' property is specified, only those files are included."


Manually select the run settings file. In the IDE, select Test > Configure Run Settings > Select Solution Wide runsettings File, and then select the .runsettings file. This file overrides the .runsettings file at the root of the solution, if one is present, and is applied across all tests run.


Also, for me, with this guide, I've had my VSCode failing to recognize test and expect functions in *.ts files, showing errors, even though it worked overall with npm run test, passing these 2 tests. What helped me is to remove test files (here: "**/*.spec.ts" ) from "exclude" prop in tsconfig.json .