mercredi 1 août 2018

Sqlite3 Table doesn't exist, React

I'm trying to write a test for my DB functions. The first test is working but I get the errors written below for the "AddExercise" test. Please have a look at my code below.

This is the seed file:

exports.seed = function(knex, Promise) {
// Deletes ALL existing entries
return knex('exercises').del()
.then(function () {
  // Inserts seed entries
  return knex('exercises').insert([
    {id: 1, exe_name: 'rowing', exe_info:'For back and chest', 
  comment:'Good', 
  exe_url:'https://www.freetrainers.com/exercise/calendar/'}        
  ]);});};

These are the DB functions:

 const db = require('./connection')

 const getExercises = (testDb) => {
 return (testDb || db)('exercises')
.select()}

 const getExerciseById = (id, testDb) => {
 return (testDb || db)('exercises')
.select()
.where('exercises.id', id)}

const addExercise = (exercise, testDb) => {
return (testDb || db)('exercises').insert(exercise)
.then(exercise_id => getExerciseById(exercise_id[0], db))}

This the test file:

const request = require('supertest')
const env = require('./test-environment')
const exercises = require('../../server/db/exercises')

let testDb = null
beforeEach(() => {
testDb = env.getTestDb()
return env.initialise(testDb) })
afterEach(() => env.cleanup(testDb))

//Tests

test('getAllexercises function gets all exercises', () => {
const expected = 1
return exercises.getExercises(testDb)
.then(exercises => {
  const actual = exercises.length
  expect(actual).toBe(expected)
})})

//AddExercise

test('addExercise functions adds an exercise', () => {
const fakeExercise = {
exe_name: 'Test',
exe_info: 'Test title',
comment: 'dsfasdfdf',
exe_url: 'fake/blah'    
}
const expected = {
...fakeExercise,
id:2}
return exercises.addExercise(fakeExercise, testDb)
.then(actual => {
  expect(actual).toBe(expected)
  return testDb('exercises')
})
.then(exercises => {
  expect(exercises.length).toBe(expected)
})})

When I run yarn test db.test.js I get the following:

Error: SQLITE_ERROR: no such table: exercises
Ran all test suites matching /db.test.js/i.Jest did not exit one 
second after the test run has completed.

This usually means that there are asynchronous operations that weren't 
stopped in your tests. 
Consider running Jest with `--detectOpenHandles` to troubleshoot this 
issue.
error An unexpected error occurred: "Command failed.
Exit code: 1
Command: sh
Arguments: -c jest db.test.js
Directory: /home/eda/new/Gym-app
Output:
".
info If you think this is a bug, please open a bug report with the 
information provided in "/home/hameetsingh/eda/new/Gym-app/yarn- 
error.log"

My package.json and knexfile configuration seems to be fine as another project has similar settings and the tests seem to work fine for it. Please advise. Thanks

Aucun commentaire:

Enregistrer un commentaire