graphql-ts-server-boilerplate icon indicating copy to clipboard operation
graphql-ts-server-boilerplate copied to clipboard

trying to create same table twice while running tests with same mock email and password

Open AkashRajpurohit opened this issue 6 years ago • 6 comments

I was trying from lesson 14. Fixing Connection Error, while following along with you i found that my test cases were failing when i re run the test with same mock email and password in register.test.ts I tried logging the queries from postgres and found this line

console.log node_modules/typeorm/platform/PlatformTools.js:214
      query: CREATE TABLE "users" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "email" character varying(255) NOT NULL, "password" text NOT NULL, "confirmed" boolean NOT NULL DEFAULT false, CONSTRAINT "PK_a3ffb1c0c8416b9fc6f907b7433" PRIMARY KEY ("id"))
    console.log node_modules/typeorm/platform/PlatformTools.js:217
      query failed: CREATE TABLE "users" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "email" character varying(255) NOT NULL, "password" text NOT NULL, "confirmed" boolean NOT NULL DEFAULT false, CONSTRAINT "PK_a3ffb1c0c8416b9fc6f907b7433" PRIMARY KEY ("id"))
    console.log node_modules/typeorm/platform/PlatformTools.js:217
      error: { error: duplicate key value violates unique constraint "pg_type_typname_nsp_index"
          at Connection.Object.<anonymous>.Connection.parseE

So I guess it is trying to create the users table twice and throwing error while creating it second time and thereby failing my subsequent tests.

It works if i manually change the email and password before every test but i see you don't change the email every time you test yet it works. Can't figure out why it isn't in my case

AkashRajpurohit avatar Jan 02 '19 06:01 AkashRajpurohit

I get the same error, I'm on lesson number 10, and after setting global config for jest I cannot test anything cause I'm on Windows and I cannot use process.env.TEST_HOST = ".....". Anyway as a temporary workaround I hardcoded 127.0.0.1://0 in the resolvers.test.ts, but every time the beforeAll is called I see the same error as above... So I removed the beforeAll but I cannot execute the first test

shall-git avatar Jan 02 '19 15:01 shall-git

I think the solution is to set synchronize to false: https://github.com/benawad/graphql-ts-server-boilerplate/blob/45b735f7f15514e65c9b085516c7fa35e3178426/ormconfig.json#L29

And to drop the database before all the tests run

benawad avatar Jan 02 '19 17:01 benawad

I tried, but with this setting I receive another error "'users' relation doesn't exist", so I revert back to the other config, I'm skipping tests that need to connect to the DB

shall-git avatar Jan 02 '19 21:01 shall-git

Ben I tried something else to overcome this issue but I'm not sure if its the ideal solution. let me know if I'm doing it wrong.

What I did was I removed the dropSchema from ormconfig. After that the tests were running but the database was not getting cleared and the next subsequent test would throw error of email Id already exist.. so fir this I created a deleteSchema.ts in utils in which I clear all the entries from the schema passed as parameter and call this function in afterAll event of tests (before closing the connection) In this way the database used to be cleared and tests are working..

this is something I came up with as a workaround

AkashRajpurohit avatar Jan 03 '19 01:01 AkashRajpurohit

@AkashRajpurohit I think that's a good workaround

benawad avatar Jan 03 '19 19:01 benawad

I have the same issues. I think because we use async method in beforeAll() . I should be beforeAll(async done => { await createTypeormConn() done() }) Because our TypeOrm will drop Schema every time new connection was established and it should be call before every tests.

harry-nguyen22 avatar Sep 17 '19 08:09 harry-nguyen22