Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.1k
feat(sample): add hmr with esm example#16078
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
shash-hq wants to merge1 commit intonestjs:masterChoose a base branch fromshash-hq:fix/hmr-esm-support
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+647 −0
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
feat(sample): add hmr with esm example#14331
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commit6a2636925da3093dceb1c1cbf61a0cd04577ac12
There are no files selected for viewing
18 changes: 18 additions & 0 deletionssample/36-hmr-esm/.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # dependencies | ||
| /node_modules | ||
| # IDE | ||
| /.idea | ||
| /.awcache | ||
| /.vscode | ||
| # misc | ||
| npm-debug.log | ||
| # tests | ||
| /test | ||
| /coverage | ||
| /.nyc_output | ||
| # dist | ||
| /dist |
34 changes: 34 additions & 0 deletionssample/36-hmr-esm/e2e/cats/cats.e2e-spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { INestApplication } from '@nestjs/common'; | ||
| import { Test } from '@nestjs/testing'; | ||
| import * as request from 'supertest'; | ||
| import { CatsModule } from '../../src/cats/cats.module'; | ||
| import { CatsService } from '../../src/cats/cats.service'; | ||
| import { CoreModule } from '../../src/core/core.module'; | ||
| describe('Cats', () => { | ||
| const catsService = { findAll: () => ['test'] }; | ||
| let app: INestApplication; | ||
| beforeAll(async () => { | ||
| const moduleRef = await Test.createTestingModule({ | ||
| imports: [CatsModule, CoreModule], | ||
| }) | ||
| .overrideProvider(CatsService) | ||
| .useValue(catsService) | ||
| .compile(); | ||
| app = moduleRef.createNestApplication(); | ||
| await app.init(); | ||
| }); | ||
| it(`/GET cats`, () => { | ||
| return request(app.getHttpServer()).get('/cats').expect(200).expect({ | ||
| data: catsService.findAll(), | ||
| }); | ||
| }); | ||
| afterAll(async () => { | ||
| await app.close(); | ||
| }); | ||
| }); |
14 changes: 14 additions & 0 deletionssample/36-hmr-esm/e2e/jest-e2e.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "moduleFileExtensions": [ | ||
| "ts", | ||
| "tsx", | ||
| "js", | ||
| "json" | ||
| ], | ||
| "transform": { | ||
| "^.+\\.tsx?$": "ts-jest" | ||
| }, | ||
| "testRegex": "/e2e/.*\\.(e2e-test|e2e-spec).(ts|tsx|js)$", | ||
| "collectCoverageFrom" : ["src/**/*.{js,jsx,tsx,ts}", "!**/node_modules/**", "!**/vendor/**"], | ||
| "coverageReporters": ["json", "lcov"] | ||
| } |
42 changes: 42 additions & 0 deletionssample/36-hmr-esm/eslint.config.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // @ts-check | ||
| import eslint from '@eslint/js'; | ||
| import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended'; | ||
| import globals from 'globals'; | ||
| import tseslint from 'typescript-eslint'; | ||
| export default tseslint.config( | ||
| { | ||
| ignores: ['eslint.config.mjs'], | ||
| }, | ||
| eslint.configs.recommended, | ||
| ...tseslint.configs.recommendedTypeChecked, | ||
| eslintPluginPrettierRecommended, | ||
| { | ||
| languageOptions: { | ||
| globals: { | ||
| ...globals.node, | ||
| ...globals.jest, | ||
| }, | ||
| ecmaVersion: 5, | ||
| sourceType: 'module', | ||
| parserOptions: { | ||
| projectService: true, | ||
| tsconfigRootDir: import.meta.dirname, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| rules: { | ||
| '@typescript-eslint/no-explicit-any': 'off', | ||
| '@typescript-eslint/no-floating-promises': 'off', | ||
| '@typescript-eslint/no-unsafe-argument': 'warn', | ||
| '@typescript-eslint/ban-ts-comment': 'off', | ||
| '@typescript-eslint/no-unsafe-return': 'off', | ||
| '@typescript-eslint/no-unsafe-assignment': 'warn', | ||
| '@typescript-eslint/no-unsafe-call': 'warn', | ||
| '@typescript-eslint/no-unsafe-member-access': 'warn', | ||
| '@typescript-eslint/require-await': 'warn', | ||
| '@typescript-eslint/no-unused-vars': 'warn', | ||
| }, | ||
| }, | ||
| ); |
14 changes: 14 additions & 0 deletionssample/36-hmr-esm/jest.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "moduleFileExtensions": [ | ||
| "ts", | ||
| "tsx", | ||
| "js", | ||
| "json" | ||
| ], | ||
| "transform": { | ||
| "^.+\\.tsx?$": "ts-jest" | ||
| }, | ||
| "testRegex": "/src/.*\\.(test|spec).(ts|tsx|js)$", | ||
| "collectCoverageFrom" : ["src/**/*.{js,jsx,tsx,ts}", "!**/node_modules/**", "!**/vendor/**"], | ||
| "coverageReporters": ["json", "lcov"] | ||
| } |
77 changes: 77 additions & 0 deletionssample/36-hmr-esm/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| { | ||
| "name": "nest-typescript-starter", | ||
| "version": "1.0.0", | ||
| "description": "Nest TypeScript starter repository", | ||
| "type": "module", | ||
| "license": "MIT", | ||
| "scripts": { | ||
| "prebuild": "rimraf dist", | ||
| "build": "nest build", | ||
| "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", | ||
| "start": "nest start", | ||
| "start:dev": "nest start --watch", | ||
| "start:debug": "nest start --debug --watch", | ||
| "start:dev:hmr": "nest build --webpack --webpackPath webpack-hmr.config.cjs --watch", | ||
| "start:prod": "node dist/main", | ||
| "lint": "eslint '{src,apps,libs,test}/**/*.ts' --fix", | ||
| "test": "jest", | ||
| "test:watch": "jest --watch", | ||
| "test:cov": "jest --coverage", | ||
| "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", | ||
| "test:e2e": "jest --config ./e2e/jest-e2e.json" | ||
| }, | ||
| "dependencies": { | ||
| "@nestjs/common": "11.1.9", | ||
| "@nestjs/core": "11.1.9", | ||
| "@nestjs/platform-express": "11.1.9", | ||
| "class-transformer": "0.5.1", | ||
| "class-validator": "0.14.3", | ||
| "reflect-metadata": "0.2.2", | ||
| "rimraf": "6.1.2", | ||
| "rxjs": "7.8.2" | ||
| }, | ||
| "devDependencies": { | ||
| "run-script-webpack-plugin": "0.2.0", | ||
| "webpack": "5.97.1", | ||
| "webpack-node-externals": "3.0.0", | ||
| "@eslint/eslintrc": "3.3.3", | ||
| "@eslint/js": "9.39.2", | ||
| "@nestjs/cli": "11.0.14", | ||
| "@nestjs/schematics": "11.0.9", | ||
| "@nestjs/testing": "11.1.9", | ||
| "@types/express": "5.0.6", | ||
| "@types/jest": "30.0.0", | ||
| "@types/node": "24.10.3", | ||
| "@types/supertest": "6.0.3", | ||
| "@types/webpack-env": "1.18.5", | ||
| "jest": "30.2.0", | ||
| "prettier": "3.7.4", | ||
| "supertest": "7.1.4", | ||
| "ts-jest": "29.4.6", | ||
| "ts-loader": "9.5.4", | ||
| "ts-node": "10.9.2", | ||
| "tsconfig-paths": "4.2.0", | ||
| "eslint": "9.39.2", | ||
| "eslint-plugin-prettier": "5.5.4", | ||
| "globals": "16.5.0", | ||
| "typescript": "5.9.3", | ||
| "typescript-eslint": "8.49.0" | ||
| }, | ||
| "jest": { | ||
| "moduleFileExtensions": [ | ||
| "js", | ||
| "json", | ||
| "ts" | ||
| ], | ||
| "rootDir": "src", | ||
| "testRegex": ".*\\.spec\\.ts$", | ||
| "transform": { | ||
| "^.+\\.(t|j)s$": "ts-jest" | ||
| }, | ||
| "collectCoverageFrom": [ | ||
| "**/*.(t|j)s" | ||
| ], | ||
| "coverageDirectory": "../coverage", | ||
| "testEnvironment": "node" | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletionssample/36-hmr-esm/src/app.module.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { Module } from '@nestjs/common'; | ||
| import { CatsModule } from './cats/cats.module'; | ||
| import { CoreModule } from './core/core.module'; | ||
| @Module({ | ||
| imports: [CoreModule, CatsModule], | ||
| }) | ||
| export class AppModule {} |
54 changes: 54 additions & 0 deletionssample/36-hmr-esm/src/cats/cats.controller.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { Test } from '@nestjs/testing'; | ||
| import { CatsController } from './cats.controller'; | ||
| import { CatsService } from './cats.service'; | ||
| import { Cat } from './interfaces/cat.interface'; | ||
| describe('CatsController', () => { | ||
| let catsController: CatsController; | ||
| let catsService: CatsService; | ||
| beforeEach(async () => { | ||
| const moduleRef = await Test.createTestingModule({ | ||
| controllers: [CatsController], | ||
| providers: [CatsService], | ||
| }).compile(); | ||
| catsService = moduleRef.get<CatsService>(CatsService); | ||
| catsController = moduleRef.get<CatsController>(CatsController); | ||
| }); | ||
| describe('findAll', () => { | ||
| it('should return an array of cats', async () => { | ||
| const cats: Cat[] = [ | ||
| { | ||
| age: 2, | ||
| breed: 'Bombay', | ||
| name: 'Pixel', | ||
| }, | ||
| ]; | ||
| // @ts-ignore | ||
| catsService.cats = cats; | ||
| expect(await catsController.findAll()).toBe(cats); | ||
| }); | ||
| }); | ||
| describe('create', () => { | ||
| it('should add a new cat', async () => { | ||
| const cat: Cat = { | ||
| age: 2, | ||
| breed: 'Bombay', | ||
| name: 'Pixel', | ||
| }; | ||
| const expectedCatArray = [cat]; | ||
| // @ts-ignore | ||
| expect(catsService.cats).toStrictEqual([]); | ||
| await catsController.create(cat); | ||
| // @ts-ignore | ||
| expect(catsService.cats).toStrictEqual(expectedCatArray); | ||
| }); | ||
| }); | ||
| }); |
33 changes: 33 additions & 0 deletionssample/36-hmr-esm/src/cats/cats.controller.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { Body, Controller, Get, Param, Post, UseGuards } from '@nestjs/common'; | ||
| import { Roles } from '../common/decorators/roles.decorator'; | ||
| import { RolesGuard } from '../common/guards/roles.guard'; | ||
| import { ParseIntPipe } from '../common/pipes/parse-int.pipe'; | ||
| import { CatsService } from './cats.service'; | ||
| import { CreateCatDto } from './dto/create-cat.dto'; | ||
| import { Cat } from './interfaces/cat.interface'; | ||
| @UseGuards(RolesGuard) | ||
| @Controller('cats') | ||
| export class CatsController { | ||
| constructor(private readonly catsService: CatsService) {} | ||
| @Post() | ||
| @Roles(['admin']) | ||
| async create(@Body() createCatDto: CreateCatDto) { | ||
| this.catsService.create(createCatDto); | ||
| } | ||
| @Get() | ||
| async findAll(): Promise<Cat[]> { | ||
| return this.catsService.findAll(); | ||
| } | ||
| @Get(':id') | ||
| findOne( | ||
| @Param('id', new ParseIntPipe()) | ||
| id: number, | ||
| ) { | ||
| // Retrieve a Cat instance by ID | ||
| console.log(id); | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletionssample/36-hmr-esm/src/cats/cats.module.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { Module } from '@nestjs/common'; | ||
| import { CatsController } from './cats.controller'; | ||
| import { CatsService } from './cats.service'; | ||
| @Module({ | ||
| controllers: [CatsController], | ||
| providers: [CatsService], | ||
| }) | ||
| export class CatsModule {} |
48 changes: 48 additions & 0 deletionssample/36-hmr-esm/src/cats/cats.service.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { Test } from '@nestjs/testing'; | ||
| import { CatsService } from './cats.service'; | ||
| import { Cat } from './interfaces/cat.interface'; | ||
| describe('CatsService', () => { | ||
| let catsService: CatsService; | ||
| beforeEach(async () => { | ||
| const moduleRef = await Test.createTestingModule({ | ||
| providers: [CatsService], | ||
| }).compile(); | ||
| catsService = moduleRef.get<CatsService>(CatsService); | ||
| }); | ||
| describe('findAll', () => { | ||
| it('should return an array of cats', async () => { | ||
| const result = [ | ||
| { | ||
| name: 'Frajola', | ||
| age: 2, | ||
| breed: 'Stray', | ||
| }, | ||
| ]; | ||
| //@ts-ignore | ||
| catsService.cats = result; | ||
| await expect(catsService.findAll()).resolves.toBe(result); | ||
| }); | ||
| }); | ||
| describe('create', () => { | ||
| it('should add a new cat', async () => { | ||
| const cat: Cat = { | ||
| name: 'Frajola', | ||
| age: 2, | ||
| breed: 'Stray', | ||
| }; | ||
| const expectedCatArray = [cat]; | ||
| //@ts-ignore | ||
| expect(catsService.cats).toStrictEqual([]); | ||
| catsService.create(cat); | ||
| //@ts-ignore | ||
| expect(catsService.cats).toStrictEqual(expectedCatArray); | ||
| }); | ||
| }); | ||
| }); |
15 changes: 15 additions & 0 deletionssample/36-hmr-esm/src/cats/cats.service.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { Injectable } from '@nestjs/common'; | ||
| import { Cat } from './interfaces/cat.interface'; | ||
| @Injectable() | ||
| export class CatsService { | ||
| private readonly cats: Cat[] = []; | ||
| create(cat: Cat) { | ||
| this.cats.push(cat); | ||
| } | ||
| findAll(): Promise<Cat[]> { | ||
| return Promise.resolve(this.cats); | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletionssample/36-hmr-esm/src/cats/dto/create-cat.dto.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { IsInt, IsString } from 'class-validator'; | ||
| export class CreateCatDto { | ||
| @IsString() | ||
| readonly name: string; | ||
| @IsInt() | ||
| readonly age: number; | ||
| @IsString() | ||
| readonly breed: string; | ||
| } |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.