首页 > 解决方案 > Jasmine 测试 TypeORM - 错误:DriverPackageNotInstalledError: SQLite package has not been found installed

问题描述

我正在尝试对使用 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

标签: angularsqliteunit-testingjasminetypeorm

解决方案


安装sqlite3

如果 typeorm 选项指定为database:':memory'sqlite则使用库在正在运行的实例中创建和使用sqlite3

如果您想使用 external sqlite3,请查看此页面。

https://github.com/mapbox/node-sqlite3#source-install


推荐阅读