NestJS – Test suite failed to run Cannot find module ‘src/article/article.entity’ from ‘comment/comment.entity.ts’

You can tell Jest how to resolve module paths by configuring the moduleNameMapper option, which is useful if you’re using packages like module-alias or if you’re using absolute paths. Add these lines to your Jest configuration: { // … “jest”: { // … “moduleNameMapper”: { “^src/(.*)$”: “<rootDir>/$1” } } } Now modules that start with … Read more

Nest can’t resolve dependencies of the ItemsService (?). Please make sure that the argument at index [0] is available in the AppModule context

In order to solve it you have to remove the ItemsController and ItemsService imports from the app.module.ts file. This was the solution because: You already import ItemsController and ItemsService in your items.module.ts file so it’s not necessary to import them again in the app.module.ts file. In your items.module.ts you have the next line: @Module({ imports: … Read more

How to copy non-ts files to dist when building typescript?

You can also do this by adding an asset property in the nest-cli.json as mentioned in the documentation. For your case, you can try something like this: “assets”:[“**/Mail/templates/*”] Or if your templates all have a specific filetype (I am assuming its called .template), this should also work: “assets”:[“**/*.template”] The 2nd example will copy all files … Read more

Why do we need DTOs and interfaces both in NestJS

According to the Nestjs docs: But first (if you use TypeScript), we need to determine the DTO (Data Transfer Object) schema. A DTO is an object that defines how the data will be sent over the network. We could determine the DTO schema by using TypeScript interfaces, or by simple classes. Interestingly, we recommend using … Read more

Access raw body of Stripe webhook in Nest.js

For anyone looking for a more elegant solution, turn off the bodyParser in main.ts. Create two middleware functions, one for rawbody and the other for json-parsed-body. json-body.middleware.ts import { Request, Response } from ‘express’; import * as bodyParser from ‘body-parser’; import { Injectable, NestMiddleware } from ‘@nestjs/common’; @Injectable() export class JsonBodyMiddleware implements NestMiddleware { use(req: … Read more

Howto get req.user in services in Nest JS

Update March 2019 Since version v6, you can now inject the request object into a request-scoped provider: import { REQUEST } from ‘@nestjs/core’; import { Request } from ‘express’; @Injectable({ scope: Scope.REQUEST }) export class UsersService { constructor(@Inject(REQUEST) private readonly request: Request) {} } Outdated answer It’s not possible to inject the user (or request) … Read more

How can I create columns with type Date and type DateTime in nestjs with typeORM?

You can see the docs here that explains the @Column decorator. In @Column there is an option called type -> here is where you specify which type of date you want to store for that specific column. More on column types here. For example (using PostgreSQL): @Column({ type: ‘date’ }) date_only: string; @Column({ type: ‘timestamptz’ … Read more

How to exclude entity field from returned by controller JSON. NestJS + Typeorm

I’d suggest creating an interceptor that takes advantage of the class-transformer library: @Injectable() export class TransformInterceptor implements NestInterceptor { intercept( context: ExecutionContext, call$: Observable<any>, ): Observable<any> { return call$.pipe(map(data => classToPlain(data))); } } Then, simply exclude properties using @Exclude() decorator, for example: import { Exclude } from ‘class-transformer’; export class User { id: number; email: … Read more