Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: How to do transactions of Lucid during testing #23

Closed
gauravgango opened this issue Oct 5, 2020 · 1 comment
Closed

Question: How to do transactions of Lucid during testing #23

gauravgango opened this issue Oct 5, 2020 · 1 comment
Assignees
Labels
Type: Question Needs clarification

Comments

@gauravgango
Copy link

Prerequisites

How do one does transactional testing of Lucid models in test cases.

Description

As japa is capable of running multiple testcases in parallel, it would be very dangerous that the data set of testing is polluting the data set of another test case. And parallelization also means that the migrations can't be done for each test group.

I tried the following but failed

LoginController.ts

import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'

export default class LoginController {
  /**
   * Login action
   * @param request
   */
  public async login ({request, auth}: HttpContextContract) {
    const {email, password} = request.all()
    return await auth.use('api').attempt(email, password)
  }
}

Japa test file

import test from 'japa'
import supertest from 'supertest'
import Route from '@ioc:Adonis/Core/Route'
import Admin from 'App/Models/Admin'
import fakerStatic from 'faker'
import Database, { TransactionClientContract } from '@ioc:Adonis/Lucid/Database'

const BASE_URL = `http://${process.env.HOST}:${process.env.PORT}`

test.group('Login Controller', (group) => {
  let transactionCtx: TransactionClientContract

  group.beforeEach(async () => {
    transactionCtx = await Database.transaction()
  })

  group.afterEach(async () => {
    await transactionCtx.rollback()
  })

  test('ensure login works with valid credentials', async (assert) => {
    const adminModel = new Admin()
    adminModel.useTransaction(transactionCtx)
    const admin = adminModel.fill({
      email: fakerStatic.internet.email(),
      name: fakerStatic.name.firstName(),
      password: 'password',
    })
    await admin.save()

    const result = await supertest(BASE_URL)
      .post(Route.makeUrl('api.v1.admin.login') as string)
      .set('Content-Type', 'application/json')
      .set('Accept', 'application/json')
      .send({email: admin.email, password: 'password'})
      .expect(200)
    assert.containsAllKeys(result.body, ['token', 'type'])
  })
})

Package version

{
  "name": "nodejs-backend",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "build": "node ace build",
    "start": "node ace serve --watch --debug",
    "lint": "eslint . --ext=.ts"
  },
  "devDependencies": {
    "@adonisjs/assembler": "^2.0.0",
    "@types/jsdom": "^16.2.4",
    "@types/supertest": "^2.0.10",
    "adonis-preset-ts": "^1.0.4",
    "eslint": "^7.10.0",
    "eslint-plugin-adonis": "^1.0.15",
    "execa": "^4.0.3",
    "get-port": "^5.1.1",
    "japa": "^3.1.1",
    "jsdom": "^16.4.0",
    "pino-pretty": "^4.2.1",
    "supertest": "^5.0.0",
    "ts-node": "^9.0.0",
    "typescript": "^4.0.3",
    "youch": "^2.1.0",
    "youch-terminal": "^1.0.1"
  },
  "dependencies": {
    "@adonisjs/ace": "^6.0.0",
    "@adonisjs/auth": "^4.3.1",
    "@adonisjs/core": "^5.0.0-preview-rc",
    "@adonisjs/fold": "^6.0.0",
    "@adonisjs/lucid": "^8.4.4",
    "@gauravgango/adonisjs-stomp-driver": "1.0.0-beta.20",
    "luxon": "^1.25.0",
    "mysql": "^2.18.1",
    "phc-argon2": "^1.0.10",
    "phc-bcrypt": "^1.0.3",
    "proxy-addr": "^2.0.6",
    "reflect-metadata": "^0.1.13",
    "source-map-support": "^0.5.19"
  }
}

Error Message & Stack Trace

Login Controller

  ✖ ensure login works with valid credentials
    Error: expected 200 "OK", got 400 "Bad Request"

Relevant Information

It appears the request happening through japa is not on same node request where the main test case is running hece the request is not in same transactional reference

@thetutlage
Copy link
Collaborator

There is a section on this in AdonisJS cookbooks related to global transactions. Please check https://docs.adonisjs.com/cookbooks/testing-adonisjs-apps#using-global-database-transactions

@thetutlage thetutlage self-assigned this Jan 7, 2022
@thetutlage thetutlage added the Type: Question Needs clarification label Jan 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Question Needs clarification
Projects
None yet
Development

No branches or pull requests

2 participants