-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add extra examples for Node.js (#24)
- Loading branch information
1 parent
693ccd9
commit b7ced5d
Showing
8 changed files
with
218 additions
and
2 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
71 changes: 71 additions & 0 deletions
71
examples/nifty-lil-tricks-testing-nodejs/nestjs_server.test.ts
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright 2023-2023 the Nifty li'l' tricks authors. All rights reserved. MIT license. | ||
|
||
import { Controller, Get, Injectable, Module } from "@nestjs/common"; | ||
import { | ||
setupTestsFactory, | ||
SetupTestsTeardown, | ||
} from "@nifty-lil-tricks/testing"; | ||
import { | ||
nestJsPlugin, | ||
PluginConfig, | ||
} from "@nifty-lil-tricks/testing-plugin-nestjs"; | ||
import t from "tap"; | ||
|
||
// In another file, load plugins as follows to generate a setupTests function: | ||
const { setupTests } = setupTestsFactory({ server: nestJsPlugin }); | ||
|
||
// In another file, define a NestJS app as follows: | ||
@Injectable() | ||
export class BasicAppService { | ||
getHello(): string { | ||
return "Hello, world!"; | ||
} | ||
} | ||
|
||
@Controller() | ||
export class BasicAppController { | ||
constructor(private readonly service: BasicAppService) {} | ||
|
||
@Get("/hello") | ||
getHello(): string { | ||
return this.service.getHello(); | ||
} | ||
} | ||
|
||
@Module({ | ||
imports: [], | ||
controllers: [BasicAppController], | ||
providers: [BasicAppService], | ||
}) | ||
export class BasicAppModule {} | ||
|
||
// Then one can use this in any test file as follows: | ||
t.test("Service", async (t) => { | ||
let teardownTests: SetupTestsTeardown; | ||
let origin: string; | ||
|
||
t.beforeEach(async () => { | ||
// Setup tests with configured plugins | ||
const result = await setupTests({ | ||
server: { | ||
appModule: BasicAppModule, | ||
} as PluginConfig, | ||
}); | ||
teardownTests = result.teardownTests; | ||
origin = result.outputs.server.output.origin; | ||
}); | ||
|
||
t.afterEach(async () => { | ||
// Teardown tests to restore environment after tests have run | ||
await teardownTests(); | ||
}); | ||
|
||
t.test("should test something that relies on the nestjs plugin", async (t) => { | ||
// Arrange & Act | ||
const response = await fetch(new URL("/hello", origin)); | ||
|
||
// Assert | ||
t.equal(response.status, 200); | ||
t.equal(await response.text(), "Hello, world!"); | ||
}); | ||
}); |
85 changes: 85 additions & 0 deletions
85
examples/nifty-lil-tricks-testing-nodejs/nestjs_server_with_overrides.test.ts
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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2023-2023 the Nifty li'l' tricks authors. All rights reserved. MIT license. | ||
|
||
import { Controller, Get, Injectable, Module } from "@nestjs/common"; | ||
import { | ||
setupTestsFactory, | ||
SetupTestsTeardown, | ||
} from "@nifty-lil-tricks/testing"; | ||
import { | ||
nestJsPlugin, | ||
PluginConfig, | ||
ProviderOverrideType, | ||
} from "@nifty-lil-tricks/testing-plugin-nestjs"; | ||
import t from "tap"; | ||
|
||
// In another file, load plugins as follows to generate a setupTests function: | ||
const { setupTests } = setupTestsFactory({ server: nestJsPlugin }); | ||
|
||
// In another file, define a NestJS app as follows: | ||
@Injectable() | ||
export class BasicAppService { | ||
getHello(): string { | ||
return "Hello, world!"; | ||
} | ||
} | ||
|
||
@Controller() | ||
export class BasicAppController { | ||
constructor(private readonly service: BasicAppService) {} | ||
|
||
@Get("/hello") | ||
getHello(): string { | ||
return this.service.getHello(); | ||
} | ||
} | ||
|
||
@Module({ | ||
imports: [], | ||
controllers: [BasicAppController], | ||
providers: [BasicAppService], | ||
}) | ||
export class BasicAppModule {} | ||
|
||
// In another file, define a NestJS app overrides for testing as follows: | ||
@Injectable() | ||
export class NewAppService { | ||
getHello(): string { | ||
return "Ahoy!"; | ||
} | ||
} | ||
|
||
// Then one can use this in any test file as follows: | ||
t.test("Service", async (t) => { | ||
let teardownTests: SetupTestsTeardown; | ||
let origin: string; | ||
|
||
t.beforeEach(async () => { | ||
// Setup tests with configured plugins | ||
const result = await setupTests({ | ||
server: { | ||
appModule: BasicAppModule, | ||
providers: [{ | ||
type: ProviderOverrideType.CLASS, | ||
typeOrToken: BasicAppService, | ||
useClass: NewAppService, | ||
}], | ||
} as PluginConfig, | ||
}); | ||
teardownTests = result.teardownTests; | ||
origin = result.outputs.server.output.origin; | ||
}); | ||
|
||
t.afterEach(async () => { | ||
// Teardown tests to restore environment after tests have run | ||
await teardownTests(); | ||
}); | ||
|
||
t.test("should test something that relies on the nestjs plugin", async (t) => { | ||
// Arrange & Act | ||
const response = await fetch(new URL("/hello", origin)); | ||
|
||
// Assert | ||
t.equal(response.status, 200); | ||
t.equal(await response.text(), "Ahoy!"); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"compilerOptions": { | ||
"lib": ["ES2023"], | ||
"module": "commonjs", | ||
"target": "ES2022", | ||
"esModuleInterop": true, | ||
"strict": true, | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true | ||
} | ||
} |
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