Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
AkashRajpurohit opened this issue Jan 2, 2019 · 6 comments

Comments

@AkashRajpurohit
Copy link

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

@shall-git
Copy link

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

@benawad
Copy link
Owner

benawad commented Jan 2, 2019

I think the solution is to set synchronize to false:

"synchronize": true,

And to drop the database before all the tests run

@shall-git
Copy link

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

@AkashRajpurohit
Copy link
Author

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

@benawad
Copy link
Owner

benawad commented Jan 3, 2019

@AkashRajpurohit I think that's a good workaround

@harry-nguyen22
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants