Skip to content

Saving database works in browser, fails in UnitTest #56

@Stwissel

Description

@Stwissel

This is probably less a bug and more a testimony of my lack of understanding. First of all: LOVE your work!

I'm trying to understand kdbxweb, so I setup a simple project that creates, saves and loads a database. Works like a charm in the browser (run npm run dev to see). However I failed to create a unit test using vitest. Creation works, but save fails:

TypeError: Failed to execute 'digest' on 'SubtleCrypto': 2nd argument is not instance of ArrayBuffer, Buffer, TypedArray, or DataView.

The test method looks like this:

import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { init, createDatabase, saveDatabase, loadDatabase, clearDatabase } from '../src/vault';
import * as kdbxweb from 'kdbxweb';

describe('Vault Kxdb Tests', () => {
  beforeEach(() => {
    init();
  });

  afterEach(() => {
    clearDatabase();
  });

  it('should create a new database', async () => {
    const db = await createDatabase('test');
    expect(db).toBeDefined();
  });

  it('should save the database', async () => {
    const db = await createDatabase('test');
    const arrayBuffer = await saveDatabase(db);
    expect(arrayBuffer).toBeDefined();
  });

  it('should load the database', async () => {
    const db = await createDatabase('test');
    const arrayBuffer = await saveDatabase(db);
    const loadedDb = await loadDatabase(arrayBuffer, 'test');
    expect(loadedDb).toBeDefined();
  });
});

full project here: https://github.com/Stwissel/kxdbtest

What do I miss?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions