refactor(ses): prepare performEval test for refactor
MODIFY
packages/ses/test/test-evaluate.js
packages/ses/test/test-evaluate.js
@@ -7,26 +7,23 @@ test('performEval - default (non-sloppy, no localObject)', t => {
t.plan(6);
const globalObject = { abc: 123 };
+ const evaluate = source => performEval(source, globalObject);
- t.is(
- performEval('typeof def', globalObject),
- 'undefined',
- 'typeof non declared global',
- );
+ t.is(evaluate('typeof def'), 'undefined', 'typeof non declared global');
t.throws(
- () => performEval('def', globalObject),
+ () => evaluate('def'),
{ instanceOf: ReferenceError },
'non declared global cause a reference error',
);
- t.is(performEval('abc', globalObject), 123, 'globals can be referenced');
+ t.is(evaluate('abc'), 123, 'globals can be referenced');
t.is(
- performEval('this.def = abc + 333', globalObject),
+ evaluate('this.def = abc + 333'),
456,
'globals can be defined through `this`',
);
- t.is(performEval('def', globalObject), 456, 'defined global persists');
+ t.is(evaluate('def'), 456, 'defined global persists');
t.is(globalObject.def, 456, 'assigned global uses the global object');
});
@@ -34,29 +31,18 @@ test('performEval - sloppyGlobalsMode', t => {
t.plan(5);
const globalObject = {};
- const options = { sloppyGlobalsMode: true };
+ const evaluate = source =>
+ performEval(source, globalObject, {}, { sloppyGlobalsMode: true });
+ t.is(evaluate('typeof def'), 'undefined', 'typeof non declared global');
t.is(
- performEval('typeof def', globalObject, {}, options),
- 'undefined',
- 'typeof non declared global',
- );
- t.is(
- performEval('def', globalObject, {}, options),
+ evaluate('def'),
undefined,
'non declared global do not cause a reference error',
);
- t.is(
- performEval('def = 456', globalObject, {}, options),
- 456,
- 'define global through sloppy assignment',
- );
- t.is(
- performEval('def', globalObject, {}, options),
- 456,
- 'defined global persists',
- );
+ t.is(evaluate('def = 456'), 456, 'define global through sloppy assignment');
+ t.is(evaluate('def'), 456, 'defined global persists');
t.is(globalObject.def, 456, 'assigned global uses the global object');
});
@@ -65,19 +51,14 @@ test('performEval - endowments', t => {
const globalObject = {};
const endowments = { abc: 123 };
+ const endowedEvaluate = source =>
+ performEval(source, globalObject, endowments);
+ const evaluate = source => performEval(source, globalObject);
- t.is(
- performEval('abc', globalObject, endowments),
- 123,
- 'endowments can be referenced',
- );
- t.is(
- performEval('abc += 333', globalObject, endowments),
- 456,
- 'endowments can be mutated',
- );
+ t.is(endowedEvaluate('abc'), 123, 'endowments can be referenced');
+ t.is(endowedEvaluate('abc += 333'), 456, 'endowments can be mutated');
t.throws(
- () => performEval('abc', globalObject),
+ () => evaluate('abc'),
{ instanceOf: ReferenceError },
'endowments do not affect other evaluate scopes with same globalObject (do not persist)',
);
@@ -107,17 +88,16 @@ test('performEval - transforms - rewrite source', t => {
},
];
- t.is(
- performEval('ABC', globalObject, endowments, {
+ const evaluate = (source, options = {}) =>
+ performEval(source, globalObject, endowments, {
+ ...options,
globalTransforms,
- }),
- 123,
- 'globalTransforms rewrite source',
- );
+ });
+
+ t.is(evaluate('ABC'), 123, 'globalTransforms rewrite source');
t.is(
- performEval('ABC', globalObject, endowments, {
+ evaluate('ABC', {
localTransforms,
- globalTransforms,
}),
456,
'localTransforms rewrite source first',

refactor(ses): prepare performEval test for refactor