配置tsconfig与spec/test文件夹的设置

132

假设我把我的代码放在 src 文件夹下,而测试文件放在 spec 文件夹下:

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

我只想将src转译到dist文件夹中。由于index.ts是我的包的入口点,所以我的tsconfig.json看起来像这样:

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

然而,这个tsconfig.json并没有包含测试文件,所以我无法解决它们的依赖关系。

另一方面,如果我将测试文件包含在tsconfig.json中,它们也会被转译到dist文件夹中。

我该如何解决这个问题?


类似的问题和(相当优雅的)解答在这里:https://dev59.com/Jbroa4cB1Zd3GeqPdBSc#61153019。 - AKd
6个回答

98

我最终定义了多个配置文件,并使用extends来简化它们。

比方说,我有两个文件:tsconfig.jsontsconfig.build.json

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

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

这样一来,我就可以精细地控制使用什么(使用tsc -p tsconfig.build.json)以及何时让ts语言服务(IDE)处理。

更新:随着我的项目越来越大,我最终拥有了更多的配置文件。我现在使用 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": [ ... ]
 }

2
现在或许值得使用Typescript 3的项目引用功能,具体请参考https://dev59.com/FVUK5IYBdhLWcg3wlQoJ。 - Jedateach
1
那更适用于 monorepo 项目引用。我在我的 monorepos 中使用它,例如 https://github.com/unional/standard-log - unional

62

以下是管理源代码和测试的详细解决方案:

  • 编译包括源代码和测试文件夹/文件
  • 构建仅包括源代码
  • IDE (VSCode, ...)

配置

此解决方案基于其他答案中提到的两个tsconfig.json文件。

主要的./tsconfig.json文件(用于编译和IDE):

{
  "compileOptions": {
    "module": "commonjs"
    "outDir": "dist"
  },
  "include": [
    "spec/**/*.spec.ts"
  ],
  "files": [
    "src/index.ts"
  ]
}

第二个./tsconfig-build.json(用于构建):

{
  "extends": "./tsconfig.json",
  "exclude": [
    "spec/**/*.spec.ts"
  ]
}

注意:我们排除了已经包含过的测试文件。

构建

构建命令:tsc -p tsconfig-build.json

或者如果在package.json中添加了脚本,可以用npm run build

{
  "scripts": {
    "build": "tsc -p tsconfig-build.json",
}


18

这在某种程度上取决于你使用的测试框架,但我喜欢使用ts-node来编译我的测试文件。如果你正在使用mocha,则你的npm test脚本可能如下所示:

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

请确保在您的 tsconfig.json 文件中移除 rootDir 选项。

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "noImplicitAny": false,
        "removeComments": true,
        "sourceMap": true,
        "outDir": "lib"
    },
    "include": [
        "src/**/*.ts"
    ],
    "exclude": [
        "node_modules",
        "lib",
        "typings/**"
    ]
}
当您尝试将 TypeScript 的 rootDir 设置为 src 或您的应用程序代码的基本文件夹时,它将禁止在位于外部目录(如tests)的任何目录中进行编译。使用 ts-node,您可以轻松地将所有内容保持分离,而不必拥有单独的 TypeScript 配置文件。

3
不将配置文件拆分为多个文件的缺点是在软件包中分发了额外的(测试)文件。另外,你的两个“include”是冗余的,只需要使用src/**/*.ts即可。 - unional

2

我认为你不应该在配置中使用“files”选项。相反,你可以排除不需要的文件,让它像这样:

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

这将在“dist”文件夹中保留您的原始结构,而不会混淆测试和应用程序js文件:
--dist
----spec
-------....
----src
-------....

11
但他不希望测试文件最终在dist文件夹中出现,这很合理,它们不是用于发布的。将它们放在子目录中并没有真正帮助。我也希望只有我的项目文件最终出现在lib/中,而test/下的文件仍然保持原样。如果测试文件的.js版本被移动到其他地方,我将面临两个新问题:a) 排除它们不被发布,b) 使测试运行程序找到它们,当然可以解决,但这会增加额外的麻烦。如果tsc能够将测试文件存储在测试目录中,那就不必这样了。 - Mörre

2

只需添加要编译和包含在构建中的源文件的包含目录。接下来,在tsconfig.json中指定要排除的目录。对于您的用例,没有必要使用多个tsconfig文件。

{
  "include": [ "src/**/*" ],
  "exclude": [ "./spec" ]
}

2
这个设置能在VSCode中启用智能感知和重构吗? - AKd
当你进行大规模重构(更改源文件夹或规范所在位置)时,你需要手动更新此tsconfig文件。可能有VSCode插件可以检测这些更改,但我怀疑。 - Clayton Selby
@AKd 不,要让VSCode自动导入/代码完成您的文件,它必须包含在相应的tsconfig中。 - Guilherme Matuella

0

对我来说,这是因为我的 Jest 版本是 26,而我的 ts-jest 版本是 27,所以它们不同步。

yarn jest --version

yarn add ts-jest@26

我的jest.config.js

module.exports = {
    preset: "ts-jest",
    moduleFileExtensions: ["ts", "tsx", "js", "jsx"],
    transform: {
        "^.+\\.tsx?$": "ts-jest",
    },
    globals: {
        "ts-jest": {
            diagnostics: false,
        },
    },
    testMatch: ["**/*.(test|spec).(ts|tsx|js|jsx)"],
    coveragePathIgnorePatterns: ["/node_modules/"],
    coverageReporters: ["json", "lcov", "text", "text-summary"],
    transformIgnorePatterns: [
        "<rootDir>/node_modules/(?!(firebase/.*|react/.*)/)",
    ],
    testEnvironment: "jest-environment-jsdom-sixteen",
    moduleNameMapper: {
        "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$":
            "<rootDir>/__mocks__/mocks.js",
        "\\.(css|less|scss)$": "<rootDir>/__mocks__/mocks.js",
        "^src/(.*)": "<rootDir>/src/$1",
    },
};


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接