typeorm: 0.2.24 JEST tests suddenly throwing RepositoryNotFound, no changes to code

Issue type:

[ ] question [x] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[x] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

Not sure how exactly this started, I ran an dependency update to upgrade to TypeORM 0.2.24, and then suddenly my tests started failing, frustratingly just rolling back to .22 the error still happens, I believe some other dependency was updated with my yarn.lock, in general this issue has been problematic during development, but these tests had been working for 3 months without issue.

tsconfig.json

{
  "compilerOptions": {
    "lib": ["es5", "es6", "es2016", "es2017", "dom"],
    "target": "ES2016",
    "module": "commonjs",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "outDir": "./build",
    "noImplicitAny": true,
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "baseUrl": ".",
    "paths": {
      "*": ["node_modules/"],
      "typings/*": ["src/typings/*"]
    }
  },
  "include": ["src/**/*.ts"],
  "exclude": ["src/test/**/*.spec.ts", "node_modules"]
}

jest.config.js

module.exports = {
  preset: "ts-jest",
  testEnvironment: "node",
  collectCoverage: true,
  collectCoverageFrom: [
    "./src/**/*.{ts,tsx,js,jsx}",
    "!**/node_modules/**",
    "!**/build/**",
    "!./src/generated/**",
    "!./src/migration/**"
  ],
  testPathIgnorePatterns: [
    "/node_modules/",
    "/build/",
    "/__utils",
    "/__testData",
    "/__queries"
  ],
  globalSetup: "./src/jest.globalSetup.ts"
};

schema config:

export const schemaConfig = {
  entities: ["build/entity/**/*.{js,ts}"],
  migrations: ["build/migration/**/*.{js,ts}"],
  subscribers: ["build/subscriber/**/*.{js,ts}"],
  cli: {
    entitiesDir: "src/entity",
    migrationsDir: "src/migration",
    subscribersDir: "src/subscriber"
  }
};
 RepositoryNotFoundError: No repository for "Card" was found. Looks like this entity is not registered in current "testConnection" connection?

You can see the full repo for these tests here: https://github.com/seandearnaley/brainstrike-typescript-starter/tree/master/server

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 4
  • Comments: 20 (7 by maintainers)

Most upvoted comments

Oh, I checked the PR and added some comment. I hope the PR to be accepted

I have solution @seandearnaley we can solve this problem with replacing code like this

Before

protected findMetadata(target: Function|EntitySchema<any>|string): EntityMetadata|undefined {
        return this.entityMetadatas.find(metadata => {
            if (metadata.target === target)
                return true;
            if (target instanceof EntitySchema) {
                return metadata.name === target.options.name;
            }
            if (typeof target === "string") {
                if (target.indexOf(".") !== -1) {
                    return metadata.tablePath === target;
                } else {
                    return metadata.name === target || metadata.tableName === target;
                }
            }

            return false;
        });
    }

After

protected findMetadata(target: Function|EntitySchema<any>|string): EntityMetadata|undefined {
        return this.entityMetadatas.find(metadata => {
            if (Object.getPrototypeOf(metadata.target) === Object.getPrototypeOf(target)&&metadata.target.name===target.name)
                // First Check if target is made from same entity constructor as metadata target have. Then check if target name is same as metadata target name
                return true;
            if (target instanceof EntitySchema) {// I don't know if this condition is necessary
                return metadata.name === target.options.name;
            }
            if (typeof target === "string") {// I don't know if this condition is necessary
                if (target.indexOf(".") !== -1) {
                    return metadata.tablePath === target;
                } else {
                    return metadata.name === target || metadata.tableName === target;
                }
            }

            return false;
        });
    }

Object.getPrototypeOf(metadata.target) === Object.getPrototypeOf(target) will satisfy the problem requirement

  1. check if metadata.target is same as target
  2. check if target is not only same named function but also created by entity constructor (resolve false positivity)

please add new PR from this patch. it will work well

Yea - I’m looking at both. Because jest is involved here I have a feeling it could be similar but slightly different. 😃

thanks for working on that @YoonSeongKyeong