使用 spec/test 文件夹设置 tsconfig

假设我将我的代码放在 src下,测试放在 spec下:

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

我想只传送 srcdist文件夹。由于 index.ts是我的包的入口点,我的 tsconfig.json看起来像这样:

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

但是,这个 tsconfig.json不包含测试文件,因此我无法解析它们中的依赖项。

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

How do I solve this problem?

77361 次浏览

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/**"
]
}

这样可以保留“ dist”文件夹中的原始结构,而不需要混合使用测试和 app js 文件:

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

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

假设我有两个文件: tsconfig.jsontsconfig.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.

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

这在某种程度上取决于您正在使用的测试框架,但是我喜欢使用 ts-node来编译我的测试文件。使用摩卡,您的 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/**"
]
}

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.

下面是一个管理源和测试的详细解决方案:

  • compilation includes sources and tests folders/files
  • Build 只包含源代码
  • IDE (VSCode,...)

配置

该解决方案是基于2个 tsconfig.json文件,正如在其他答案中提到的。

./tsconfig.json (用于编译和 IDE) :

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

The second ./tsconfig-build.json (used for build):

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

注意: 我们排除了以前包含的测试文件

Build

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

如果在 package.json中添加脚本,则为 npm run build:

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


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

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

对我来说,这是因为我的玩笑版本是26,而我的玩笑版本是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",
},
};