我正在尝试将 mocha 测试框架集成到我现有的 Angular 应用程序中。我的设置按预期工作,但由于我想测试组件、服务等,我正在尝试使用 Angular Test Bed 配置,所以我想用打字稿编写测试文件,它会为我产生错误。
我尝试了不同的方法,这给了我不同的错误。首先,我安装了mocha & @types/mocha & 然后也更新了package.json。然后,当我运行npm test时,它产生了错误。我尝试安装ts-node并在测试配置中要求它并运行测试,但它没有解决这个问题。然后我尝试更改编译器模块,然后产生另一个错误。我尝试搜索 SO 和许多其他网站,但解决方案对我没有帮助。
这是第一次尝试。我的test.ts文件
import { TestBed, ComponentFixture } from "@angular/core/testing";
import { LoginComponent } from 'src/app/login/login.component';
let assert = require('assert');
describe('LoginComponent', () => {
let fixture: ComponentFixture<LoginComponent>;
let component: LoginComponent;
beforeEach(function() {
TestBed.configureTestingModule({
declarations: [LoginComponent]
})
fixture = TestBed.createComponent(LoginComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('create Login component', () => {
assert.notNull(component);
});
});
并产生了以下错误堆栈跟踪。
D:\Testing\cenium-web\test\login.component.test.ts:1
(function (exports, require, module, __filename, __dirname) { import { TestBed } from "@angular/core/testing";
SyntaxError: Unexpected token {
at new Script (vm.js:80:7)
at createScript (vm.js:274:10)
at Object.runInThisContext (vm.js:326:10)
at Module._compile (internal/modules/cjs/loader.js:664:28)
at Module.m._compile (D:\Testing\cenium-web\node_modules\ts-node\src\index.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Object.require.extensions.(anonymous function) [as .ts] (D:\Testing\cenium-web\node_modules\ts-node\src\index.ts:442:12)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at D:\Testing\cenium-web\node_modules\mocha\lib\mocha.js:330:36
at Array.forEach (<anonymous>)
at Mocha.loadFiles (D:\Testing\cenium-web\node_modules\mocha\lib\mocha.js:327:14)
at Mocha.run (D:\Testing\cenium-web\node_modules\mocha\lib\mocha.js:804:10)
at Object.exports.singleRun (D:\Testing\cenium-web\node_modules\mocha\lib\cli\run-helpers.js:207:16)
at exports.runMocha (D:\Testing\cenium-web\node_modules\mocha\lib\cli\run-helpers.js:300:13)
at Object.exports.handler.argv [as handler] (D:\Testing\cenium-web\node_modules\mocha\lib\cli\run.js:296:3)
at Object.runCommand (D:\Testing\cenium-web\node_modules\mocha\node_modules\yargs\lib\command.js:242:26)
at Object.parseArgs [as _parseArgs] (D:\Testing\cenium-web\node_modules\mocha\node_modules\yargs\yargs.js:1087:28)
at Object.parse (D:\Testing\cenium-web\node_modules\mocha\node_modules\yargs\yargs.js:566:25)
at Object.exports.main (D:\Testing\cenium-web\node_modules\mocha\lib\cli\cli.js:63:6)
at Object.<anonymous> (D:\Testing\cenium-web\node_modules\mocha\bin\_mocha:10:23)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
npm ERR! Test failed. See above for more details.
这是我的tsconfig.json文件。
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"module": "es2015",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"target": "es5",
"types": ["node", "mocha"],
"typeRoots": ["node_modules/@types"],
"lib": ["es2018", "dom"],
"paths": {
"cComponents": ["dist/cComponents"],
"cComponents/*": ["dist/cComponents/*"]
}
}
}
然后我尝试将编译器模块从es2015更改为commonjs。
这在我认为的路径中导致了不同的错误
Error: Cannot find module 'src/app/login/login.component'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
at Function.Module._load (internal/modules/cjs/loader.js:508:25)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (D:\Testing\cenium-web\test\login.component.test.ts:2:1)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Module.m._compile (D:\Testing\cenium-web\node_modules\ts-node\src\index.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Object.require.extensions.(anonymous function) [as .ts] (D:\Testing\cenium-web\node_modules\ts-node\src\index.ts:442:12)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at D:\Testing\cenium-web\node_modules\mocha\lib\mocha.js:330:36
at Array.forEach (<anonymous>)
at Mocha.loadFiles (D:\Testing\cenium-web\node_modules\mocha\lib\mocha.js:327:14)
at Mocha.run (D:\Testing\cenium-web\node_modules\mocha\lib\mocha.js:804:10)
at Object.exports.singleRun (D:\Testing\cenium-web\node_modules\mocha\lib\cli\run-helpers.js:207:16)
at exports.runMocha (D:\Testing\cenium-web\node_modules\mocha\lib\cli\run-helpers.js:300:13)
at Object.exports.handler.argv [as handler] (D:\Testing\cenium-web\node_modules\mocha\lib\cli\run.js:296:3)
at Object.runCommand (D:\Testing\cenium-web\node_modules\mocha\node_modules\yargs\lib\command.js:242:26)
at Object.parseArgs [as _parseArgs] (D:\Testing\cenium-web\node_modules\mocha\node_modules\yargs\yargs.js:1087:28)
at Object.parse (D:\Testing\cenium-web\node_modules\mocha\node_modules\yargs\yargs.js:566:25)
at Object.exports.main (D:\Testing\cenium-web\node_modules\mocha\lib\cli\cli.js:63:6)
at Object.<anonymous> (D:\Testing\cenium-web\node_modules\mocha\bin\_mocha:10:23)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
npm ERR! Test failed. See above for more details.
我有一点说我必须使用webpack 之类的东西来解析这些模块。这个对吗?如果可能的话,有人可以指导我找到解决方案吗?
我想在我现有的测试文件中使用TestBed配置来测试我的组件。非常感谢如果有人可以帮助我,因为我是测试新手。提前致谢。希望信息足够。