首页 > 解决方案 > 如何配置 tsc 以从特定文件夹编译测试规范?

问题描述

我正在将 e2e 测试实施到 Angular 项目中。它不是以 开头的@angular-cli,所以我手动配置了大部分。

我现在要做的是定义一个脚本,package.json仅将tests/文件夹中的规范转换为tests/compiled.

我试图关注这个问题:tsconfig.json - Only build ts files from folder。推荐使用--rootDir来定义文件夹。

但是如果我使用 this: tsc --rootDir tests --outDir ./tests/compiled,它无论如何都会编译来自其他文件夹的文件(如 ./src)。此外,它返回很多 TS6059 错误,抱怨 rootDir 应该包含所有源文件。

一个例子: error TS6059: File 'C:/Users/Betalabs/Documents/Projetos/Engine-Frontend/src/vendor.browser.ts' is not under 'rootDir' 'tests'. 'rootDir' is expected to contain all source files.

文件层次结构是这样的。请注意,*.spec.ts我要转换的测试规范 ( ) 在tests/文件夹中。

(root)
-- src/
-- tests/
    -- compiled/
        -- (transpiled test files, ".js")
    -- helpers/
        -- (helper modules, ".ts")
    -- page-objects/
        -- (page objects, ".ts")
    -- forms.spec.ts
    -- filters.spec.ts
-- .gitignore
-- .tern-project
-- buildspec.yml
-- conf.js
-- package-lock.json
-- package.json
-- README.md
-- tsconfig.json
-- tsconfig.webpack.json
-- tslint.json
-- typedoc.json
-- webpack.config.js

这是我的 tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "noEmitHelpers": true,
    "importHelpers": true,
    "strictNullChecks": false,
    "baseUrl": "./src",
    "paths": {
      "@angular/*": ["node_modules/@angular/*"]
    },
    "lib": [
      "dom",
      "es6",
      "es2017.object"
    ],
    "typeRoots": [
      "node_modules/@types"
    ],
    "types": [
      "jasmine",
      "hammerjs",
      "node",
      "source-map",
      "uglify-js",
      "webpack"
    ]
  },
  "exclude": [
    "node_modules",
    "dist"
  ],
  "awesomeTypescriptLoaderOptions": {
    "forkChecker": true,
    "useWebpackText": true
  },
  "compileOnSave": false,
  "buildOnSave": false,
  "atom": { "rewriteTsconfig": false }
}

我通过编译所有文件并手动删除不必要的文件来“解决”这个问题。换句话说:一团糟:P

你能帮我解决这个问题吗?

标签: jsonangulartypescript

解决方案


为了编译这些文件,您可以使用

"include": ["tests"]

在你的 tsconfig


话虽如此,我认为将项目转换为使用 ng cli 会容易得多 :) 祝你好运


推荐阅读