freshollie / jest-dynalite

Jest preset to run Dynalite (DynamoDB local) per test runner
https://www.npmjs.com/package/jest-dynalite
MIT License
150 stars 16 forks source link

ReadError: Database is not open / existing setup after env #67

Open mattensor opened 3 years ago

mattensor commented 3 years ago

Hey there

I get the following error when running dynalite.

ReadError: Database is not open
    at /api/node_modules/levelup/lib/levelup.js:190:15
    at /api/node_modules/encoding-down/index.js:75:21
    at processTicksAndRejections (internal/process/task_queues.js:81:21)

I have an existing setupFilesAfterEnv file in which there are conflicts. When I remove it, my tests work - but I have no dynalite goodness :(

afterEnvSetup

import { server } from '~mocks/server'

beforeAll(() => {
  server.listen({
    onUnhandledRequest: 'warn'
  })
})

afterAll(() => {
  server.close()
})

afterEach(() => {
  server.resetHandlers()
})

I've tried the granular approach importing the start / stop fns individually. The out of the box solution and on an individual file. Still no luck

Would be great to get some help

jonoirwinrsa commented 3 years ago

I had this issue too. For me it was because I was returning undefined as a TableName in my jest-dynalite-config.js file. Perhaps it's the same for you?

freshollie commented 3 years ago

@jonoirwinrsa might be right here. If you remove your other afterEnvSetup file, does this work? Can I see your dynalite config?

mattensor commented 3 years ago

It works if I remove my other afterEnvSetup setup file, but I need that to mock external api calls.

Here's my jest-dynalite-config.ts file

const tables = [
  {
    TableName: 'conversations',
    AttributeDefinitions: [
      { AttributeName: 'id', AttributeType: 'S' },
      { AttributeName: 'userId', AttributeType: 'S' }
    ],
    KeySchema: [{ AttributeName: 'id', KeyType: 'HASH' }],
    BillingMode: 'PAY_PER_REQUEST',
    GlobalSecondaryIndexes: [
      {
        IndexName: 'userConversationsIndex',
        KeySchema: [{ AttributeName: 'userId', KeyType: 'HASH' }],
        Projection: { ProjectionType: 'ALL' }
      }
    ]
  }
]

module.exports = {
  tables,
  basePort: 8000
}

jest.config.ts

import { pathsToModuleNameMapper } from 'ts-jest/utils'
import { compilerOptions } from './tsconfig.json'

export default {
  moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {
    prefix: '<rootDir>/'
  }),
  preset: 'jest-dynalite',
  setupFiles: ['<rootDir>/jestEnvironmentSetup.ts'],
  setupFilesAfterEnv: ['<rootDir>/jestServiceWorkerSetup.ts']
}
freshollie commented 3 years ago

Ok. Can you narrow down which test is causing this error and maybe post details about this test? It's likely you get an error like this if a DB call happens outside a test (as this library tears down the server after each test).

This could be due to a forgotten await keyword, or a side effect which is kicked off by another function and not being awaited.

yamatatsu commented 1 year ago

I've created a minimal representation code.

https://github.com/yamatatsu/represent-jest-dynalite-database-is-not-open

I guess the condition of this error is below:

In my case, the work arround I found is to extend the timeout period.

test('test case' async () => {...}, 60_000 /* long enough */)