【问题标题】:Setting up tsconfig with spec/test folder使用 spec/test 文件夹设置 tsconfig
【发布时间】:2016-05-29 23:20:14
【问题描述】:

假设我将代码放在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 文件夹中。

我该如何解决这个问题?

【问题讨论】:

标签: typescript visual-studio-code tsconfig


【解决方案1】:

我认为您不应该在配置中使用“文件”选项。相反,您可以排除不需要的文件并拥有它:

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

这将保留您在“dist”文件夹中的原始结构,而不会混合测试和应用 js 文件:

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

【讨论】:

  • 但他不希望测试文件最终出现在dist 文件夹中。这是合乎逻辑的,它们不适合出版。将它们放在子目录中并没有真正的帮助。我也想要我的项目文件,只有那些以lib/ 结尾的文件,test/ 下的文件保留在原处。如果测试文件的 .js 版本被移动到其他地方,我会有 两个 新问题:a)将它们从发布中排除,b)让测试运行者找到它们,当然可以解决,但它会越来越多黑客的顶峰。如果 tsc 可以只将测试文件存储在测试目录中,则没有必要。
【解决方案2】:

我最终定义了多个配置文件并使用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 language service (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": [ ... ]
 }

【讨论】:

【解决方案3】:

这在某种程度上取决于您使用的任何测试框架,但我喜欢使用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 配置文件。

【讨论】:

  • 不分成多个配置文件的缺点是在包中分发额外的(测试)文件。另外,您的两个include 是多余的。你只需要src/**/*.ts
【解决方案4】:

这是管理源和测试的详细解决方案:

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

配置

该解决方案基于其他答案中提到的 2 个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",
}

【讨论】:

    【解决方案5】:

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

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

    【讨论】:

    • 此设置是否允许在 VSCode 中进行智能感知和重构?
    • 在进行大型重构(更改源文件夹或规范所在位置)时,您需要手动更新此 tsconfig 文件。可能有一个 VSCode 插件可以检测这些变化,但我对此表示怀疑。
    • @AKd 不,要让 VSCode 自动导入/代码完成您的文件,它必须包含在相应的 tsconfig
    【解决方案6】:

    对我来说,这是因为我的 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",
        },
    };
    
    

    【讨论】:

      猜你喜欢
      • 2012-12-17
      • 1970-01-01
      • 2020-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-29
      • 2023-02-05
      相关资源
      最近更新 更多