El-Baraa Radwan
El-Baraa Radwan

Reputation: 13

Prisma Error: P1017: Server has closed the connection

I'm following a course, here is the link

halfway through the course he started testing the code but made another database connection and created a new .env name it .env.test

in .env

PORT=3002
JWT_SECRET=secret
DB_USERNAME=root
DB_PASSWORD=MySQL.Bebon3232
DB_HOST=db_test
DB_NAME=learning_nestjs
DATABASE_URL="mysql://root:MySQL.Bebon3232@localhost:3307/learning_nestjs"

in .env.test

PORT=3002
JWT_SECRET=secret
DB_USERNAME=root
DB_PASSWORD=MySQL.Bebon3232
DB_HOST=db_test
DB_NAME=learning_nestjs
DATABASE_URL="mysql://root:MySQL.Bebon3232@localhost:3308/learning_nestjs"

the only difference is the port number

while in docker-compose

version: '3.8'

services:
  dev_db:
    image: mysql:latest
    restart: always
    ports:
      - 3307:3306
    expose:
      - 3307
    volumes:
      - ./db_data:/data/db
    environment:
      - MYSQL_ROOT_USER=${DB_USERNAME}
      - MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
      - MYSQL_DATABASE=${DB_NAME}

  test_db:
    image: mysql:latest
    restart: always
    ports:
      - 3308:3306
    expose:
      - 3308
    volumes:
      - ./db_data:/data/db
    environment:
      - MYSQL_ROOT_USER=${DB_USERNAME}
      - MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
      - MYSQL_DATABASE=${DB_NAME}

when running the code everything works just fine, untell the testing part.

this is the scripts for running the code

"prisma:dev:deploy": "prisma migrate deploy",
"db:dev:up": "docker compose up dev_db -d",
"db:dev:rm": "docker compose rm dev_db -s -f -v",
"db:dev:restart": "yarn db:dev:rm && yarn db:dev:up && yarn prisma:dev:deploy",

"prisma:test:deploy": "dotenv -e .env.test -- prisma migrate deploy",
"db:test:up": "docker compose up test_db -d",
"db:test:rm": "docker compose rm test_db -s -f -v",
"db:test:restart": "yarn db:test:rm && yarn db:test:up && yarn prisma:test:deploy",

"build": "nest build",
"start": "nest start",
"start:dev": "nest start --watch",

"pretest:e2e": "yarn db:test:restart",
"test:e2e": "dotenv -e .env.test -- jest --watchAll --no-cache --config ./test/jest-e2e.json"

I run test:e2e which runs the following commands by order before it runs itself

  1. yarn compose rm test_db -s -f -v
  2. yarn compose up test_db -d
  3. dotenv -e .env.test -- prisma migrate deploy

and it stops when Prisma deploys and shows this error

Error: P1017: Server has closed the connection.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Even though running the commands separately

npx dotenv -e .env.test -- prisma migrate deploy

npx dotenv -e .env.test -- jest --watchAll --no-cache --config ./test/jest-e2e.json

work just fine alone but not as a group of commands

it should pass the test that has been made, which is

import { INestApplication, ValidationPipe } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { AppModule } from '../src/app.module';

describe('app e2e', () => {
  let app: INestApplication;

  beforeAll(async () => {
    const moduleRef = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();

    app = moduleRef.createNestApplication();
    app.useGlobalPipes(
      new ValidationPipe({
        whitelist: true,
      }),
    );
    await app.init();
  });

  afterAll(async () => {
    await app.close();
  });

  it.todo('should pass');
});

Note that the database containers (dev_db & test_db) work completely

Upvotes: 0

Views: 186

Answers (0)

Related Questions