viniciusjssouza / typeorm-transactional-tests

Add transactional tests to typeorm projects
MIT License
54 stars 11 forks source link

Not rolling back #1

Open dan-klasson opened 4 years ago

dan-klasson commented 4 years ago

Doesn't seem to be working:

# e2e.spec.ts

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

  beforeAll(async () => {
    const moduleFixture = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();
    const appPreInit = moduleFixture.createNestApplication();
    app = await appPreInit.init();
  });

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

# transactional-context.ts

// eslint-disable-next-line import/no-extraneous-dependencies
import { TransactionalTestContext } from 'typeorm-transactional-tests';
import { getConnection } from 'typeorm';

let transactionalContext: TransactionalTestContext;
let connection;

// @ts-ignore
global.beforeEach(async () => {
  connection = getConnection();
  transactionalContext = new TransactionalTestContext(connection);
  transactionalContext.start();
});

// @ts-ignore
global.afterEach(async () => {
  console.log('cleaning up..');
  transactionalContext.finish();
});

Output:

npm run test:e2e

> backend@0.0.1 test:e2e /Users/danklasson/Code/analyze/backend
> jest --config ./test/jest-e2e.json

  console.log node_modules/typeorm/platform/PlatformTools.js:197
    query: START TRANSACTION

  console.log node_modules/typeorm/platform/PlatformTools.js:197
    query: START TRANSACTION

  console.log node_modules/typeorm/platform/PlatformTools.js:197
    query: INSERT INTO `project`(`id`, `name`) VALUES (DEFAULT, ?) -- PARAMETERS: ["Ondricka, Stark and Breitenberg"]

  console.log node_modules/typeorm/platform/PlatformTools.js:197
    query: COMMIT

  console.log node_modules/typeorm/platform/PlatformTools.js:197
    query: SELECT `Project`.`id` AS `Project_id`, `Project`.`name` AS `Project_name` FROM `project` `Project`

  console.log src/project/project.e2e-spec.ts:33
    [ Project { id: 1, name: 'Ondricka, Stark and Breitenberg' } ]

 PASS  src/project/project.e2e-spec.ts
  e2e
    ✓ should fetch projects (137ms)
    ✓ should fetch projects2 (31ms)

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        4.581s, estimated 5s
Ran all test suites.

  console.log test/test.transactional-context.ts:17
    cleaning up..

  console.log node_modules/typeorm/platform/PlatformTools.js:197
    query: ROLLBACK

  console.log node_modules/typeorm/platform/PlatformTools.js:197
    query: START TRANSACTION

  console.log node_modules/typeorm/platform/PlatformTools.js:197
    query: START TRANSACTION

  console.log node_modules/typeorm/platform/PlatformTools.js:197
    query: INSERT INTO `project`(`id`, `name`) VALUES (DEFAULT, ?) -- PARAMETERS: ["Daugherty, Lesch and Schmidt"]

  console.log node_modules/typeorm/platform/PlatformTools.js:197
    query: COMMIT

  console.log node_modules/typeorm/platform/PlatformTools.js:197
    query: SELECT `Project`.`id` AS `Project_id`, `Project`.`name` AS `Project_name` FROM `project` `Project`

  console.log src/project/project.e2e-spec.ts:53
    [ Project { id: 1, name: 'Ondricka, Stark and Breitenberg' },
      Project { id: 2, name: 'Daugherty, Lesch and Schmidt' } ]

  console.log test/test.transactional-context.ts:17
    cleaning up..

  console.log node_modules/typeorm/platform/PlatformTools.js:197
    query: ROLLBACK
highflying commented 4 years ago

Same here, everything is getting committed and not rolled back.

ewenw commented 4 years ago

Same issue. Anyone able to resolve this?

mmarkell commented 4 years ago

Same issue for me as well

dan-klasson commented 4 years ago

I went with this other library: https://github.com/entrostat/typeorm-test-transactions

ewenw commented 4 years ago

I went with this other library: https://github.com/entrostat/typeorm-test-transactions

Does this library work for you? For some reason, it's still not rolling back for me.

mmarkell commented 4 years ago

I "fixed" the issue by putting the source code directly into my repo, so the version of typeorm is the same between the typeorm-transactional-tests code and my own code

dan-klasson commented 4 years ago

@ewenw Yes. Make sure you wrap your test functions with runInTransaction

ewenw commented 4 years ago

I did do that. Which typeOrm version are you running? Thanks for the help!

dan-klasson commented 4 years ago

@ewenw 6.2.0. Ask on that issue tracker and tag me if you need help.

omerd-cyera commented 2 years ago

I "fixed" the issue by putting the source code directly into my repo, so the version of typeorm is the same between the typeorm-transactional-tests code and my own code

If this is the case, make sure you are using the same version of typeorm as the library. or force dependency hoisting. I don't think its an issue with this library isn't it?