Skip to content

Commit

Permalink
Tests WIP 2
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Dec 11, 2023
1 parent c2cff73 commit 7c90050
Showing 1 changed file with 31 additions and 21 deletions.
52 changes: 31 additions & 21 deletions test/with.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
// Imports

const support = require('./support/index.js'),
itSerializes = support.itSerializes.withOptions({strictEnv: false}),
{transpiledFiles} = support;
itSerializes = support.itSerializes.withOptions({strictEnv: false});

// Tests

Expand Down Expand Up @@ -389,41 +388,52 @@ describe('with statements', () => {

describe("does not disrupt instrumentation's internal vars", () => {
itSerializes('`livepack_tracker` + `livepack_setScopeId`', {
in() {
in: `
const x = 123;
with ({livepack_tracker: 1, livepack_getScopeId: 2}) {
const y = 456;
return () => [x, y];
module.exports = () => [x, y];
}
},
out: '(x=>b=>{with(b)return a=>()=>[x,a]})(123)({livepack_tracker:1,livepack_getScopeId:2})(456)',
validate(fn) {
`,
out: `(x=>b=>{
with(b)return a=>()=>[x,a]
})(123)({livepack_tracker:1,livepack_getScopeId:2})(456)`,
validate(fn, {transpiled}) {
expect(fn).toBeFunction();
expect(fn()).toEqual([123, 456]);

// Check internal functions match the ones being tested for
expect(transpiledFiles[__filename]).toInclude(
// eslint-disable-next-line no-useless-concat
'const [livepack_tracker, livepack_getScopeId] = ' + 'require('
);
expect(transpiled).toInclude('const [livepack_tracker, livepack_getScopeId] = require(');
}
});

itSerializes('`livepack_scopeId`', {
in: `
const x = 123;
with ({livepack_scopeId_2: 1}) {
module.exports = () => x;
const fns = [];
for (const x of [1, 2]) {
with ({livepack_scopeId_4: 3}) {
fns.push(() => x);
}
}
module.exports = fns;
`,
out: '(x=>a=>{with(a)return()=>x})(123)({livepack_scopeId_2:1})',
validate(fn, {transpiled}) {
expect(fn).toBeFunction();
expect(fn()).toBe(123);
out: `(()=>{
const a=x=>a=>{with(a)return()=>x};
return[
a(1)({livepack_scopeId_4:3}),
a(2)({livepack_scopeId_4:3})
]
})()`,
validate(fns, {transpiled}) {
expect(fns).toBeArrayOfSize(2);
const [fn1, fn2] = fns;
expect(fn1).toBeFunction();
expect(fn1()).toBe(1);
expect(fn2()).toBe(2);

// Check internal var matches the one being tested for
expect(transpiled.split('\n')[0])
.toInclude(';const livepack_scopeId_2 = livepack_getScopeId();');
expect(transpiled)
.toInclude('for (const x of [1, 2]) {const livepack_scopeId_4 = livepack_getScopeId();');
}
});

Expand All @@ -450,7 +460,7 @@ describe('with statements', () => {
return 123;
}
}
with ({livepack_temp_20: 1}) module.exports = class C extends S {
with (Object.freeze({livepack_temp_20: 1})) module.exports = class C extends S {
foo() {
return super.foo();
}
Expand Down

0 comments on commit 7c90050

Please sign in to comment.