使用 Chutzpah 的简单 TypeScript 测试不起作用
Simple TypeScript Testing with Chutzpah not Working
我正在尝试使用 Jasmine
和 Chutzpah
运行 最基本的 TypeScript
测试,但我无法让它工作。这是源文件:
class Simple {
public easyTest() {
return 5;
}
}
这是规范:
/// <reference path="../../../scripts/typings/jasmine/jasmine.d.ts" />
/// <reference path="../../../src/typescript/classSimple.ts" />
describe("Simple Calculations", () => {
it("should return true", () => {
var simpl = new Simple();
expect(simpl.easyTest()).toBe(5);
});
});
这是我的 chutzpah.json
文件:
{
"RootReferencePathMode": "SettingsFileDirectory",
"Framework": "jasmine",
"Compile": {
"Mode": "External",
"Extensions": [ ".ts" ],
"ExtensionsWithNoOutput": [ ".d.ts" ]
},
"References": [
{ "Path": "../../../src/typescript/classSimple.ts" }
],
"Tests": [
{ "Path": "simpleSpec.ts" }
],
"EnableTracing": true
}
这是文件夹结构
TestProject.sln
-src
--typescript
---classSimple.ts
-tests
--jasmine
---typescript
----chutzpah.json
----simpleSpec.ts
此 VS.NET 项目设置为在保存时编译 TypeScript
我正在查看转译后的 JavaScript
输出:classSimple.js
接下来在完全相同的目录中至 classSimple.ts
。然而,当我 运行 使用 Chutzpah
的规范时,我得到以下信息:
Message:Chutzpah run started in mode Execution with parallelism set to
4 Message:Building test context for
C:\dev\TestProject\tests\jasmine\typescript\simpleSpec.ts
Message:Building test context for
'C:\dev\TestProject\tests\jasmine\typescript\simpleSpec.ts'
Message:Test file
c:\dev\TestProject\tests\jasmine\typescript\simplespec.ts matched test
file path from settings file Message:Investigating reference file path
'../../../src/typescript/classSimple.ts' Message:Added file
'C:\dev\TestProject\src\typescript\classSimple.ts' to referenced
files. Local: True, IncludeInTestHarness: True Message:Processing
referenced file 'C:\dev\TestProject\src\typescript\classSimple.ts' for
expanded references Message:Added framework dependency
'C:\Users\myself\AppData\Local\Microsoft\VisualStudio.0\Extensions\mfv4e3ra.luv\TestFiles\jasmine\v2\jasmine_favicon.png' to referenced files Message:Added framework dependency
'C:\Users\myself\AppData\Local\Microsoft\VisualStudio.0\Extensions\mfv4e3ra.luv\TestFiles\jasmine\v2\boot.js'
to referenced files Message:Added framework dependency
'C:\Users\myself\AppData\Local\Microsoft\VisualStudio.0\Extensions\mfv4e3ra.luv\TestFiles\jasmine\v2\jasmine-html.js'
to referenced files Message:Added framework dependency
'C:\Users\myself\AppData\Local\Microsoft\VisualStudio.0\Extensions\mfv4e3ra.luv\TestFiles\jasmine\v2\jasmine.js'
to referenced files Message:Added framework dependency
'C:\Users\myself\AppData\Local\Microsoft\VisualStudio.0\Extensions\mfv4e3ra.luv\TestFiles\jasmine\v2\jasmine.css'
to referenced files Message:Added framework dependency
'C:\Users\myself\AppData\Local\Microsoft\VisualStudio.0\Extensions\mfv4e3ra.luv\TestFiles\chutzpah_boot.js'
to referenced files Message:Finished building test context for
C:\dev\TestProject\tests\jasmine\typescript\simpleSpec.ts Error: 0 :
Time:22:47:48.6888513; Thread:23; Message:Can't find location for
generated path on C:\dev\TestProject\src\typescript\classSimple.ts
Warning: 0 : Time:22:47:48.6978503; Thread:23; Message:Chutzpah
determined generated .js files are missing but the compile mode is
External so Chutzpah can't compile them. Test results may be wrong.
Information: 0 : Time:22:47:48.7038522; Thread:23; Message:Found
generated path for
C:\dev\TestProject\tests\jasmine\typescript\simpleSpec.ts at
c:\dev\TestProject\tests\jasmine\typescript\simpleSpec.js Error: 0 :
Time:22:47:48.7088508; Thread:23; Message:Couldn't find generated path
for C:\dev\TestProject\src\typescript\classSimple.ts at
就是这样 - 它只是在最后切断,没有任何反应。问题是,我看到 .js
文件 正好 在上面跟踪中所述的位置,但 Chutzpah 声称 Couldn't find generated path for ...
这对于单元测试来说非常简单。我知道测试独立于 .ts
文件工作,因为如果我 运行 只有 .js
文件测试通过。
我做错了什么?
感谢您发布非常详细的重现。你遇到了我见过几次的问题。由于您的 chutzpah.json 嵌套在您的测试文件夹中,您需要指定 chutzpah 应该使用的文件夹路径映射来确定如何从源映射到输出文件夹。为此,只需将 chutzpah.json 更改为:
{
"RootReferencePathMode": "SettingsFileDirectory",
"Framework": "jasmine",
"Compile": {
"Mode": "External",
"Extensions": [ ".ts" ],
"ExtensionsWithNoOutput": [ ".d.ts" ],
"Paths": [
{ "SourcePath": "../../../", "OutputPath": "../../../"}
]
},
"References": [
{ "Path": "../../../src/typescript/classSimple.ts" }
],
"Tests": [
{ "Path": "simpleSpec.ts" }
],
"EnableTracing": true,
"TraceFilePath": "trace.txt"
}
另一种解决方法是将 chutzpah.json 放在项目的根目录下。
我需要考虑改进 Chutzpah 以避免这种情况。我想我会考虑在这种情况下让 chutzpah 假设文件位于同一位置以避免这种常见的陷阱。
Google 把我带到这里来解决类似的问题:
我的 Chutzpah 项目中的一些新测试是用 TypeScript 编写并编译成单个 outfile
:
tsconfig.json
"compilerOptions": {
"noImplicitAny": true,
"target": "es5",
"module": "none",
"outFile": "./Tests/mytests-ts.js",
"sourceMap": true
}
Chutzpah 找不到 *.ts
个文件
Chutzpah Error: System.IO.FileNotFoundException: Couldn't find generated path for D:\Code\ChutzpahProject\Scripts\DateServiceMock.ts
直到我使用 Paths
选项:
chutzpah.json
"Compile": {
"Extensions": [ ".ts" ],
"ExtensionsWithNoOutput": [ ".d.ts" ],
"Executable": "build-ts.cmd",
"Paths": [
{ "OutputPath": "Tests/mytests-tests-ts.js" }
]
},
感觉这可能是对@matthew-manela 的回答的评论...我想。
我正在尝试使用 Jasmine
和 Chutzpah
运行 最基本的 TypeScript
测试,但我无法让它工作。这是源文件:
class Simple {
public easyTest() {
return 5;
}
}
这是规范:
/// <reference path="../../../scripts/typings/jasmine/jasmine.d.ts" />
/// <reference path="../../../src/typescript/classSimple.ts" />
describe("Simple Calculations", () => {
it("should return true", () => {
var simpl = new Simple();
expect(simpl.easyTest()).toBe(5);
});
});
这是我的 chutzpah.json
文件:
{
"RootReferencePathMode": "SettingsFileDirectory",
"Framework": "jasmine",
"Compile": {
"Mode": "External",
"Extensions": [ ".ts" ],
"ExtensionsWithNoOutput": [ ".d.ts" ]
},
"References": [
{ "Path": "../../../src/typescript/classSimple.ts" }
],
"Tests": [
{ "Path": "simpleSpec.ts" }
],
"EnableTracing": true
}
这是文件夹结构
TestProject.sln
-src
--typescript
---classSimple.ts
-tests
--jasmine
---typescript
----chutzpah.json
----simpleSpec.ts
此 VS.NET 项目设置为在保存时编译 TypeScript
我正在查看转译后的 JavaScript
输出:classSimple.js
接下来在完全相同的目录中至 classSimple.ts
。然而,当我 运行 使用 Chutzpah
的规范时,我得到以下信息:
Message:Chutzpah run started in mode Execution with parallelism set to 4 Message:Building test context for C:\dev\TestProject\tests\jasmine\typescript\simpleSpec.ts Message:Building test context for 'C:\dev\TestProject\tests\jasmine\typescript\simpleSpec.ts' Message:Test file c:\dev\TestProject\tests\jasmine\typescript\simplespec.ts matched test file path from settings file Message:Investigating reference file path '../../../src/typescript/classSimple.ts' Message:Added file 'C:\dev\TestProject\src\typescript\classSimple.ts' to referenced files. Local: True, IncludeInTestHarness: True Message:Processing referenced file 'C:\dev\TestProject\src\typescript\classSimple.ts' for expanded references Message:Added framework dependency 'C:\Users\myself\AppData\Local\Microsoft\VisualStudio.0\Extensions\mfv4e3ra.luv\TestFiles\jasmine\v2\jasmine_favicon.png' to referenced files Message:Added framework dependency 'C:\Users\myself\AppData\Local\Microsoft\VisualStudio.0\Extensions\mfv4e3ra.luv\TestFiles\jasmine\v2\boot.js' to referenced files Message:Added framework dependency 'C:\Users\myself\AppData\Local\Microsoft\VisualStudio.0\Extensions\mfv4e3ra.luv\TestFiles\jasmine\v2\jasmine-html.js' to referenced files Message:Added framework dependency 'C:\Users\myself\AppData\Local\Microsoft\VisualStudio.0\Extensions\mfv4e3ra.luv\TestFiles\jasmine\v2\jasmine.js' to referenced files Message:Added framework dependency 'C:\Users\myself\AppData\Local\Microsoft\VisualStudio.0\Extensions\mfv4e3ra.luv\TestFiles\jasmine\v2\jasmine.css' to referenced files Message:Added framework dependency 'C:\Users\myself\AppData\Local\Microsoft\VisualStudio.0\Extensions\mfv4e3ra.luv\TestFiles\chutzpah_boot.js' to referenced files Message:Finished building test context for C:\dev\TestProject\tests\jasmine\typescript\simpleSpec.ts Error: 0 : Time:22:47:48.6888513; Thread:23; Message:Can't find location for generated path on C:\dev\TestProject\src\typescript\classSimple.ts Warning: 0 : Time:22:47:48.6978503; Thread:23; Message:Chutzpah determined generated .js files are missing but the compile mode is External so Chutzpah can't compile them. Test results may be wrong. Information: 0 : Time:22:47:48.7038522; Thread:23; Message:Found generated path for C:\dev\TestProject\tests\jasmine\typescript\simpleSpec.ts at c:\dev\TestProject\tests\jasmine\typescript\simpleSpec.js Error: 0 : Time:22:47:48.7088508; Thread:23; Message:Couldn't find generated path for C:\dev\TestProject\src\typescript\classSimple.ts at
就是这样 - 它只是在最后切断,没有任何反应。问题是,我看到 .js
文件 正好 在上面跟踪中所述的位置,但 Chutzpah 声称 Couldn't find generated path for ...
这对于单元测试来说非常简单。我知道测试独立于 .ts
文件工作,因为如果我 运行 只有 .js
文件测试通过。
我做错了什么?
感谢您发布非常详细的重现。你遇到了我见过几次的问题。由于您的 chutzpah.json 嵌套在您的测试文件夹中,您需要指定 chutzpah 应该使用的文件夹路径映射来确定如何从源映射到输出文件夹。为此,只需将 chutzpah.json 更改为:
{
"RootReferencePathMode": "SettingsFileDirectory",
"Framework": "jasmine",
"Compile": {
"Mode": "External",
"Extensions": [ ".ts" ],
"ExtensionsWithNoOutput": [ ".d.ts" ],
"Paths": [
{ "SourcePath": "../../../", "OutputPath": "../../../"}
]
},
"References": [
{ "Path": "../../../src/typescript/classSimple.ts" }
],
"Tests": [
{ "Path": "simpleSpec.ts" }
],
"EnableTracing": true,
"TraceFilePath": "trace.txt"
}
另一种解决方法是将 chutzpah.json 放在项目的根目录下。
我需要考虑改进 Chutzpah 以避免这种情况。我想我会考虑在这种情况下让 chutzpah 假设文件位于同一位置以避免这种常见的陷阱。
Google 把我带到这里来解决类似的问题:
我的 Chutzpah 项目中的一些新测试是用 TypeScript 编写并编译成单个 outfile
:
tsconfig.json
"compilerOptions": {
"noImplicitAny": true,
"target": "es5",
"module": "none",
"outFile": "./Tests/mytests-ts.js",
"sourceMap": true
}
Chutzpah 找不到 *.ts
个文件
Chutzpah Error: System.IO.FileNotFoundException: Couldn't find generated path for D:\Code\ChutzpahProject\Scripts\DateServiceMock.ts
直到我使用 Paths
选项:
chutzpah.json
"Compile": {
"Extensions": [ ".ts" ],
"ExtensionsWithNoOutput": [ ".d.ts" ],
"Executable": "build-ts.cmd",
"Paths": [
{ "OutputPath": "Tests/mytests-tests-ts.js" }
]
},
感觉这可能是对@matthew-manela 的回答的评论...我想。