entrostat / typeorm-test-transactions

Automatically rollback database changes after each test is completed when using TypeORM
MIT License
63 stars 5 forks source link

Tests won't fail; is runInTransaction running? #19

Closed drnic closed 4 years ago

drnic commented 4 years ago

I am reproducing an issue I found in my own code base -- code inside await runInTransaction(async () => { ... }) does not fail its expectations; and it swallows console.log output. So perhaps the block inside runInTransaction is not running at all?

This commit includes a failing test but the test suite doesn't fail.

The output I see is:

 PASS  tests/rollback.spec.ts
  ● Console

    console.log
      log outside runInTransaction

      at Object.<anonymous> (tests/rollback.spec.ts:44:17)

    console.log
      log after runInTransaction

      at Object.<anonymous> (tests/rollback.spec.ts:49:17)

 PASS  tests/entity-creation.spec.ts
 PASS  tests/rollback-second-file.spec.ts

But I should also see log inside runInTransaction and the spec should not pass.

Ideas?

kerren commented 4 years ago

I agree that this is a bug, I think I can see why it wasn't running, it's because Jest expects a function so the runInTransaction wrapper actually returns a function and doesn't run it.

Unfortunately, that means that a number of my tests weren't running either :disappointed:

I'm going to push up a fix to resolve this, for your case, you'll have to run the returned value, eg:

        await runInTransaction(async () => {
            console.log("create another User ... 2");

            const user = User.create({ email });
            await user.save();
            const found = await User.findOne({
                where: { email },
            });
            expect(found).toBeDefined();
            expect(false).toBeTruthy();
        })();
kerren commented 4 years ago

I've created a pull request here which resolves the issue (but it does require changes in your tests because you'll have to call the transactions you created). I apologise for the inconvenience this may have caused and I hope it didn't take up too much of your time!

Thanks for pointing it out :smile:

drnic commented 4 years ago

@Kerren-Entrostat 👍