Skip to content

Commit

Permalink
Add extra examples for Node.js (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnydgreen authored Oct 30, 2023
1 parent 693ccd9 commit b7ced5d
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 2 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
71 changes: 71 additions & 0 deletions examples/nifty-lil-tricks-testing-nodejs/nestjs_server.test.ts
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!");
});
});
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!");
});
});
9 changes: 7 additions & 2 deletions examples/nifty-lil-tricks-testing-nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 11 additions & 0 deletions examples/nifty-lil-tricks-testing-nodejs/tsconfig.json
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
}
}
15 changes: 15 additions & 0 deletions plugin_nestjs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 13 additions & 0 deletions plugin_postgresql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b7ced5d

Please sign in to comment.