Skip to content

Commit

Permalink
test: add attempt to test the new feature added
Browse files Browse the repository at this point in the history
Since there's no other way to check TypeScript types within this project
without addind some 3rd-party lib, I've made this approach: let TSC
complains about typings while running the npm-script `test:integration`.
  • Loading branch information
micalevisk committed Oct 1, 2021
1 parent 259d793 commit 87972b3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/jest-e2e.json
@@ -1,4 +1,9 @@
{
"globals": {
"ts-jest": {
"tsconfig": "./tests/tsconfig.json"
}
},
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": ".",
"testEnvironment": "node",
Expand Down
29 changes: 29 additions & 0 deletions tests/src/app.module.ts
Expand Up @@ -7,15 +7,44 @@ import { ConfigService } from '../../lib/config.service';
import databaseConfig from './database.config';
import nestedDatabaseConfig from './nested-database.config';

type Config = {
database: ConfigType<typeof databaseConfig> & {
driver: ConfigType<typeof nestedDatabaseConfig>
};
};

@Module({})
export class AppModule {
constructor(
private readonly configService: ConfigService,
// The following is the same object as above but narrowing its types
private readonly configServiceNarrowed: ConfigService<Config, true>,
@Optional()
@Inject(databaseConfig.KEY)
private readonly dbConfig: ConfigType<typeof databaseConfig>,
) {}

/**
* This method is not meant to be used anywhere! It just here for testing
* types defintions while runnig test suites (in some sort).
* If some typings doesn't follows the requirements, Jest will fail due to
* TypeScript errors.
*/
private noop(): void {
// Arrange
const identityString = (v: string) => v;
const identityNumber = (v: number) => v;
// Act
const knowConfig = this.configServiceNarrowed.get<Config['database']>('database');
// Assert
// We don't need type assertions bellow anymore since `knowConfig` is not
// expected to be `undefined` beforehand.
identityString(knowConfig.host);
identityNumber(knowConfig.port);
identityString(knowConfig.driver.host);
identityNumber(knowConfig.driver.port);
}

static withCache(): DynamicModule {
return {
module: AppModule,
Expand Down
14 changes: 14 additions & 0 deletions tests/tsconfig.json
@@ -0,0 +1,14 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"rootDir": "../",
"strictNullChecks": true
},
"include": [
"**/*.spec.ts",
],
"exclude": [
"node_modules",
"dist"
]
}

0 comments on commit 87972b3

Please sign in to comment.