-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v1.1.0 - switched to asyncLocalStorage solution
- Loading branch information
Showing
9 changed files
with
2,273 additions
and
165 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,74 +1,55 @@ | ||
import { InjectTransformModule } from "./inject-transform.module.mjs"; | ||
import { Module } from "@nestjs/common"; | ||
import { Body, Controller, Module, Post } from "@nestjs/common"; | ||
import { NestFactory } from "@nestjs/core"; | ||
import { InjectTransform } from "./decorators/index.mjs"; | ||
import { plainToInstance } from "class-transformer"; | ||
import { InjectLifecycleError } from "./exceptions.mjs"; | ||
import { default as supertest } from "supertest"; | ||
|
||
@Module({ imports: [InjectTransformModule] }) | ||
class TestAppModuleWithImport {} | ||
const x = Symbol("eh"); | ||
|
||
class TestSubject { | ||
@InjectTransform((params, tokenValue: number) => params.value + tokenValue, { | ||
inject: [x], | ||
}) | ||
x: number; | ||
} | ||
|
||
@Module({ imports: [] }) | ||
class TestAppModuleWithoutImport {} | ||
@Controller() | ||
class TestController { | ||
@Post("/") | ||
returnInject(@Body() subject: TestSubject) { | ||
return plainToInstance(TestSubject, subject); | ||
} | ||
} | ||
|
||
@Module({ | ||
imports: [InjectTransformModule.forRoot({ ignoreInjectLifecycle: true })], | ||
imports: [InjectTransformModule], | ||
controllers: [TestController], | ||
providers: [{ provide: x, useValue: 10 }], | ||
}) | ||
class TestAppModuleWithOptions {} | ||
class TestAppModuleWithImport {} | ||
|
||
describe("InjectTransformModule", () => { | ||
beforeEach(() => InjectTransformModule.clearInjectTransformModule()); | ||
|
||
it("should set the global module when injected", async () => { | ||
await NestFactory.createApplicationContext(TestAppModuleWithImport); | ||
expect( | ||
InjectTransformModule.getInjectTransformModule() | ||
).not.toBeUndefined(); | ||
}); | ||
|
||
it("should not have a global module when not injected", async () => { | ||
await NestFactory.createApplicationContext(TestAppModuleWithoutImport); | ||
expect(InjectTransformModule.getInjectTransformModule()).toBeUndefined(); | ||
}); | ||
|
||
it("should hint at import when using transform container without global module", () => { | ||
it("should hint at import when no context is found", () => { | ||
expect(() => InjectTransformModule.getInjectTransformContainer()).toThrow( | ||
"Did you forget to import the InjectTransformModule?" | ||
); | ||
}); | ||
|
||
describe("Module options", () => { | ||
it("should consider module options", async () => { | ||
const app = await NestFactory.createApplicationContext( | ||
TestAppModuleWithOptions | ||
); | ||
await app.close(); | ||
it("should inject during controller requests", async () => { | ||
const app = await NestFactory.create(TestAppModuleWithImport); | ||
await app.init(); | ||
|
||
// Accessing the inject transform container when the app is closed should throw an error, | ||
// unless it has taken the `ignoreLifecycleErrors` module option into account, which this | ||
// test wants to assert. | ||
expect( | ||
InjectTransformModule.getInjectTransformContainer() | ||
).not.toBeUndefined(); | ||
}); | ||
await supertest(app.getHttpServer()) | ||
.post("/") | ||
.send({ x: 5 }) | ||
.expect(201) | ||
.expect({ x: 15 }); | ||
|
||
it("should override module options with decorator options", async () => { | ||
const app = await NestFactory.createApplicationContext( | ||
TestAppModuleWithOptions | ||
); | ||
await app.close(); | ||
|
||
class Test { | ||
@InjectTransform(() => 5, { ignoreInjectLifecycle: false }) | ||
x: number; | ||
} | ||
await app.close(); | ||
}); | ||
|
||
// Accessing the inject transform container when the app is closed should throw an error, | ||
// unless it has taken the `ignoreLifecycleErrors` module option into account, which this | ||
// test wants to assert to be overridden by the decorator options. | ||
expect(() => plainToInstance(Test, { x: 3 })).toThrow( | ||
InjectLifecycleError | ||
); | ||
}); | ||
it("should error without an active application", async () => { | ||
expect(() => plainToInstance(TestSubject, { x: 5 })).toThrow(); | ||
}); | ||
}); |
4 changes: 1 addition & 3 deletions
4
lib/interfaces/inject-transform-container-options.interface.mts
This file contains 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 |
---|---|---|
@@ -1,3 +1 @@ | ||
export interface InjectTransformContainerOptions { | ||
ignoreInjectLifecycle?: boolean; | ||
} | ||
export interface InjectTransformContainerOptions {} |
File renamed without changes.
Oops, something went wrong.