Skip to content

Commit

Permalink
Merge pull request #147 from Grubba27/master
Browse files Browse the repository at this point in the history
[METEOR 3]: Removing Fibers from Mocha
  • Loading branch information
jankapunkt authored Jan 3, 2024
2 parents 2d61478 + 40cef47 commit 4beee13
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 66 deletions.
27 changes: 14 additions & 13 deletions package/.versions
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
babel-compiler@7.10.2
babel-compiler@7.11.0-alpha300.11
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
http@1.4.2
http@1.0.1
[email protected]
meteor@1.11.0
meteortesting:browser-tests@1.4.2
meteortesting:mocha@2.1.0
meteortesting:mocha-core@8.0.1
meteor@2.0.0-alpha300.11
meteortesting:browser-tests@1.5.3-alpha300.11
meteortesting:mocha@3.0.3-alpha300.11
meteortesting:mocha-core@8.2.2-alpha300.11
[email protected]
[email protected].0
[email protected].1-alpha300.11
[email protected]
promise@0.12.2
[email protected].5
url@1.3.2
promise@1.0.0-alpha300.11
[email protected].7
underscore@1.0.14-alpha300.11
8 changes: 4 additions & 4 deletions package/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ Package.describe({
summary: 'Run Meteor package or app tests with Mocha',
git: 'https://github.com/meteortesting/meteor-mocha.git',
documentation: '../README.md',
version: '2.1.0',
version: '3.0.3-alpha300.11',
testOnly: true,
});

