Skip to content

Commit

Permalink
fix: add mockSandbox at unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
wanghangit committed Dec 20, 2023
1 parent 58e693f commit 975bb6d
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions packages/shared/src/assets-transpilers/__tests__/script.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import { expect, it } from 'vitest';
import transpileScript from '../script';
import { StandardSandbox } from '../../../../sandbox/src/core/sandbox/StandardSandbox';

it('inline script not include sourceURL', () => {
class MockSandbox {
id = 'testApp';
constantIntrinsicNames = [];
makeEvaluateFactory(source: string, sourceURL?: string): string {
const sourceMapURL = sourceURL ? `//# sourceURL=${sourceURL}\n` : '';
const globalObjectOptimizer = this.constantIntrinsicNames.length
? `const {${this.constantIntrinsicNames.join(',')}} = this;`
: '';
// eslint-disable-next-line max-len
return `;(function(){with(this){${globalObjectOptimizer}${source}\n${sourceMapURL}}}).bind(window.${this.id})();`;
}
}

const scriptElement = document.createElement('script');
scriptElement.innerHTML = 'console.log("hello world")';
const sandboxInstance = new StandardSandbox("app", {});
const sandboxInstance = new MockSandbox();
const publicPath = 'http://localhost:8000';
const transpiledScriptElement = transpileScript(scriptElement, publicPath, {fetch: window.fetch, rawNode: scriptElement, sandbox: sandboxInstance});
const transpiledScriptElement = transpileScript(scriptElement, publicPath, {
fetch: window.fetch,
rawNode: scriptElement,
sandbox: sandboxInstance,
});
expect(transpiledScriptElement.innerHTML).toEqual(expect.not.stringContaining(`//# sourceURL=${publicPath}`));
});

0 comments on commit 975bb6d

Please sign in to comment.