For intergration tests, always remember when you create a 'mass' you should aslo clean up the 'mass'.

 

For example when you start the server, you need to close the server after the tests. See the 

 

Also for authentication, when you create a new user for testing, you should also clean it up.

There is a help function for tests, to create a new user:

async function createNewUser(overrides) {
  const password = faker.internet.password()
  const userData = generateUserData(overrides)
  const {email, username} = userData
  const user = await api
    .post('users', {user: {email, password, username}})
    .then(getUser)
  return {
    user,
    cleanup() {
      return api.delete(`users/${user.username}`)
    },
  }
}

In the return value, it also provide a function to clean up the user.

 

const api = axios.create({
  baseURL: 'http://localhost:3000/api',
})

describe('authenticated', () => {
  let cleanupUser
  beforeAll(async () => {
    const results = await createNewUser()
    cleanupUser = results.cleanup
    api.defaults.headers.common.authorization = `Token ${results.user.token}` // set default http header, add authorization for JWT token
  })

  afterAll(async () => {
    await cleanupUser()
    api.defaults.headers.common.authorization = ''
  })

})