RobinCK / typeorm-fixtures

:pill: Fixtures loader for typeorm πŸ‡ΊπŸ‡¦
https://robinck.github.io/typeorm-fixtures/
MIT License
538 stars 46 forks source link

Stuck in using this library with nestjs for e2e testing #68

Open Feelthewind opened 4 years ago

Feelthewind commented 4 years ago

Hi. Thank you for this awesome project. I'm trying to use it with nestjs for e2e testing and i keep getting this error.

TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):
    test/test.utils.ts:113:11 - error TS2571: Object is of type 'unknown'.

    113           entity.constructor.name,
                  ~~~~~~
    test/test.utils.ts:114:17 - error TS2345: Argument of type 'unknown' is not assignable to parameter of type 'DeepPartial<{}>'.

    114         )).save(entity);

/test/test.utils.ts

import { Injectable } from "@nestjs/common";
import * as fs from "fs";
import * as path from "path";
import {
  Builder,
  fixturesIterator,
  Loader,
  Parser,
  Resolver,
} from "typeorm-fixtures-cli/dist";
import { DatabaseService } from "../src/database/database.service";

@Injectable()
export class TestUtils {
  databaseService: DatabaseService;

  constructor(databaseService: DatabaseService) {
    if (process.env.NODE_ENV !== "test") {
      throw new Error("ERROR-TEST-UTILS-ONLY-FOR-TESTS");
    }
    this.databaseService = databaseService;
  }

  async loadFixtures(fixturesPath: string) {
    const connection = await this.databaseService.connection;
    try {
      const loader = new Loader();
      loader.load(path.resolve("./fixtures/User.yml"));

      const resolver = new Resolver();
      const fixtures = resolver.resolve(loader.fixtureConfigs);
      const builder = new Builder(connection, new Parser());

      for (const fixture of fixturesIterator(fixtures)) {
        const entity = await builder.build(fixture);
        (await this.databaseService.getRepository(
          entity.constructor.name,
        )).save(entity);
      }
    } catch (err) {
      throw err;
    } finally {
      if (connection) {
        await connection.close();
      }
    }
  }
}

/src/database/database.service.ts

import { Injectable } from "@nestjs/common";
import { InjectConnection } from "@nestjs/typeorm";
import { Connection } from "typeorm";

@Injectable()
export class DatabaseService {
  constructor(@InjectConnection() public connection: Connection) {}

  async getRepository<T>(entity) {
    return this.connection.getRepository(entity);
  }
}

/test/fixtures/User.yml

entity: User
items:
  user1:
    name: '{{name.firstName}}'
    email: '{{internet.email}}'
    password: 'password',
  user2:
    name: '{{name.firstName}}'
    email: '{{internet.email}}'
    password: 'password',

Any help would be appreciated!

RobinCK commented 4 years ago

try this

const entity: any = await builder.build(fixture);
Feelthewind commented 4 years ago

Thank you for the help, RobinCK. The error i mentioned disappeared, but another one shows up. I checked that it succeed resolving User.yml file by logging.

[
        {
          parameters: {},
          processor: undefined,
          entity: 'User',
          name: 'user1',
          data: {
            name: '{{name.firstName}}',
            email: '{{internet.email}}',
            password: 'password'
          },
          dependencies: []
        },
        {
          parameters: {},
          processor: undefined,
          entity: 'User',
          name: 'user2',
          data: {
            name: '{{name.firstName}}',
            email: '{{internet.email}}',
            password: 'password'
          },
          dependencies: []
        }
      ]
Cannot use a pool after calling end on the pool

      at Pool.connect (../node_modules/pg-pool/index.js:148:19)
      at ../src/driver/postgres/PostgresDriver.ts:721:25
      at PostgresDriver.Object.<anonymous>.PostgresDriver.obtainMasterConnection (../src/driver/postgres/PostgresDriver.ts:720:16)
      at PostgresQueryRunner.Object.<anonymous>.PostgresQueryRunner.connect (../src/driver/postgres/PostgresQueryRunner.ts:90:58)
      at PostgresQueryRunner.<anonymous> (../src/driver/postgres/PostgresQueryRunner.ts:163:55)
      at step (../node_modules/tslib/tslib.js:136:27)
      at Object.next (../node_modules/tslib/tslib.js:117:57)
      at ../node_modules/tslib/tslib.js:110:75
ReferenceError: You are trying to `import` a file after the Jest environment has been torn down.

      at Object.getCodec (../node_modules/iconv-lite/lib/index.js:65:27)
      at Object.getDecoder (../node_modules/iconv-lite/lib/index.js:127:23)
      at getDecoder (../node_modules/raw-body/index.js:45:18)
      at readStream (../node_modules/raw-body/index.js:180:15)
      at getRawBody (../node_modules/raw-body/index.js:108:12)

I make use of the connection from @InjectConnection() decorator. It might have nothing to do with this library. But i have no idea how to solve it 😭

RobinCK commented 4 years ago

if to use CLI, everything works fine?

Feelthewind commented 4 years ago

Yes. I just tried it and if i use cli, everything works okay. But for e2e testing, i guess i need to do it programmatically so.

RobinCK commented 4 years ago

Can you create a mini-sample project? For live check

Feelthewind commented 4 years ago

Thank you for your time! https://github.com/Feelthewind/parking-tes/tree/master/nodejs This is the project i'm working on. You can just change the test.yml from /nodejs/config/test.yml here.

johannesschobel commented 4 years ago

did you solve the issue?