diff --git a/README.md b/README.md index b8a55a6..7e9fa6f 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,21 @@ import * as testing from "https://deno.land/x/nifty_lil_tricks_testing/mod.ts"; npm install @nifty-lil-tricks/testing ``` +### TypeScript + +The TypeScript `tsconfig.json` must contain the following recommended settings: + +```jsonc +{ + "compilerOptions": { + "target": "ES2022", + "strict": true + } +} +``` + +Note, check the plugin READMEs for any additional TypeScript settings. + ## Features The following features are supported diff --git a/deno.json b/deno.json index 5f7b744..67bd135 100644 --- a/deno.json +++ b/deno.json @@ -35,6 +35,7 @@ "clean": "rm -rf ./cov && rm -rf ./html_cov && rm -rf ./npm", "check": "deno check *.ts examples/*.ts plugin_*/**/*.ts", "test:examples": "deno test --parallel --doc --allow-all examples/*.ts", + "test:examples:nodejs": "cd examples/nifty-lil-tricks-testing-nodejs && npm install && npm test", "test": "deno test --parallel --doc --allow-all --coverage=./cov", "test:all": "deno task test && deno task test:examples", "test:browser": "git grep --name-only \"This module is browser compatible.\" | grep -v deno.json | grep -v .github/workflows | grep -v _tools | xargs deno check --config browser-compat.tsconfig.json", diff --git a/examples/nifty-lil-tricks-testing-nodejs/nestjs_server.test.ts b/examples/nifty-lil-tricks-testing-nodejs/nestjs_server.test.ts new file mode 100644 index 0000000..8ade45d --- /dev/null +++ b/examples/nifty-lil-tricks-testing-nodejs/nestjs_server.test.ts @@ -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!"); + }); +}); diff --git a/examples/nifty-lil-tricks-testing-nodejs/nestjs_server_with_overrides.test.ts b/examples/nifty-lil-tricks-testing-nodejs/nestjs_server_with_overrides.test.ts new file mode 100644 index 0000000..e96252e --- /dev/null +++ b/examples/nifty-lil-tricks-testing-nodejs/nestjs_server_with_overrides.test.ts @@ -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!"); + }); +}); diff --git a/examples/nifty-lil-tricks-testing-nodejs/package.json b/examples/nifty-lil-tricks-testing-nodejs/package.json index 5b5aa44..42fab53 100644 --- a/examples/nifty-lil-tricks-testing-nodejs/package.json +++ b/examples/nifty-lil-tricks-testing-nodejs/package.json @@ -4,16 +4,21 @@ "description": "", "main": "index.js", "scripts": { - "test": "tap --ts" + "test": "tap --ts", + "check": "tsc --noEmit" }, "author": "Jonny Green", "license": "ISC", + "dependencies": { + "@nestjs/common": "^10.2.7", + "pg": "^8.11.3" + }, "devDependencies": { "@nifty-lil-tricks/testing": "latest", "@nifty-lil-tricks/testing-plugin-postgresql": "latest", + "@nifty-lil-tricks/testing-plugin-nestjs": "latest", "@types/pg": "^8.10.7", "@types/tap": "^15.0.8", - "pg": "^8.11.3", "tap": "^16.3.7", "ts-node": "^10.9.1", "typescript": "^5.1.3" diff --git a/examples/nifty-lil-tricks-testing-nodejs/tsconfig.json b/examples/nifty-lil-tricks-testing-nodejs/tsconfig.json new file mode 100644 index 0000000..90027c5 --- /dev/null +++ b/examples/nifty-lil-tricks-testing-nodejs/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "lib": ["ES2023"], + "module": "commonjs", + "target": "ES2022", + "esModuleInterop": true, + "strict": true, + "emitDecoratorMetadata": true, + "experimentalDecorators": true + } +} diff --git a/plugin_nestjs/README.md b/plugin_nestjs/README.md index 6faa5bd..d7e59b2 100644 --- a/plugin_nestjs/README.md +++ b/plugin_nestjs/README.md @@ -25,6 +25,21 @@ import { nestJsPlugin } from "https://deno.land/x/nifty_lil_tricks_testing/plugi npm install @nifty-lil-tricks/testing-plugin-nestjs ``` +### TypeScript + +The TypeScript `tsconfig.json` must contain the following recommended settings: + +```jsonc +{ + "compilerOptions": { + "target": "ES2022", + "strict": true, + "emitDecoratorMetadata": true, + "experimentalDecorators": true + } +} +``` + ## Features The following features are supported: diff --git a/plugin_postgresql/README.md b/plugin_postgresql/README.md index 1b088eb..d236e0c 100644 --- a/plugin_postgresql/README.md +++ b/plugin_postgresql/README.md @@ -25,6 +25,19 @@ import { postgreSqlPlugin } from "https://deno.land/x/nifty_lil_tricks_testing/p npm install @nifty-lil-tricks/testing-plugin-postgresql ``` +### TypeScript + +The TypeScript `tsconfig.json` must contain the following recommended settings: + +```jsonc +{ + "compilerOptions": { + "target": "ES2022", + "strict": true + } +} +``` + ## Features The following features are supported