typicode / lowdb

Simple and fast JSON database

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

jest 27.5.1

Bidek56 opened this issue · comments

commented

lowdb 1.0 worked fine with jest, but 3.0 does not seem to work with jest 27.5.1. Does anyone know how to fix it? Thx

My sample 3.0 index.test.ts code:

import { Low, JSONFile } from 'lowdb'
it('test file upload', async () => { 
  const adapter = new JSONFile("db.json")  
  const db = new Low(adapter)  
  console.log("Test:", db)  
})

using jest 27.5.1, I get the following error:

({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export * from './adapters/JSONFile.js';

I have tried the following babel.config.js:

module.exports = {
    presets: [
      ['@babel/preset-env', {targets: {node: 'current'}}],
      '@babel/preset-typescript',
    ],
  };

Struggling with the same.

Especially lowdb 3.0.0 (which is purely esm) in combination with jest.
I have tried running it with yarn node --experimental-vm-modules $(yarn bin jest) on node 16.x

commented

I have switched to Playwright and it works fine.

low.spec.js

import { test, expect } from '@playwright/test';
import { Low, JSONFile } from 'lowdb'

test('lowdb test', async () => {
  const adapter = new JSONFile("./db.json");
  const db = new Low(adapter);

  await db.read()
  expect(db.data.users).toBeTruthy();
});

Thanks @Bidek56 , I'm gonna give that a try!

Playwright is neither working.

commented

This simple example using Node 19.3 and this package.json works fine for me:

{
  "name": "lowdb-test",
  "version": "1.0.0",
  "description": "Test of lowdb and jest",
  "type": "module",
  "scripts": {
    "test": "playwright test",
    "build": "rm -rf dist/ && tsc"
  },
  "author": "Darek",
  "license": "ISC",
  "devDependencies": {
    "@playwright/test": "^1.29.1",
    "@types/lowdb": "^1.0.11",
    "playwright": "^1.29.1",
    "typescript": "^4.9.4"
  },
  "dependencies": {
    "lowdb": "5.0.5"
  }
}

low.spec.js

import { test, expect } from '@playwright/test';
import { Low } from 'lowdb'
import { JSONFile } from 'lowdb/node'

test('lowdb test', async () => {
  const adapter = new JSONFile("./db.json");
  const db = new Low(adapter);

  await db.read()
  expect(db.data.users).toBeTruthy();
});