-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscenarist-loader.js
37 lines (37 loc) · 1.89 KB
/
scenarist-loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
@license https://github.com/t2ym/reportage/blob/master/LICENSE.md
Copyright (c) 2023 Tetsuya Mori <[email protected]>. All rights reserved.
*/
/*
Install a wrapped version of Suite to sandbox.Suite
The wrapped version uses sandbox.describe, etc. when Suite.run(classes, target, sandbox) is called
Suite class (not instance) object is unique and cannot be reset once loaded
*/
import { sandbox } from './sandbox-global.js';
import Resolved from './resolved-paths.js';
const scenaristResponse = await fetch(new URL(Resolved['scenarist/Suite.js'], import.meta.url)); // fetch the UMD version
const scenaristScriptText = await scenaristResponse.text();
// This patch expects scenarist 1.1.10
const scenaristScriptText_wrapped = scenaristScriptText
.replace("this.scope = target || '';", "this.scope = target || '';this.file = (new Error().stack.split('\\n')[2]).replace(/^ *at ([^(]*):[0-9]*:[0-9]*$$/, '$$1')")
.replace(/\(typeof ([A-Za-z]+) === 'function' \? ([A-Za-z]+) : ([A-Za-z]+)\)/g,
"(typeof sandbox.$1 === 'function' ? sandbox.$2 : sandbox.$3)")
.replace(/.call\(self, parameters\)/g, '.call(self, parameters, this)')
.replace(/.call\(self\)/g, '.call(self, this)')
.replace('self.setup()', 'self.setup(this)')
.replace('self.teardown()', 'self.teardown(this)')
.replace('run(classes, target)', 'run(classes, target, sandbox = globalThis)')
.replace('instance[1].run()', 'instance[1].run(undefined, undefined, sandbox)');
const scenaristInstaller = new Function('module', 'exports', scenaristScriptText_wrapped);
const _module = {
exports: {},
};
scenaristInstaller(_module, _module.exports); // disguise the UMD installer so that Suite can be installed at _module.exports
const Suite = _module.exports; // install Suite class into sandbox.Suite
Object.defineProperty(sandbox, 'Suite', {
configurable: true,
enumerable: true,
writable: true,
value: Suite,
});
export default Suite;