Package.onUse(function onUse(api) {
api.use([
'meteortesting:mocha-core@8.0.0',
'ecmascript@0.15.1',
'meteortesting:mocha-core@8.2.2-alpha300.11',
'ecmascript@0.16.8-alpha300.11',
]);

api.use(['meteortesting:browser-tests@1.4.2', '[email protected] || 2.0.0'], 'server');
api.use(['meteortesting:browser-tests@1.5.3-alpha300.11', '[email protected] || 2.0.0'], 'server');
api.use('[email protected]', 'server', { weak: true });
api.use('lmieulet:[email protected] || 2.0.1 || 3.0.0 || 4.1.0', 'client', { weak: true });

Expand Down
133 changes: 84 additions & 49 deletions package/server.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
/* global Package */
import { mochaInstance } from 'meteor/meteortesting:mocha-core';
import { startBrowser } from 'meteor/meteortesting:browser-tests';
import { onMessage } from 'meteor/inter-process-messaging';
import { mochaInstance } from "meteor/meteortesting:mocha-core";
import { startBrowser } from "meteor/meteortesting:browser-tests";
import { onMessage } from "meteor/inter-process-messaging";

import fs from 'fs';
import fs from "fs";

import setArgs from './runtimeArgs';
import handleCoverage from './server.handleCoverage';
import setArgs from "./runtimeArgs";
import handleCoverage from "./server.handleCoverage";

if (Package['browser-policy-common'] && Package['browser-policy-content']) {
const { BrowserPolicy } = Package['browser-policy-common'];
var { mochaOptions, runnerOptions, coverageOptions } = setArgs();
var { grep, invert, reporter, serverReporter, serverOutput, clientOutput } =
mochaOptions || {};

if (Package["browser-policy-common"] && Package["browser-policy-content"]) {
const { BrowserPolicy } = Package["browser-policy-common"];

// Allow the remote mocha.css file to be inserted, in case any CSP stuff
// exists for the domain.
BrowserPolicy.content.allowInlineStyles();
BrowserPolicy.content.allowStyleOrigin('https://cdn.rawgit.com');
BrowserPolicy.content.allowStyleOrigin("https://cdn.rawgit.com");
}

const { mochaOptions, runnerOptions, coverageOptions } = setArgs();
const { grep, invert, reporter, serverReporter, serverOutput, clientOutput } = mochaOptions || {};

// Since intermingling client and server log lines would be confusing,
// the idea here is to buffer all client logs until server tests have
// finished running and then dump the buffer to the screen and continue
Expand All @@ -31,20 +32,22 @@ const clientLines = [];
function clientLogBuffer(line) {
if (serverTestsDone) {
// printing and removing the extra new-line character. The first was added by the client log, the second here.
console.log(line.replace(/\n$/, ''));
console.log(line.replace(/\n$/, ""));
} else {
clientLines.push(line);
}
}

function printHeader(type) {
const lines = [
'\n--------------------------------',
Meteor.isAppTest ? `--- RUNNING APP ${type} TESTS ---` : `----- RUNNING ${type} TESTS -----`,
'--------------------------------\n',
"\n--------------------------------",
Meteor.isAppTest
? `--- RUNNING APP ${type} TESTS ---`
: `----- RUNNING ${type} TESTS -----`,
"--------------------------------\n",
];
lines.forEach((line) => {
if (type === 'CLIENT') {
if (type === "CLIENT") {
clientLogBuffer(line);
} else {
console.log(line);
Expand All @@ -57,25 +60,33 @@ let clientFailures = 0;
let serverFailures = 0;
function exitIfDone(type, failures) {
callCount++;
if (type === 'client') {
if (type === "client") {
clientFailures = failures;
} else {
serverFailures = failures;
serverTestsDone = true;
clientLines.forEach((line) => {
// printing and removing the extra new-line character. The first was added by the client log, the second here.
console.log(line.replace(/\n$/, ''));
console.log(line.replace(/\n$/, ""));
});
}

if (callCount === 2) {
// We only need to show this final summary if we ran both kinds of tests in the same console
if (runnerOptions.runServer && runnerOptions.runClient && runnerOptions.browserDriver) {
console.log('All tests finished!\n');
console.log('--------------------------------');
console.log(`${Meteor.isAppTest ? 'APP ' : ''}SERVER FAILURES: ${serverFailures}`);
console.log(`${Meteor.isAppTest ? 'APP ' : ''}CLIENT FAILURES: ${clientFailures}`);
console.log('--------------------------------');
if (
runnerOptions.runServer &&
runnerOptions.runClient &&
runnerOptions.browserDriver
) {
console.log("All tests finished!\n");
console.log("--------------------------------");
console.log(
`${Meteor.isAppTest ? "APP " : ""}SERVER FAILURES: ${serverFailures}`
);
console.log(
`${Meteor.isAppTest ? "APP " : ""}CLIENT FAILURES: ${clientFailures}`
);
console.log("--------------------------------");
}

handleCoverage(coverageOptions).then(() => {
Expand All @@ -93,13 +104,13 @@ function exitIfDone(type, failures) {

function serverTests(cb) {
if (!runnerOptions.runServer) {
console.log('SKIPPING SERVER TESTS BECAUSE TEST_SERVER=0');
exitIfDone('server', 0);
console.log("SKIPPING SERVER TESTS BECAUSE TEST_SERVER=0");
exitIfDone("server", 0);
if (cb) cb();
return;
}

printHeader('SERVER');
printHeader("SERVER");

if (grep) mochaInstance.grep(grep);
if (invert) mochaInstance.invert(invert);
Expand All @@ -108,41 +119,45 @@ function serverTests(cb) {
// We need to set the reporter when the tests actually run to ensure no conflicts with
// other test driver packages that may be added to the app but are not actually being
// used on this run.
mochaInstance.reporter(serverReporter || reporter || 'spec', {
mochaInstance.reporter(serverReporter || reporter || "spec", {
output: serverOutput,
});

mochaInstance.run((failureCount) => {
if (typeof failureCount !== 'number') {
console.log('Mocha did not return a failure count for server tests as expected');
exitIfDone('server', 1);
if (typeof failureCount !== "number") {
console.log(
"Mocha did not return a failure count for server tests as expected"
);
exitIfDone("server", 1);
} else {
exitIfDone('server', failureCount);
exitIfDone("server", failureCount);
}
if (cb) cb();
});
}

function clientTests() {
if (clientTestsRunning) {
console.log('CLIENT TESTS ALREADY RUNNING');
console.log("CLIENT TESTS ALREADY RUNNING");
return;
}

if (!runnerOptions.runClient) {
console.log('SKIPPING CLIENT TESTS BECAUSE TEST_CLIENT=0');
exitIfDone('client', 0);
console.log("SKIPPING CLIENT TESTS BECAUSE TEST_CLIENT=0");
exitIfDone("client", 0);
return;
}

if (!runnerOptions.browserDriver) {
console.log('Load the app in a browser to run client tests, or set the TEST_BROWSER_DRIVER environment variable. '
+ 'See https://github.com/meteortesting/meteor-mocha/blob/master/README.md#run-app-tests');
exitIfDone('client', 0);
console.log(
"Load the app in a browser to run client tests, or set the TEST_BROWSER_DRIVER environment variable. " +
"See https://github.com/meteortesting/meteor-mocha/blob/master/README.md#run-app-tests"
);
exitIfDone("client", 0);
return;
}

printHeader('CLIENT');
printHeader("CLIENT");
clientTestsRunning = true;

startBrowser({
Expand All @@ -169,23 +184,37 @@ function clientTests() {
},
done(failureCount) {
clientTestsRunning = false;
if (typeof failureCount !== 'number') {
console.log('The browser driver package did not return a failure count for server tests as expected');
exitIfDone('client', 1);
if (typeof failureCount !== "number") {
console.log(
"The browser driver package did not return a failure count for server tests as expected"
);
exitIfDone("client", 1);
} else {
exitIfDone('client', failureCount);
exitIfDone("client", failureCount);
}
},
});
}

// Before Meteor calls the `start` function, app tests will be parsed and loaded by Mocha
function start() {
runnerOptions = setArgs().runnerOptions;
coverageOptions = setArgs().coverageOptions;
mochaOptions = setArgs().mochaOptions;
grep = mochaOptions.grep;
invert = mochaOptions.invert;
reporter = mochaOptions.reporter;
serverReporter = mochaOptions.serverReporter;
serverOutput = mochaOptions.serverOutput;
clientOutput = mochaOptions.clientOutput;

// Run in PARALLEL or SERIES
// Running in series is a better default since it avoids db and state conflicts for newbs.
// If you want parallel you will know these risks.
if (runnerOptions.runParallel) {
console.log('Warning: Running in parallel can cause side-effects from state/db sharing');
console.log(
"Warning: Running in parallel can cause side-effects from state/db sharing"
);

serverTests();
clientTests();
Expand All @@ -198,12 +227,18 @@ function start() {

export { start };

onMessage('client-refresh', (options) => {
console.log('CLIENT TESTS RESTARTING (client-refresh)', options === undefined ? '' : options);
onMessage("client-refresh", (options) => {
console.log(
"CLIENT TESTS RESTARTING (client-refresh)",
options === undefined ? "" : options
);
clientTests();
});

onMessage('webapp-reload-client', (options) => {
console.log('CLIENT TESTS RESTARTING (webapp-reload-client)', options === undefined ? '' : options);
onMessage("webapp-reload-client", (options) => {
console.log(
"CLIENT TESTS RESTARTING (webapp-reload-client)",
options === undefined ? "" : options
);
clientTests();
});

0 comments on commit 4beee13

Please sign in to comment.