Home > database >  connect ECONNREFUSED 127.0.0.1:3306 with using jest in docker
connect ECONNREFUSED 127.0.0.1:3306 with using jest in docker

Time:12-16

I'm trying to use jest codes before deploy to dev. So i made docker-compose.yml and put "npm test (ENV=test jest --runInBand --forceExit test/**.test.ts -u)" but it has error.

This is my local.yml file (for docker-compose.yml)

version: "3"

services:
  my-node:
    image: my-api-server:dev
    container_name: my_node
    # sleep 10 sec for db init
    command: bash -c "sleep 10; pwd; cd packages/server; yarn orm schema:sync -f ormconfig.dev.js; yarn db:migrate:run -f ormconfig.dev.js; npm test; cross-env ENV=dev node lib/server.js"
    ports:
      - "8082:8082"
    depends_on:
      - my-mysql
      - my-redis

  my-mysql:
    image: mysql:5.7
    container_name: my_mysql
    command: --character-set-server=utf8mb4 --sql_mode="NO_ENGINE_SUBSTITUTION" 
    ports:
      - "33079:3306"
    volumes:
      - ./init/:/docker-entrypoint-initdb.d/
    environment:
      - MYSQL_ROOT_PASSWORD=test
      - MYSQL_DATABASE=test
      - MYSQL_USER=test
      - MYSQL_PASSWORD=test
   
  my-redis:
    image: redis:6.2-alpine
    container_name: my_redis
    ports:
      - 6379:6379
    command: redis-server --requirepass test        

networks:
  default:
    external:
      name: my_nginx_default

and it's my ormconfig.dev.js file

module.exports = {
    type: 'mysql',
    host: 'my_mysql',
    port: 3306,
    username: 'test',
    password: 'test',
    database: 'test',
    entities: ['./src/modules/**/entities/*'],
    migrations: ['migration/dev/*.ts'],
    cli: { "migrationsDir": "migration/dev" }
}

But after i use docker-compose -f res/docker/local.yml up, it throws error after whole build and then jests. and then it opens server which does not have error.

Errors are like these below.

connect ECONNREFUSED 127.0.0.1:3306

and then

my_node     |     TypeError: Cannot read property 'subscriptionsPath' of undefined
my_node     |
my_node     |       116 | ): Promise<TestResponse<T>> => {
my_node     |       117 |   const req = request(server.app)
my_node     |     > 118 |     .post(server.apolloServer!.subscriptionsPath!)
my_node     |           |                                ^
my_node     |       119 |     .set('Accept', 'application/json')
my_node     |       120 |
my_node     |       121 |   if (token) {

I've tried to change entities path.

  • entities: ['./src/modules/**/entities/*']
  • entities: ['src/modules/**/entities/*']
  • entities: [__dirname '/src/modules/**/entities/*']

My entities are in the right path.

entities path

Here is my whole file structure File structure

Can anyone help this problem?

CodePudding user response:

in your module.export host:my-mysql

CodePudding user response:

Looking at the documentation, it could be an environment variable issue. As per the docs, the orm config file used is -

  1. From the environment variables. Typeorm will attempt to load the .env file using dotEnv if it exists. If the environment variables TYPEORM_CONNECTION or TYPEORM_URL are set, Typeorm will use this method.
  2. From the ormconfig.env.
  3. From the other ormconfig.[format] files, in this order: [js, ts, json, yml, yaml, xml].

Since you have not defined the first two, it must be defaulting to use the ormconfig.js file. There is no reason it should pick ormconfig.dev.js.

If you can, change the ormconfig.js file to be this -

module.exports = {
    type: 'mysql',
    host: 'my_mysql',
    port: 3306,
    username: 'test',
    password: 'test',
    database: 'test',
    entities: ['./src/modules/**/entities/*'],
    migrations: ['migration/dev/*.ts'],
    cli: { "migrationsDir": "migration/dev" }
}
  • Related