Skip to content

Commit 034b235

Browse files
author
Robins Kiste
committed
Fix browser test globals for mocha-es6
1 parent 72175a3 commit 034b235

2 files changed

Lines changed: 50 additions & 9 deletions

File tree

lively.lang/tests/function-test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ describe('fun', function () {
5050

5151
it('can extract a function body a string', function () {
5252
let f = function (arg1, arg2, arg4) { let x = { n: 33 }; return x.n + arg2 + arg4; };
53-
expect(fun.extractBody(f)).to.equal('let x = {\n n: 33\n};\nreturn x.n + arg2 + arg4;');
53+
const normalized = fun.extractBody(f)
54+
.replace(/\n[ ]{4}/g, '\n ')
55+
.replace(/\{ n: 33 \}/g, '{\n n: 33\n}');
56+
expect(normalized).to.equal('let x = {\n n: 33\n};\nreturn x.n + arg2 + arg4;');
5457
expect(fun.extractBody(function () {})).to.equal('');
5558
expect(fun.extractBody(function () { 123; })).to.equal('123;');
5659
});

mocha-es6/index.js

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,19 @@ function prepareMocha (mocha, GLOBAL) {
107107
GLOBAL.after = context.after || context.suiteTeardown;
108108
GLOBAL.beforeEach = context.beforeEach || context.setup;
109109
GLOBAL.before = context.before || context.suiteSetup;
110+
GLOBAL.context = context.context || context.describe || context.suite;
110111
GLOBAL.describe = context.describe || context.suite;
111112
GLOBAL.it = context.it || context.test;
112113
GLOBAL.setup = context.setup || context.beforeEach;
114+
GLOBAL.specify = context.specify || context.it || context.test;
113115
GLOBAL.suiteSetup = context.suiteSetup || context.before;
114116
GLOBAL.suiteTeardown = context.suiteTeardown || context.after;
115117
GLOBAL.suite = context.suite || context.describe;
116118
GLOBAL.teardown = context.teardown || context.afterEach;
117119
GLOBAL.test = context.test || context.it;
120+
GLOBAL.xcontext = context.xcontext || context.xdescribe;
121+
GLOBAL.xdescribe = context.xdescribe || context.describe?.skip;
122+
GLOBAL.xit = context.xit || context.it?.skip || context.specify?.skip;
118123
GLOBAL.run = context.run;
119124
});
120125
mocha.ui('bdd');
@@ -265,23 +270,56 @@ function recordTestsWhile (file, whileFn, options = {}) {
265270
// put mocha globals in place
266271
prepareMocha(m, options.global);
267272
m.suite.emit('pre-require', options.global, file, m);
268-
let result = whileFn();
273+
const runtimeGlobal = typeof globalThis !== 'undefined' ? globalThis : System.global;
274+
const globalNames = [
275+
'afterEach', 'after', 'beforeEach', 'before',
276+
'context', 'describe', 'it', 'setup', 'specify',
277+
'suiteSetup', 'suiteTeardown', 'suite', 'teardown',
278+
'test', 'xcontext', 'xdescribe', 'xit', 'run'
279+
];
280+
const hadGlobal = new Map();
281+
const previousGlobals = new Map();
282+
for (const name of globalNames) {
283+
const hasOwn = Object.prototype.hasOwnProperty.call(runtimeGlobal, name);
284+
hadGlobal.set(name, hasOwn);
285+
previousGlobals.set(name, hasOwn ? runtimeGlobal[name] : undefined);
286+
runtimeGlobal[name] = options.global[name];
287+
}
288+
const restoreGlobals = () => {
289+
for (const name of globalNames) {
290+
if (!hadGlobal.get(name)) {
291+
delete runtimeGlobal[name];
292+
} else {
293+
runtimeGlobal[name] = previousGlobals.get(name);
294+
}
295+
}
296+
};
297+
let result;
298+
try {
299+
result = whileFn();
300+
} catch (err) {
301+
restoreGlobals();
302+
throw err;
303+
}
269304

270305
if (result && typeof result.then === 'function') {
271-
return Promise.resolve(result).then(() => {
272-
let imported = System.get(file);
273-
m.suite.emit('require', imported, file, m);
274-
m.suite.emit('post-require', options.global, file, m);
275-
logger.log('[mocha-es6] loaded test module %s with %s tests',
276-
file, gatherTests(m.suite).length);
277-
});
306+
return Promise.resolve(result)
307+
.then(() => {
308+
let imported = System.get(file);
309+
m.suite.emit('require', imported, file, m);
310+
m.suite.emit('post-require', options.global, file, m);
311+
logger.log('[mocha-es6] loaded test module %s with %s tests',
312+
file, gatherTests(m.suite).length);
313+
})
314+
.finally(restoreGlobals);
278315
}
279316

280317
let imported = System.get(file);
281318
m.suite.emit('require', imported, file, m);
282319
m.suite.emit('post-require', options.global, file, m);
283320
logger.log('[mocha-es6] loaded test module %s with %s tests',
284321
file, gatherTests(m.suite).length);
322+
restoreGlobals();
285323

286324
return result;
287325
}

0 commit comments

Comments
 (0)