在使用 jest 进行单元测试期间,我遇到了 NestJS 的依赖注入问题。这是我的代码库。app.controller.spec.ts
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';
describe('AppController', () => {
let appController: AppController;
beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [AppController],
providers: [AppService],
}).compile();
appController = app.get<AppController>(AppController);
});
describe('root', () => {
it('should return "Hello World!"', () => {
expect(appController.getHello()).toBe('Hello World!');
});
});
});
Run Code Online (Sandbox Code Playgroud)
问题是,存在依赖注入app.service.ts
import { Injectable, CACHE_MANAGER, Inject } from "@nestjs/common";
import {Cache} from …Run Code Online (Sandbox Code Playgroud) 按照此处的文档,可以轻松启用 HTTP REST 应用程序的 NestJS URI 版本控制。
然而,文档没有解释如何使 URI 版本控制成为可选。
例子:
/api/v1/users
/api/v2/users
/api/users-> 应映射到 v1 以允许现有的 webhooks 继续工作
问题:
我们如何在 NestJS REST 应用程序中使 URI 版本控制成为可选,以便旧版本(没有任何 api 版本)继续工作?
我正在使用 graphql 开发 NestJs 应用程序,并且我正在尝试使用类转换器来清理我的解析器输入,如下所示:
@InputType()
export class CreateUserInput {
@Field(() => String)
@Transform(({ value }) => value.trim())
email!: string;
}
Run Code Online (Sandbox Code Playgroud)
但 Transform 内容永远不会被执行。
如何使用 InputType 中的装饰器正确清理?
我面临的错误是
npx @nestjs/cli rest_of_command_here
Run Code Online (Sandbox Code Playgroud)
我还尝试使用 -g 标志、-g --save-dev 标志一起安装,但似乎没有任何点击
sh: 1: nest: not found
Run Code Online (Sandbox Code Playgroud)
我已经尝试过找不到嵌套命令的答案,但它不起作用,因为我面临一个新问题,其中写着
bash: /usr/local/Cellar/node/11.9.0/bin/nest: No such file or directory
Run Code Online (Sandbox Code Playgroud)
节点版本 16.13.2
npm 版本 8.1.2
我的系统有这样的自定义错误:
import { ExtendableError } from './extandable.error'
export const customError = {
EMAIL_EXIST: () => new ExtendableError({
message: 'USER WITH SUCH EMAIL ALREADY EXIST',
code: 403
}),
INVALID_EMAIL: () => new ExtendableError({
message: 'INVALID EMAIL',
code: 400
}),
INVALID_PASSWORD: () => new ExtendableError({
message: 'INVALID EMAIL OR PASSWORD',
code: 403
}),
EMAIL_DOES_NOT_EXIST: () => new ExtendableError({
message: 'EMAIL DOES NOT EXIST',
code: 404
}),
TOKEN_DOES_NOT_EXIST: () => new ExtendableError({
message: 'TOKEN DOES NOT EXIST',
code: 404
}),
DIFFERENT_PASSWORDS: () => …Run Code Online (Sandbox Code Playgroud) 在 NestJS 中我有自定义记录器:
import { Injectable, Logger, Scope } from '@nestjs/common';
@Injectable({ scope: Scope.TRANSIENT })
export class LoggerService extends Logger {
log(message: any) {
super.log(message);
}
error(message: any) {
super.log(message);
}
warn(message: any) {
super.log(message);
}
debug(message: any) {
super.log(message);
}
verbose(message: any) {
super.log(message);
}
setContext(context: string) {
super.context = context;
}
}
Run Code Online (Sandbox Code Playgroud)
它在全球注册:
import { Global, Module } from '@nestjs/common';
import { LoggerService } from './logger.service';
@Global()
@Module({
providers: [LoggerService],
exports: [LoggerService],
})
export class LoggerModule {}
Run Code Online (Sandbox Code Playgroud)
有没有办法以某种方式在服务构造函数中传递注入上下文,并避免在每个服务中执行 …
我正在使用 NestJS 和 TypeScript 为大型应用程序编写第一个测试。我想导入一个实体,该实体从另一个文件夹中的模块导入接口。事实证明,这具有挑战性。
该项目的结构如下
apps
..apis
--..web-api
----..src
----..package.json
----..jest.config.js
------..app
--------..profiles
----------.._entities
------------..profile.entity.spec.ts <--- the test
------------..profile.entity.ts
libs
--app-interfaces
----src
------lib
--------profile
----------index.ts <--- the files the test is trying to import and test
----------gender.enum.ts
----------profile.interface.ts
Run Code Online (Sandbox Code Playgroud)
在 profile.entity.spec.ts 中我有:
apps
..apis
--..web-api
----..src
----..package.json
----..jest.config.js
------..app
--------..profiles
----------.._entities
------------..profile.entity.spec.ts <--- the test
------------..profile.entity.ts
libs
--app-interfaces
----src
------lib
--------profile
----------index.ts <--- the files the test is trying to import and test
----------gender.enum.ts
----------profile.interface.ts
Run Code Online (Sandbox Code Playgroud)
在 profile.entity.ts 中:
import { …Run Code Online (Sandbox Code Playgroud) 我正在使用 Nest.js graphql prisma。
当我运行 npm run start:dev 命令时,出现以下错误。
错误
/node_modules/apollo-server-core/src/ApolloServer.ts:471 throw Error('只有一个插件可以实现 renderLandingPage。'); ^ 错误:只有一个插件可以实现 renderLandingPage。
app.module.ts
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ApolloServerPluginLandingPageLocalDefault } from 'apollo-server-core';
import { DonationsModule } from './donations/donations.module';
import { ApolloDriverConfig, ApolloDriver } from '@nestjs/apollo';
@Module({
imports: [
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
plugins: [ApolloServerPluginLandingPageLocalDefault()],
typePaths: ['./**/*.graphql'],
}),
DonationsModule,
],
controllers: [AppController],
providers: [AppService],
})
export …Run Code Online (Sandbox Code Playgroud) 我知道这看起来很基本,但我似乎找不到在 Nest.js 猫鼬模式中设置 _id 字段的正确方法。我附上了一张图片,只是想知道设置此属性的最佳方法是什么?我收到以下错误
“SchemaType”仅指类型,但在此处用作命名空间。ts(2702)
我一直在尝试让我的 TypeOrm Config 与 Migrations 和 .env 很好地配合工作,而不需要在两个地方指定它。
斗争是这样的 - 我的迁移脚本需要读取立即返回的对象 -
{
type: 'postgres',
host: '127.0.0.1',
port: 5432,
username: 'postgres',
password: 'myPassword',
database: 'postgres',
entities: [ 'dist/**/*.entity.js' ],
logging: [ 'query', 'error', 'schema' ],
synchronize: false,
migrations: [ 'dist/app/database/migrations/*.js' ],
cli: { migrationsDir: 'src/app/database/migrations' },
namingStrategy: SnakeNamingStrategy {
nestedSetColumnNames: { left: 'nsleft', right: 'nsright' },
materializedPathColumnName: 'mpath'
},
subscribers: [],
migrationsRun: false,
dropSchema: false
}
Run Code Online (Sandbox Code Playgroud)
但是,当我使用 NestJs 建议的配置,同时还利用 .env (DOTENV) 文件时,解决方案如下所示:
import {TypeOrmModuleOptions, TypeOrmOptionsFactory} from "@nestjs/typeorm";
import {SnakeNamingStrategy} from …Run Code Online (Sandbox Code Playgroud)