Meteor-Community-Packages / meteor-mocha

A Mocha test driver package for Meteor 1.3+. This package reports server AND client test results in the server console and can be used for running tests on a CI server or locally.
https://packosphere.com/meteortesting/mocha
MIT License
67 stars 40 forks source link

Cannot set custom reporter. #90

Open mfranzs opened 5 years ago

mfranzs commented 5 years ago

I'm trying to set my client reporter to mochawesome. To do so, I've modified my run script to:

TEST_WATCH=1 CLIENT_TEST_REPORTER="mochawesome" meteor test --driver-package meteortesting:mocha --port 3002.

Server tests run successfully, but opening client tests in the browser fails with Uncaught Error: invalid reporter "mochawesome".

Both packages are saved as project dependencies, with versions: "mocha": "^6.1.4", "mochawesome": "^4.0.1",

Does anyone know what might be causing this problem? Thanks!

SimonSimCity commented 5 years ago

I've now taken some time to try to get it running, but this one looks very tough!

Create a file called main.test.js somewhere in your projects client directory, so it's only loaded on the client and add the following code:

import Mochawesome from 'mochawesome';
if (Meteor.settings.public.mochaRuntimeArgs.mochaOptions.clientReporter === 'mochawesome') {
  Meteor.settings.public.mochaRuntimeArgs.mochaOptions.clientReporter = Mochawesome;
}

Mocha allows you to either define a reporter as a string or as a function. If it is a string, it tries to include the package. Because of the way Meteor works, you can't cross-reference npm packages between meteor packages - and mocha is being used as such to integrate with Fibers. These lines of code import the package from a scope where it's reachable and set the imported function it as reporter.

It basically overwrite the package-setting of the meteortesting:mocha package - but it will do the trick.

But! This just get's you half-wise to the goal. The reporter mochawesome uses the package fs-extra, which in turn is based on fs, which is not available on the client-side 😜

So, as of now, you can't use this for tests executed in the browser.

The server variant is a bit trickier as of now:

import Mochawesome from 'mochawesome';
import { mochaInstance } from 'meteor/meteortesting:mocha-core';
mochaInstance.reporter(Mochawesome);
mochaInstance.reporter = () => mochaInstance;

The reason is that we currently do not allow for anything like this, so you have to change the reporter and ensure it's not overwritten later (by taking away the function by which this package would set the reporter).

Due to the way this reporter works, this will not help you much if you want to include the client-tests.

But the fact, that this isn't working on the client-side should not stop us from implementing a better way to integrate custom reporters, that's for sure.