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

TypeOrm listeners not executed during e2e testing #3243

Closed
igern opened this issue Oct 23, 2019 · 7 comments
Closed

TypeOrm listeners not executed during e2e testing #3243

igern opened this issue Oct 23, 2019 · 7 comments
Labels
needs triage This issue has not been looked into

Comments

@igern
Copy link

igern commented Oct 23, 2019

Bug Report

Current behavior

The issue manifests when running e2e test with typeorm and you try to save something to the the database. The corresponsing typeorm listerners are not being executed. These are shown in the code below.
The issue does not manifest when the application is ran normally.

Input Code

Individual.entity.ts

...
  @BeforeInsert()
  @BeforeUpdate()
  private encryptPassword(): void {
    if (this.tmpPassword !== this.password) {
      this.hashPassword();
      this.tmpPassword = this.password;
    }
  }

  @BeforeInsert()
  private hashPassword() {
    this.passwordSalt = bcrypt.genSaltSync();
    this.password = bcrypt.hashSync(this.password, this.passwordSalt);
  }
...

e2e.spec.ts

describe('AppController (e2e)', () => {
  let app: INestApplication;
  let module: TestingModule;
  let connection: Connection

  beforeAll(async () => {
    module = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();
    connection = module.get(Connection)
    app = module.createNestApplication();
    await app.init();
  })

  afterAll(async () => {
    module.close();
  })

  beforeEach( async () => {
    await connection.synchronize(true)
  })
...

Expected behavior

I expect the behavior in the two listeners in the code to be executed before they are inserted into the database.

Possible Solution

Environment


Nest version: 6.7.2

 
For Tooling issues:
- Node version: 10.16.3
- Platform: Linux

Others:

@igern igern added the needs triage This issue has not been looked into label Oct 23, 2019
@kamilmysliwiec
Copy link
Member

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.

@igern
Copy link
Author

igern commented Oct 23, 2019

This is not a question. I believe there is a bug when it comes to e2e testing with databases using typeorm. The behavior when running my Nest app normally and when it is run using the test framework from @nestjs/testing is different from one another.
When i run it normally, TypeOrm listeners as described in the issue, are being executed properly causing the password to be hashed before they are saved in the database
When i run it in a test, they are not executed, causing different non hashed passwords to be saved.

I can not find any working examples/tests that documents this odd behavior.

@jmcdo29
Copy link
Member

jmcdo29 commented Oct 24, 2019

Do you have a repository that is mimicking this behavior? It seems strange that it would work in a normal start up and not in the e2e context, as nothing changes there. Could this be an issue with TypeORM directly?

@igern
Copy link
Author

igern commented Oct 24, 2019

I have made a repository that tests this behavior:
https://github.com/igern/Nestjs-Typeorm-e2e-testing
The repository just tests whether the TypeOrm listener is executed before insert.
It also contains a command to simply run the service so you can see the TypeOrm listeners are being executed normally outside of testing

@igern
Copy link
Author

igern commented Oct 28, 2019

The problem was assumed to be caused by the jest environment not having access to the entity class fully causing it to not have the TypeOrm listeners and therefore not executing them.
The fix in this case was to change the string used to find our entities in the TypeOrmModuleOptions from

entities: ["dist/**/*.entity.{ts,is}"];

to

entities: [ __dirname + '/**/*.entity.{ts,js}'];

@mmbmatteo
Copy link

mmbmatteo commented Nov 2, 2019

Found the same here.

Thank you @igern for the workaround 😄 👍

Can use something like this for both testing and running the application server (quick and dirty solution, probably you want to use different ormconfig files)

const entitiesPath = process.env.NODE_ENV === 'test' ? __dirname + "/**/*.entity{.ts,.js}" : "dist/**/*.entity{.ts,.js}"

@lock
Copy link

lock bot commented Jan 31, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 31, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

4 participants