- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在 Typescript Monorepo 中操作。我想向 Monorepo 添加一个带有 Jest 测试的 Angular 8 前端。但是我遇到了一些问题。
我正在使用
Angular CLI: 8.3.5
I will use this repository as a starting point!
然后在<root>/services
我跑了:
ng new frontend
创建 Angular 应用程序后,我能够运行 ng test
结果如下:
一切正常。
I am using https://github.com/briebug/jest-schematic to easily add Jest to my Angular application
yarn global add @briebug/jest-schematic
ng add @briebug/jest-schematic
这会导致以下变化
正在运行 jest
导致以下错误:
$ jest
FAIL src/app/app.component.spec.ts
● Test suite failed to run
File not found: <rootDir>/src/tsconfig.spec.json (resolved as: /home/flo/Desktop/stackoverflow-monorepo-angular-jest/services/frontend/src/tsconfig.spec.json)
at ConfigSet.resolvePath (node_modules/ts-jest/dist/config/config-set.js:712:19)
at ConfigSet.get (node_modules/ts-jest/dist/config/config-set.js:202:67)
at ConfigSet.tsJest (node_modules/ts-jest/dist/util/memoize.js:43:24)
at ConfigSet.get (node_modules/ts-jest/dist/config/config-set.js:297:41)
at ConfigSet.versions (node_modules/ts-jest/dist/util/memoize.js:43:24)
at ConfigSet.get (node_modules/ts-jest/dist/config/config-set.js:583:32)
at ConfigSet.jsonValue (node_modules/ts-jest/dist/util/memoize.js:43:24)
at ConfigSet.get [as cacheKey] (node_modules/ts-jest/dist/config/config-set.js:598:25)
at TsJestTransformer.getCacheKey (node_modules/ts-jest/dist/ts-jest-transformer.js:126:36)
at ScriptTransformer._getCacheKey (node_modules/@jest/transform/build/ScriptTransformer.js:266:23)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 1.267s
Ran all test suites.
Jest 试图找到 tsconfig.spec.json
在错误的文件夹中。幸运的是,我找到了a fix .我需要更改 jest.config.js
module.exports = {
preset: 'jest-preset-angular',
setupFilesAfterEnv: ['<rootDir>/src/setup-jest.ts'],
globals: {
'ts-jest': {
tsConfig: '<rootDir>/tsconfig.spec.json',
diagnostics: false,
stringifyContentPathRegex: '\\.html$',
astTransformers: [require.resolve('jest-preset-angular/InlineHtmlStripStylesTransformer')],
},
},
};
现在当我运行 jest
它有效:
我现在添加了 Angular HttClient
到我的AppComponent
:
// app.component.ts
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
title = 'frontend';
constructor(private http: HttpClient) {}
}
我还添加了 HttpClientModule
至 app.module.ts
和 imports
在app.component.spec.ts
.
// app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, AppRoutingModule, HttpClientModule],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {}
// app.component.spec.ts
import { TestBed, async } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';
import { HttpClientModule, HttpClient } from '@angular/common/http';
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule, HttpClientModule],
declarations: [AppComponent],
}).compileComponents();
}));
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
});
it(`should have as title 'frontend'`, () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app.title).toEqual('frontend');
});
it('should render title', () => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.debugElement.nativeElement;
expect(compiled.querySelector('.content span').textContent).toContain('frontend app is running!');
});
});
但是,当运行 jest
,我收到以下错误:
$ jest
FAIL src/app/app.component.spec.ts
AppComponent
✕ should create the app (449ms)
✕ should have as title 'frontend' (10ms)
✕ should render title (10ms)
● AppComponent › should create the app
Can't resolve all parameters for AppComponent: (?).
at syntaxError (../../../packages/compiler/src/util.ts:100:17)
at CompileMetadataResolver._getDependenciesMetadata (../../../packages/compiler/src/metadata_resolver.ts:957:27)
at CompileMetadataResolver._getTypeMetadata (../../../packages/compiler/src/metadata_resolver.ts:836:20)
at CompileMetadataResolver.getNonNormalizedDirectiveMetadata (../../../packages/compiler/src/metadata_resolver.ts:377:18)
at CompileMetadataResolver.loadDirectiveMetadata (../../../packages/compiler/src/metadata_resolver.ts:224:11)
at ../../../packages/compiler/src/jit/compiler.ts:135:36
at Array.forEach (<anonymous>)
at ../../../packages/compiler/src/jit/compiler.ts:133:65
at Array.forEach (<anonymous>)
at JitCompiler._loadModules (../../../packages/compiler/src/jit/compiler.ts:130:71)
at JitCompiler._compileModuleAndAllComponents (../../../packages/compiler/src/jit/compiler.ts:115:32)
at JitCompiler.compileModuleAndAllComponentsAsync (../../../packages/compiler/src/jit/compiler.ts:69:33)
at CompilerImpl.compileModuleAndAllComponentsAsync (../../../packages/platform-browser-dynamic/src/compiler_factory.ts:69:27)
at TestingCompilerImpl.compileModuleAndAllComponentsAsync (../../../../packages/platform-browser-dynamic/testing/src/compiler_factory.ts:57:27)
at TestBedViewEngine.compileComponents (../../../../packages/core/testing/src/test_bed.ts:362:27)
at Function.TestBedViewEngine.compileComponents (../../../../packages/core/testing/src/test_bed.ts:160:66)
at testing_1.async (src/app/app.component.spec.ts:11:8)
● AppComponent › should create the app
Can't resolve all parameters for AppComponent: (?).
at syntaxError (../../../packages/compiler/src/util.ts:100:17)
at CompileMetadataResolver._getDependenciesMetadata (../../../packages/compiler/src/metadata_resolver.ts:957:27)
at CompileMetadataResolver._getTypeMetadata (../../../packages/compiler/src/metadata_resolver.ts:836:20)
at CompileMetadataResolver.getNonNormalizedDirectiveMetadata (../../../packages/compiler/src/metadata_resolver.ts:377:18)
at CompileMetadataResolver.loadDirectiveMetadata (../../../packages/compiler/src/metadata_resolver.ts:224:11)
at ../../../packages/compiler/src/jit/compiler.ts:135:36
at Array.forEach (<anonymous>)
at ../../../packages/compiler/src/jit/compiler.ts:133:65
at Array.forEach (<anonymous>)
at JitCompiler._loadModules (../../../packages/compiler/src/jit/compiler.ts:130:71)
at JitCompiler._compileModuleAndAllComponents (../../../packages/compiler/src/jit/compiler.ts:115:32)
at JitCompiler.compileModuleAndAllComponentsSync (../../../packages/compiler/src/jit/compiler.ts:65:38)
at CompilerImpl.compileModuleAndAllComponentsSync (../../../packages/platform-browser-dynamic/src/compiler_factory.ts:61:35)
at TestingCompilerImpl.compileModuleAndAllComponentsSync (../../../../packages/platform-browser-dynamic/testing/src/compiler_factory.ts:52:27)
at TestBedViewEngine._initIfNeeded (../../../../packages/core/testing/src/test_bed.ts:376:28)
at TestBedViewEngine.createComponent (../../../../packages/core/testing/src/test_bed.ts:570:10)
at Function.TestBedViewEngine.createComponent (../../../../packages/core/testing/src/test_bed.ts:232:36)
at it (src/app/app.component.spec.ts:15:29)
● AppComponent › should have as title 'frontend'
Can't resolve all parameters for AppComponent: (?).
at syntaxError (../../../packages/compiler/src/util.ts:100:17)
at CompileMetadataResolver._getDependenciesMetadata (../../../packages/compiler/src/metadata_resolver.ts:957:27)
at CompileMetadataResolver._getTypeMetadata (../../../packages/compiler/src/metadata_resolver.ts:836:20)
at CompileMetadataResolver.getNonNormalizedDirectiveMetadata (../../../packages/compiler/src/metadata_resolver.ts:377:18)
at CompileMetadataResolver.loadDirectiveMetadata (../../../packages/compiler/src/metadata_resolver.ts:224:11)
at ../../../packages/compiler/src/jit/compiler.ts:135:36
at Array.forEach (<anonymous>)
at ../../../packages/compiler/src/jit/compiler.ts:133:65
at Array.forEach (<anonymous>)
at JitCompiler._loadModules (../../../packages/compiler/src/jit/compiler.ts:130:71)
at JitCompiler._compileModuleAndAllComponents (../../../packages/compiler/src/jit/compiler.ts:115:32)
at JitCompiler.compileModuleAndAllComponentsAsync (../../../packages/compiler/src/jit/compiler.ts:69:33)
at CompilerImpl.compileModuleAndAllComponentsAsync (../../../packages/platform-browser-dynamic/src/compiler_factory.ts:69:27)
at TestingCompilerImpl.compileModuleAndAllComponentsAsync (../../../../packages/platform-browser-dynamic/testing/src/compiler_factory.ts:57:27)
at TestBedViewEngine.compileComponents (../../../../packages/core/testing/src/test_bed.ts:362:27)
at Function.TestBedViewEngine.compileComponents (../../../../packages/core/testing/src/test_bed.ts:160:66)
at testing_1.async (src/app/app.component.spec.ts:11:8)
● AppComponent › should have as title 'frontend'
Can't resolve all parameters for AppComponent: (?).
at syntaxError (../../../packages/compiler/src/util.ts:100:17)
at CompileMetadataResolver._getDependenciesMetadata (../../../packages/compiler/src/metadata_resolver.ts:957:27)
at CompileMetadataResolver._getTypeMetadata (../../../packages/compiler/src/metadata_resolver.ts:836:20)
at CompileMetadataResolver.getNonNormalizedDirectiveMetadata (../../../packages/compiler/src/metadata_resolver.ts:377:18)
at CompileMetadataResolver.loadDirectiveMetadata (../../../packages/compiler/src/metadata_resolver.ts:224:11)
at ../../../packages/compiler/src/jit/compiler.ts:135:36
at Array.forEach (<anonymous>)
at ../../../packages/compiler/src/jit/compiler.ts:133:65
at Array.forEach (<anonymous>)
at JitCompiler._loadModules (../../../packages/compiler/src/jit/compiler.ts:130:71)
at JitCompiler._compileModuleAndAllComponents (../../../packages/compiler/src/jit/compiler.ts:115:32)
at JitCompiler.compileModuleAndAllComponentsSync (../../../packages/compiler/src/jit/compiler.ts:65:38)
at CompilerImpl.compileModuleAndAllComponentsSync (../../../packages/platform-browser-dynamic/src/compiler_factory.ts:61:35)
at TestingCompilerImpl.compileModuleAndAllComponentsSync (../../../../packages/platform-browser-dynamic/testing/src/compiler_factory.ts:52:27)
at TestBedViewEngine._initIfNeeded (../../../../packages/core/testing/src/test_bed.ts:376:28)
at TestBedViewEngine.createComponent (../../../../packages/core/testing/src/test_bed.ts:570:10)
at Function.TestBedViewEngine.createComponent (../../../../packages/core/testing/src/test_bed.ts:232:36)
at it (src/app/app.component.spec.ts:21:29)
● AppComponent › should render title
Can't resolve all parameters for AppComponent: (?).
at syntaxError (../../../packages/compiler/src/util.ts:100:17)
at CompileMetadataResolver._getDependenciesMetadata (../../../packages/compiler/src/metadata_resolver.ts:957:27)
at CompileMetadataResolver._getTypeMetadata (../../../packages/compiler/src/metadata_resolver.ts:836:20)
at CompileMetadataResolver.getNonNormalizedDirectiveMetadata (../../../packages/compiler/src/metadata_resolver.ts:377:18)
at CompileMetadataResolver.loadDirectiveMetadata (../../../packages/compiler/src/metadata_resolver.ts:224:11)
at ../../../packages/compiler/src/jit/compiler.ts:135:36
at Array.forEach (<anonymous>)
at ../../../packages/compiler/src/jit/compiler.ts:133:65
at Array.forEach (<anonymous>)
at JitCompiler._loadModules (../../../packages/compiler/src/jit/compiler.ts:130:71)
at JitCompiler._compileModuleAndAllComponents (../../../packages/compiler/src/jit/compiler.ts:115:32)
at JitCompiler.compileModuleAndAllComponentsAsync (../../../packages/compiler/src/jit/compiler.ts:69:33)
at CompilerImpl.compileModuleAndAllComponentsAsync (../../../packages/platform-browser-dynamic/src/compiler_factory.ts:69:27)
at TestingCompilerImpl.compileModuleAndAllComponentsAsync (../../../../packages/platform-browser-dynamic/testing/src/compiler_factory.ts:57:27)
at TestBedViewEngine.compileComponents (../../../../packages/core/testing/src/test_bed.ts:362:27)
at Function.TestBedViewEngine.compileComponents (../../../../packages/core/testing/src/test_bed.ts:160:66)
at testing_1.async (src/app/app.component.spec.ts:11:8)
● AppComponent › should render title
Can't resolve all parameters for AppComponent: (?).
at syntaxError (../../../packages/compiler/src/util.ts:100:17)
at CompileMetadataResolver._getDependenciesMetadata (../../../packages/compiler/src/metadata_resolver.ts:957:27)
at CompileMetadataResolver._getTypeMetadata (../../../packages/compiler/src/metadata_resolver.ts:836:20)
at CompileMetadataResolver.getNonNormalizedDirectiveMetadata (../../../packages/compiler/src/metadata_resolver.ts:377:18)
at CompileMetadataResolver.loadDirectiveMetadata (../../../packages/compiler/src/metadata_resolver.ts:224:11)
at ../../../packages/compiler/src/jit/compiler.ts:135:36
at Array.forEach (<anonymous>)
at ../../../packages/compiler/src/jit/compiler.ts:133:65
at Array.forEach (<anonymous>)
at JitCompiler._loadModules (../../../packages/compiler/src/jit/compiler.ts:130:71)
at JitCompiler._compileModuleAndAllComponents (../../../packages/compiler/src/jit/compiler.ts:115:32)
at JitCompiler.compileModuleAndAllComponentsSync (../../../packages/compiler/src/jit/compiler.ts:65:38)
at CompilerImpl.compileModuleAndAllComponentsSync (../../../packages/platform-browser-dynamic/src/compiler_factory.ts:61:35)
at TestingCompilerImpl.compileModuleAndAllComponentsSync (../../../../packages/platform-browser-dynamic/testing/src/compiler_factory.ts:52:27)
at TestBedViewEngine._initIfNeeded (../../../../packages/core/testing/src/test_bed.ts:376:28)
at TestBedViewEngine.createComponent (../../../../packages/core/testing/src/test_bed.ts:570:10)
at Function.TestBedViewEngine.createComponent (../../../../packages/core/testing/src/test_bed.ts:232:36)
at it (src/app/app.component.spec.ts:27:29)
Test Suites: 1 failed, 1 total
Tests: 3 failed, 3 total
Snapshots: 0 total
Time: 1.682s, estimated 2s
Ran all test suites.
error Command failed with exit code 1.
使用 Jasmine + Karma 时,测试运行得很好。使用 jest 进行测试时,依赖注入(inject)似乎出了问题。
您可以在此处找到存储库:https://github.com/flolude/stackoverflow-monorepo-angular-jest/commit/9a2d8cac0dfa25a5f6620f38238c3f577b2acb63亲自尝试。
最佳答案
只需在 tsconfig.spec.json
中将 emitDecoratorMetadata
设置为 true
。
这允许 ts-jest
在 TypeScript 转译过程中不会丢失元数据。在 Angular 更新到版本 8.1 之后,这个问题对于 jest-preset-angular
开发人员来说是意想不到的
这是关于 jest-preset-angular
GitHub 的问题:https://github.com/thymikee/jest-preset-angular/issues/288
关于 Angular + Jest : Can't resolve all parameters for AppComponent: (? ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58075111/
我正在为我的 react 项目编写单元测试用例,并使用 Jest 和 enzyme 来编写测试用例。我已阅读 Jest 文档 https://jestjs.io/docs/en/jest-object
运行测试时出现以下错误: 无法从“jest-expect.js”中找到模块“jest-matchers” at Resolver.resolveModule (node_modules/jest-
如何使用 jest.run() 或 jest.runCLI() 以编程方式运行所有测试?我应该用什么作为论据? 我试图找到有关它们的文档但失败了。 如果上述函数不起作用,如果我想以编程方式运行 jes
用 Jest 和 enzyme 测试 react 和 typescript ,我如何模拟这个 ExpectsVideo 函数,以便我可以根据我的测试用例场景定义它是否返回 true 或 false j
用 Jest 和 enzyme 测试 react 和 typescript ,我如何模拟这个 ExpectsVideo 函数,以便我可以根据我的测试用例场景定义它是否返回 true 或 false j
我正在使用以下 jest.unittest.json 文件(通过 jest --config 选项使用): { "bail": false, "verbose": true, "trans
我跑npm init npm i -D jest像这样 tutorial 运行推荐 nmp test 后出现此错误 这不是生物.js 或生物.test.js 的错误,因为没有发生此文件错误。我怎样才能
我有一个 Mongoose 模型: var mongoose = require("mongoose"); var transactionSchema = mongoose.Schema({ ca
我在我的角度项目中进行了 Jest 测试。 我有一个 package.json 文件指定了我想用来运行测试的 jest 版本。该文件包括: "@types/jest": "^24.0.18", "je
我正在尝试使用 jest 编写单元测试用例,并且需要模拟以下模式。我收到 TypeError: is not a constructor。 用例:我的用例如下所述 我的组件.js : import
所以我正在使用 jest测试我的 node.js 应用程序,测试完成得很好,但我从 jest 收到一条关于打开句柄的消息。任何见解? jest --detectOpenHandles 通过 src/l
我正在使用 Babel Jest 来转换我的代码以进行测试。 我不知道如何使用相对于项目根目录的路径。 例如,如果我在一个测试文件中导入一个模块:/imports/ui/myModule Jest 抛
我正在致力于更新 Express 微服务的测试。对于某些目录(即我们的 /utils 文件夹),我编写了大量测试,显示覆盖率达到 80-90%。在/routes目录下,有0个测试,但显示100%。 总
我收到以下错误: import React from 'react'; ^^^^^^ SyntaxError: Unexpected token import 原因是一些模块/s在npm中发布时没有编
我正在创建一个 Jest 测试来测试是否记录了用于 superFetch 函数错误处理的指标。我的方法是为 retryFetch 创建一个模拟函数并返回一个 Promise 拒绝事件。我希望它能进入
我正在设置一些新的配置来开 Jest ,我忽略了一些文件,比如 *.stories.js,但是当我使用 *.js.snap 或 *.snap,这个 Jest 不太行。 我正在使用 react-scri
PS E:\react\Code\UI> yarn 测试 yarn 运行 v1.17.3$开 Jest 失败 src/App.test.js ● 测试套件运行失败 Jest encountered a
我是开 Jest 测试的新手,我写了下面的测试。我 Jest mock 了一个函数并将其作为参数传递。但是当运行测试时,在日志中我可以看到函数 onSuccess 被调用。但 expect 失败并出现
我是测试新手。我正在使用 JEST 来测试我的 nodejs API。当我在一个文件中编写所有测试时,它可以正常运行,没有任何错误,但是当我分离它时,它给我的端口已经在使用中。至于每个文件,它运行不同
我正在编写一个 Jest 模拟,但是在模拟本身之外定义一个模拟函数时我似乎遇到了问题。 我有一个类: myClass.js class MyClass { constructor(name) {
我是一名优秀的程序员,十分优秀!