Menu

Decorator-based Nest.js 12 tests with Jest in ESM mode
📰

Decorator-based Nest.js 12 tests with Jest in ESM mode

DEV Community: typescript·Maximiliano Ezequiel Carrizo·18 days ago
#njk6eNzw
#devtotypescript#devto#dev#test#jest
Reading 0:00
15s threshold

Nest.js 12 packages are ESM only, and Jest can only load them in its native ESM mode. So a Nest.js 12 test suite on Jest needs some setup before it runs at all.

nestjs-jest-decorators works in that setup, and it also lets you write tests as classes. Suites and tests are plain classes and methods, and test classes are Nest providers, so you use @Inject() instead of app.get(Component) .

@ TestSuite ( ' Demo Test Suite ' ) export class DemoTest { @ Inject () private readonly httpService ! : HttpService ;

@ Test ( ' Get Test ' ) async testGet (): Promise < void > { const { data } = await firstValueFrom ( this . httpService . get ( ' http://localhost:4343/demo ' )); expect ( data ). toBe ( ' Hello, Test world ! ' ); } }

Enter fullscreen mode

Exit fullscreen mode

@TestSuite is a Jest describe and @Test is a Jest it .

Installation

You need Nest.js 12, Jest 30 and Node.js ^20.19.0 , ^22.12.0 or >=24.0.0 .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.