How to use a different test framework with Adonis?


#1

I tried configuring it with Jest, but I’m receiving errors, such as the use variable is undefined.


#2

Any specific reason for using Jest?

Also if you plan to use a custom testing engine, you will have to boot the application yourself and then execute your tests


#3

Is there an example or plugin of how this was done before tests were introduced in 4.0?

I have my client and api as seperate modules in the same repo, and I want to use one test runner to run all tests.

(I’d keep Adonis Vow in the root repo to run test but the test runner doesn’t seem as configurable since it was made specifically for Adonis.)


#4

Yes, the test runner is written specifically for Adonis. Since it supports features like Traits, it is easier to build the blocks instead of bundling them to the core.

For example: there is a trait to authenticate requests, set proper encrypted sessions, a trait to run database queries in a transaction for tests.

If you choose to remove the inbuilt testing layer, then yes you have to implement all this by hand ( which is 100% possible ).

So it is a tradeoff you have to decide between.

  1. Similar testing frameworks
  2. Pre existing implementations to make testing easier.

Also since, a huge number of teams have different frontend and backend engineers, there is hardly any context switch.

Using jest.

Now as far as using jest is considered.

  1. Creating an ace command to run your tests.
  2. As soon as ace will execute your command, it will boot the app, so you won’t have to it by hand.
  3. Within the command, tell jest about your test files.

#5

Yeah, I decided to go with the two separate testing framework route for now.

Thanks!


#6

Hi there @alidcastano

I’m trying to use jest too: my tests files are under the tests directory. I can’t make this work :
:confused:

I keep getting:

args => { name: ‘jest’ }
Dummy implementation for test command
module.js:538
throw err;
^

Error: Cannot find module '/Users/juan/Test/adonis4/node_modules/jest/bin/jest.js tests
at Function.Module._resolveFilename (module.js:536:15)
at Function.Module._load (module.js:466:25)
at Function.Module.runMain (module.js:676:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
/Users/juan/Test/adonis4/app/Commands/Test.js:48
if (err) throw err;
^

Error: exit code 1
at ChildProcess. (/Users/juan/Test/adonis4/app/Commands/Test.js:28:39)
at emitTwo (events.js:126:13)
at ChildProcess.emit (events.js:214:7)
at Process.ChildProcess._handle.onexit (internal/child_process.js:198:12)

this is my code for the Ace Command Test.

‘use strict’

const { Command } = require(’@adonisjs/ace’)

var childProcess = require(‘child_process’);

function runScript(scriptPath, callback) {

// keep track of whether callback has been invoked to prevent multiple invocations
var invoked = false;

var process = childProcess.fork(scriptPath);

// listen for errors as they may prevent the exit event from firing
process.on('error', function (err) {
    if (invoked) return;
    invoked = true;
    callback(err);
});

// execute the callback once the process has finished running
process.on('exit', function (code) {
    if (invoked) return;
    invoked = true;
    var err = code === 0 ? null : new Error('exit code ' + code);
    callback(err);
});

}

class Test extends Command {
static get signature () {
return ‘test { name?=jest }’
}

static get description () {
return ‘run tests’
}

async handle (args, options) {
console.log("args => ", args);
this.info(‘Dummy implementation for test command’)

runScript('./node_modules/jest/bin/jest.js __tests__', function (err) {
if (err) throw err;
  console.log('finished running some-script.js');
});

}
}

module.exports = Test


#7

I ended up not using Jest for the reasons mentioned above