vendredi 17 janvier 2020

Jest returns a table not found error with sqlite

I've written a set of tests and am using Sequelize as my ORM and Jest as my test runner.

My Sequelize config looks like this:

{
  "development": {
    "dialect": "sqlite",
    "storage": "dev.sqlite"
  },
  "test": {
    "dialect": "sqlite",
    "storage": ":memory:"
  },
}

My package.json looks like this:

    "test": "jest --setupFiles dotenv/config",
    "pretest": "yarn migrate && yarn seed",
    "migrate": "cd server && npx sequelize-cli db:migrate",
    "migrate:reset": "cd server && npx sequelize-cli db:migrate:undo:all --env test && npm run migrate --env test",
    "seed": "cd server && npx sequelize-cli db:seed:all --env test",
    "test:debug": "node --inspect node_modules/.bin/jest",

My jest setup files simply ensure that the Node environment is test.

Everything works as expected and the migrations run with no problems, as do the seed files. But when the tests start I get the following:

SequelizeDatabaseError: SQLITE_ERROR: no such table: Users. This is the same for every table used.

I suspect the problem is in the fact that when a connection closes, the in-memory database is terminated so I tried running the database migrations and seed on a per-test basis but this didn't work. Likewise for using an actual test.sqlite file.

Any advice?

Aucun commentaire:

Enregistrer un commentaire