Skip to content

Commit

Permalink
test: add test case for multiple schemas (Nikaple#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikaple authored Nov 9, 2021
1 parent 42428c6 commit df8546b
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 1 deletion.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,41 @@ export class Config {
}
```

## Multiple Schemas

Just call `TypedConfigModule.forRoot` multiple times, and you're ready to go!

> PS: Please do not share any class or sub-class between schemas, or Nest.js won't know which class to inject.
```ts
// config.ts
export class FooConfig {
@IsString()
foo!: string;
}
export class BazConfig {
@IsString()
baz!: string;
}

// app.module.ts
@Module({
imports: [
// load FooConfig from config file
TypedConfigModule.forRoot({
schema: FooConfig,
load: fileLoader(),
}),
// load BazConfig from environment variables
TypedConfigModule.forRoot({
schema: BazConfig,
load: dotenvLoader(),
}),
],
})
export class AppModule {}
```

## Custom validate function

If the default `validate` function doesn't suite your use case, you can provide it like in the example below:
Expand Down
25 changes: 25 additions & 0 deletions tests/e2e/multiple-schemas.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { AppModule } from '../src/app.module';
import { BazConfig, FooConfig } from '../src/config.model';

describe('Multiple config schemas', () => {
let app: INestApplication;

it(`should merge configs from all loaders`, async () => {
const module = await Test.createTestingModule({
imports: [AppModule.withMultipleSchemas()],
}).compile();

app = module.createNestApplication();
await app.init();

const fooConfig = app.get(FooConfig);
const bazConfig = app.get(BazConfig);

expect(fooConfig.foo).toBe('bar');
expect(bazConfig.baz).toBe('qux');

await app.close();
});
});
1 change: 1 addition & 0 deletions tests/src/.env.part1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo=bar
1 change: 1 addition & 0 deletions tests/src/.env.part2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
baz=qux
27 changes: 26 additions & 1 deletion tests/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import {
dotenvLoader,
DotenvLoaderOptions,
} from '../../lib/loader/dotenv-loader';
import { Config, DirectoryConfig, TableConfig } from './config.model';
import {
BazConfig,
Config,
DirectoryConfig,
FooConfig,
TableConfig,
} from './config.model';

const loadYaml = function loadYaml(filepath: string, content: string) {
try {
Expand Down Expand Up @@ -61,6 +67,25 @@ export class AppModule {
};
}

static withMultipleSchemas(): DynamicModule {
return {
module: AppModule,
imports: [
TypedConfigModule.forRoot({
schema: FooConfig,
load: dotenvLoader({
envFilePath: join(__dirname, '../src/.env.part1'),
}),
}),
TypedConfigModule.forRoot({
schema: BazConfig,
load: dotenvLoader({
envFilePath: join(__dirname, '../src/.env.part2'),
}),
}),
],
};
}
static withToml(): DynamicModule {
return {
module: AppModule,
Expand Down
9 changes: 9 additions & 0 deletions tests/src/config.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,12 @@ export class DirectoryConfig {
@IsDefined()
public readonly table!: TableConfig;
}

export class FooConfig {
@IsString()
foo!: string;
}
export class BazConfig {
@IsString()
baz!: string;
}

0 comments on commit df8546b

Please sign in to comment.