albatrosary's blog

UI/UXとエンタープライズシステム

ormconfig の記述

Jest's __dirname is src and NestJS's __dirname is dist. So if you want to use TypeORM's ormconfig for unit tests as well, we need to write a different definition.

Jest

import {ConnectionOptions} from 'typeorm';

const config: ConnectionOptions = {
  type: 'mysql',
  host: 'localhost',
  port: 3306,
  username: 'root',
  password: 'password',
  database: 'TestDB',
  synchronize: false,
  logging: false,
  entities: [
    "src/entity/**/*.ts"
  ]
}

export = config;

Nest

import {ConnectionOptions} from 'typeorm';

const config: ConnectionOptions = {
  type: 'mysql',
  host: 'localhost',
  port: 3306,
  username: 'root',
  password: 'password',
  database: 'TestDB',
  synchronize: true,
  logging: false,
  entities: [
    "dist/entity/**/*.js"
  ],
  migrations: [
    "dist/migration/**/*.js"
  ],
  subscribers: [
    "dist/subscriber/**/*.js"
  ],
  cli: {
    entitiesDir: 'src/entity',
    migrationsDir: 'src/migration',
  },
}

export = config;

But by using ts-node we can use src.

  "scripts": {
    ...
    "serve": "ts-node-dev src/main.ts",
    ...
  },

  "devDependencies": {
    ...
    "ts-node": "^9.0.0",
    "ts-node-dev": "^1.1.1",
    ...