Jasmine测试类型ORM-错误:DriverPackageNotInstalledError:未找到已安装的SQLite包

tzdcorbm  于 2023-05-29  发布在  SQLite
关注(0)|答案(2)|浏览(160)

我正在尝试对使用TypeORM连接更新sqlite数据库的服务进行单元测试。
看起来没有固定的方法来单元测试TypeORM,所以我试图找到任何可行的方法。根据以下内容,我决定尝试使用内存中的数据库进行测试***:
https://github.com/typeorm/typeorm/issues/1267#issuecomment-483775861
这里的代码使用了Jest,所以我尝试转换到Jasmine,这是我的测试环境。
作为原理证明,如果我在 database-test.spec.ts 中执行以下操作

import { createConnection, Entity, getRepository } from "typeorm";
import { PrimaryGeneratedColumn, Column } from "typeorm";
import { fakeAsync, async } from "@angular/core/testing";

@Entity()
export class MyEntity {
  @PrimaryGeneratedColumn()
  id?: number;

  @Column()
  name?: string;
}

describe("Database test", () => {
  it("store Joe and fetch it", fakeAsync(() => {
    createConnection({
      name: "testing",
      type: "sqlite",
      database: ":memory:",
      dropSchema: true,
      entities: [MyEntity],
      synchronize: true,
      logging: false,
    }).then(conn => {
      conn.getRepository(MyEntity).insert({
        name: "Joe",
      });
      const joe = getRepository(MyEntity).find({
        where: {
          id: 1,
        },
      });

      expect(joe[0].name).toBe("Joe");
      conn.close();
    });
  }));
});

无论我做什么我得到错误

Failed: Uncaught (in promise): DriverPackageNotInstalledError: SQLite package has not been found installed. Try to install it: npm install sqlite3 --save
DriverPackageNotInstalledError: SQLite package has not been found installed. Try to install it: npm install sqlite3 --save
    at new DriverPackageNotInstalledError (http://localhost:9876/_karma_webpack_/webpack:/node_modules/typeorm/browser/error/DriverPackageNotInstalledError.js:8:1)
    at SqliteDriver.loadDependencies (http://localhost:9876/_karma_webpack_/webpack:/node_modules/typeorm/browser/driver/sqlite/SqliteDriver.js:118:1)
    at new SqliteDriver (http://localhost:9876/_karma_webpack_/webpack:/node_modules/typeorm/browser/driver/sqlite/SqliteDriver.js:24:1)
    at DriverFactory.create (http://localhost:9876/_karma_webpack_/webpack:/node_modules/typeorm/browser/driver/DriverFactory.js:35:1)
    at new Connection (http://localhost:9876/_karma_webpack_/webpack:/node_modules/typeorm/browser/connection/Connection.js:50:40)
    at ConnectionManager.create (http://localhost:9876/_karma_webpack_/webpack:/node_modules/typeorm/browser/connection/ConnectionManager.js:54:1)
    at Module.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/node_modules/typeorm/browser/index.js:171:1)
    at step (http://localhost:9876/_karma_webpack_/webpack:/node_modules/tslib/tslib.es6.js:99:1)
    at Object.next (http://localhost:9876/_karma_webpack_/webpack:/node_modules/tslib/tslib.es6.js:80:45)
    at http://localhost:9876/_karma_webpack_/webpack:/node_modules/tslib/tslib.es6.js:73:1

SQLite已安装并在测试之外运行。为什么在测试过程中会出现此错误?
其他信息:我正在使用初学者工具包:https://github.com/CubikNeRubik/angular-electron-typeorm-starter

50few1ms

50few1ms1#

安装sqlite3
如果typeorm选项被指定为database:':memory',则会创建sqlite,并使用sqlite3库在运行示例中使用。
如果您想使用外部sqlite3,请检查此页面。
https://github.com/mapbox/node-sqlite3#source-install

nhjlsmyf

nhjlsmyf2#

我在Postgres上遇到了同样的问题。我们正在使用git workspaces,看起来typeorm在寻找驱动程序,但是在错误的位置(pg安装在根node_modules git文件夹中,而不是workspaceA/node_modules中)。
解决方法是在连接期间提供驱动程序:

import { DataSource } from "typeorm";
import { models } from "../models";
import { TextEncoder } from "util";
global.TextEncoder = TextEncoder;

export default class AppDataSource {
  static build(): DataSource {
    return new DataSource({
      type: "postgres",
      url:
        process.env.POSTGRES_URL ||
        `postgres://${process.env.POSTGRES_USER || "user"}:${
          process.env.POSTGRES_PASSWORD || "password"
        }@${process.env.PG_HOST || "localhost"}:${
          process.env.POSTGRES_PORT || 5254
        }/${process.env.POSTGRES_DB || "db"}`,
      schema: process.env.PG_SCHEMA,
      entities: models,
      installExtensions: false,
      migrationsTableName: "migrations",
      synchronize: process.env.NODE_ENV === "development",
      // ADD THIS LINE
      driver: require("pg")
    });
  }
}

相关问题