Coder Social home page Coder Social logo

Comments (3)

thetutlage avatar thetutlage commented on July 23, 2024

When writing tests for Adonis, you always have to setup the application state before executing any tests. One option is to create an ace command and use it to run the tests. Example

'use strict'

const Command = use('Command')
const Helpers = use('Helpers')
const path = use('path')
const pify = use('pify')
const glob = pify(use('glob'))
const runner = new (use('japa/src/Runner'))()
const Ioc = use('adonis-fold').Ioc

class RunTest extends Command {
  constructor () {
    super()
    this.baseDir = Helpers.basePath()
    const test = runner.test.bind(runner)
    test.group = runner.group.bind(runner)
    Ioc.bind('test', () => {
      return test
    })
  }

  get signature () {
    return 'test:run {--unit} {--functional} {--files=@value}'
  }

  get description () {
    return 'Run the tests suite'
  }

  * _runTests (globPattern) {
    const files = yield glob(globPattern)
    files.forEach((file) => {
      require(file)
    })
  }

  * handle (args, options) {
    let files = '/test/**/*.spec.js'

    // Going over user arguments
    if (options.unit) {
      files = 'test/unit/*.spec.js'
    } else if (options.functional) {
      files = 'test/functional/*.spec.js'
    } else if (options.files) {
      files = 'test/functional/*.spec.js'
    }

    try {
      yield this._runTests(path.join(this.baseDir, files))
      yield runner.run()
    } catch (e) {
      // console.log(e)
    }
  }
}

module.exports = RunTest

And then you can run tests by saying ./ace test:run

Also you have to make use of test by importing it from the IoC container over requiring japa. So the test file will be like this

const test = use('test')

test('2 +2', (assert) => {
  assert.equal(2 + 2, 4)
})

from core.

lmj0011 avatar lmj0011 commented on July 23, 2024

this points me into the right direction, thanks

from core.

thetutlage avatar thetutlage commented on July 23, 2024

Also one should use https://github.com/adonisjs/adonis-vow now

from core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.