UNPKG

@codesandbox/sandpack-client

Version:

<img style="width:100%" src="https://user-images.githubusercontent.com/4838076/143581035-ebee5ba2-9cb1-4fe8-a05b-2f44bd69bb4b.gif" alt="Component toolkit for live running code editing experiences" />

1 lines 12.1 kB
(this.csbJsonP=this.csbJsonP||[]).push([[7],{"../../standalone-packages/codesandbox-browserfs/dist/shims/buffer.js":function(e,t){e.exports=BrowserFS.BFSRequire("buffer")},"../common/lib/utils/jest-lite.js":function(e,t,s){"use strict";var n;Object.defineProperty(t,"__esModule",{value:!0}),t.messages=void 0,function(e){e.INITIALIZE="initialize_tests",e.ADD_FILE="add_file",e.REMOVE_FILE="remove_file",e.FILE_ERROR="file_error",e.TOTAL_TEST_START="total_test_start",e.TOTAL_TEST_END="total_test_end",e.TEST_START="test_start",e.TEST_END="test_end",e.DESCRIBE_START="describe_start",e.DESCRIBE_END="describe_end",e.ADD_TEST="add_test"}(n||(t.messages=n={}))},"./src/sandbox/eval/tests/jest-lite.ts":function(e,t,s){"use strict";s.r(t),s.d(t,"messages",(function(){return u.messages})),s.d(t,"default",(function(){return P}));var n=s("../../node_modules/@babel/runtime/helpers/slicedToArray.js"),r=s.n(n),a=s("../../node_modules/@babel/runtime/helpers/asyncToGenerator.js"),o=s.n(a),i=s("../../node_modules/@babel/runtime/helpers/defineProperty.js"),c=s.n(i),d=s("../codesandbox-api/dist/codesandbox.es5.js"),l=s("../common/lib/templates/index.js"),u=s("../common/lib/utils/jest-lite.js"),h=s("./node_modules/expect/build/index.js"),p=s.n(h),m=s("./node_modules/jest-mock/build-es5/index.js"),b=s.n(m),f=s("../../node_modules/jest-circus/build/index.js"),g=s.n(f),j=s("../../node_modules/jest-circus/build/utils.js"),T=(s("../../node_modules/path-browserify/index.js"),s("../../node_modules/jest-each/build/index.js")),_=s("./node_modules/jest-snapshot/build/index.js"),O=s("../../node_modules/jest-circus/build/state.js"),E=s("../sandbox-hooks/react-error-overlay/utils/parser.js"),y=s("../sandbox-hooks/react-error-overlay/utils/mapper.js");const S=[],v=function(){var e=o()((function*(){const e=Object(O.getState)().rootDescribeBlock;return S.length=0,Object(O.dispatch)({name:"run_start"}),yield w(e),Object(O.dispatch)({name:"run_finish"}),Object(j.makeTestResults)(Object(O.getState)().rootDescribeBlock)}));return function(){return e.apply(this,arguments)}}(),w=function(){var e=o()((function*(e){S.push(e.name),Object(O.dispatch)({describeBlock:e,name:"run_describe_start"});const t=Object(j.getAllHooksForDescribe)(e),s=t.beforeAll,n=t.afterAll;for(const e of s)D(e);for(const t of e.tests)yield k(t);for(const t of e.children)yield w(t);for(const e of n)D(e);Object(O.dispatch)({describeBlock:e,name:"run_describe_finish"}),S.pop()}));return function(t){return e.apply(this,arguments)}}(),k=function(){var e=o()((function*(e){const t=Object.create(null);if("skip"===e.mode||Object(O.getState)().hasFocusedTests&&"only"!==e.mode)return void Object(O.dispatch)({name:"test_skip",test:e});const s=Object(j.getEachHooksForTest)(e),n=s.afterEach,r=s.beforeEach;for(const e of r)yield D(e,t);yield M(e,t);for(const e of n)yield D(e,t)}));return function(t){return e.apply(this,arguments)}}(),D=(e,t)=>{Object(O.dispatch)({hook:e,name:"hook_start"});const s=Object(O.getState)().testTimeout;return Object(j.callAsyncFn)(e.fn,t,{isHook:!0,timeout:s}).then(()=>Object(O.dispatch)({hook:e,name:"hook_success"})).catch(t=>Object(O.dispatch)({error:t,hook:e,name:"hook_failure"}))},M=function(){var e=o()((function*(e,t){Object(O.dispatch)({name:"test_start",test:e});const s=Object(O.getState)().testTimeout;if(!e.fn)throw Error("Tests with no 'fn' should have 'mode' set to 'skipped'");return(e=>{const t=p.a.getState().testPath,s=e.name.split(":#:"),n=r()(s,2),a=n[0],o=n[1],i=[...S].slice(1),c={currentTestName:(i.length>0?i.join(" ")+" ":"")+o};null!=a&&t===a||(c.snapshotState=new _.SnapshotState(a,{expand:!0,updateSnapshot:"none"}),c.testPath=a),p.a.setState(c)})(e),Object(j.callAsyncFn)(e.fn,t,{isHook:!1,timeout:s}).then(()=>Object(O.dispatch)({name:"test_success",test:e})).catch(t=>Object(O.dispatch)({error:t,name:"test_failure",test:e}))}));return function(t,s){return e.apply(this,arguments)}}();var x=v;function A(e,t){var s=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),s.push.apply(s,n)}return s}function R(e){for(var t=1;t<arguments.length;t++){var s=null!=arguments[t]?arguments[t]:{};t%2?A(Object(s),!0).forEach((function(t){c()(e,t,s[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(s)):A(Object(s)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(s,t))}))}return e}p.a.extend({toMatchSnapshot:_.toMatchSnapshot,toThrowErrorMatchingSnapshot:_.toThrowErrorMatchingSnapshot}),p.a.addSnapshotSerializer=_.addSerializer;let C=null;const N=()=>{let e="/static/js/jsdom-16.3.0.min.js";var t;return navigator.userAgent.indexOf("jsdom"),C=C||(t=e,new Promise((e,s)=>{const n=document.createElement("script");n.setAttribute("src",t),document.body.appendChild(n),n.onload=()=>{e()},n.onerror=e=>{s(e)}})),C};class P{constructor(e){var t=this;c()(this,"tests",void 0),c()(this,"ranTests",void 0),c()(this,"manager",void 0),c()(this,"watching",!0),c()(this,"LOCALHOST_URL","http://localhost"),c()(this,"dom",void 0),c()(this,"oldWindow",{}),c()(this,"oldEnvVars",void 0),c()(this,"handleMessage",function(){var e=o()((function*(e){switch(e.name){case"test_start":{const s=yield t.testToCodeSandbox(e.test);return t.sendMessage(u.messages.TEST_START,{test:s})}case"test_failure":case"test_success":{const s=p.a.getState().suppressedErrors;s&&s.length&&(e.test.errors=s,e.test.status="fail",p.a.setState({suppressedErrors:[]}));const n=yield t.testToCodeSandbox(e.test);n.errors&&n.errors.forEach(e=>{if(e.mappedErrors&&e.mappedErrors.length){const t=e.mappedErrors,s=r()(t,1)[0];Object(d.dispatch)(d.actions.error.show(e.name||"Jest Error",e.message,{line:s._originalLineNumber,column:s._originalColumnNumber,path:n.path,payload:{},source:"jest"}))}});try{return t.sendMessage(u.messages.TEST_END,{test:n})}catch(e){const s=yield t.errorToCodeSandbox(e);return t.sendMessage(u.messages.FILE_ERROR,{path:n.path,error:s})}}case"start_describe_definition":return t.sendMessage(u.messages.DESCRIBE_START,{blockName:e.blockName});case"finish_describe_definition":return t.sendMessage(u.messages.DESCRIBE_END);case"add_test":{const s=e.testName.split(":#:"),n=r()(s,2),a=n[0],o=n[1];return t.sendMessage(u.messages.ADD_TEST,{testName:o,path:a,mode:e.mode})}default:return null}}));return function(t){return e.apply(this,arguments)}}()),c()(this,"handleCodeSandboxMessage",e=>{switch(e.type){case"set-test-watching":this.watching=e.watching,!0===e.watching&&(this.ranTests.clear(),this.runTests(!0));break;case"run-all-tests":this.ranTests.clear(),this.runTests(!0);break;case"run-tests":{const t=e.path;this.ranTests.delete(t),this.runTests();break}}}),c()(this,"reportError",()=>{}),this.manager=e,this.ranTests=new Set,Object(O.addEventHandler)(this.handleMessage),Object(d.listen)(this.handleCodeSandboxMessage),this.sendMessage(u.messages.INITIALIZE)}getRuntimeGlobals(e){const t=(t,s)=>Object(O.dispatch)({fn:s,name:"add_test",testName:"".concat(e.path,":#:").concat(t)}),s=(t,s)=>Object(O.dispatch)({fn:s,mode:"skip",name:"add_test",testName:"".concat(e.path,":#:").concat(t)}),n=(t,s)=>{Object(O.dispatch)({fn:s,mode:"only",name:"add_test",testName:"".concat(e.path,":#:").concat(t)})};t.each=Object(T.bind)(t),s.each=Object(T.bind)(s),n.each=Object(T.bind)(n),t.only=n,t.skip=s;const r=t;return R(R({},g.a),{},{test:t,jest:b.a,it:r,expect:p.a})}setTestGlobals(e){const t=this.dom.window.document.defaultView,s=t.document;t.Date=Date,t.fetch=fetch;const n=this.getRuntimeGlobals(e),r={document:s,window:t,global:t,Event:t.Event};return Object.keys(n).forEach(e=>{window[e]=n[e]}),r}static isTest(e){return!(!e.includes("__tests__")||!(e.endsWith(".js")||e.endsWith(".ts")||e.endsWith(".tsx")))||[".test.js",".test.ts",".test.tsx",".test.jsx",".spec.js",".spec.ts",".spec.tsx",".spec.jsx"].filter(t=>e.endsWith(t)).length>0}findTests(e){return this.tests&&this.tests.forEach(t=>{e[t.path]||this.sendMessage(u.messages.REMOVE_FILE,{path:t.path})}),this.tests=Object.keys(e).filter(P.isTest).map(t=>e[t]),this.tests}transpileTests(){var e=this;return o()((function*(){return Promise.all((e.tests||[]).map(function(){var t=o()((function*(t){const s=e.manager.getTranspiledModule(t,"");if(s.source&&s.compilation&&e.ranTests.has(t.path))return null;e.sendMessage(u.messages.ADD_FILE,{path:t.path});try{return yield e.manager.transpileModules(t,!0),s.source||e.ranTests.delete(t.path),t}catch(s){const n=yield e.errorToCodeSandbox(s);return e.ranTests.delete(t.path),e.sendMessage(u.messages.FILE_ERROR,{path:t.path,error:n}),null}}));return function(e){return t.apply(this,arguments)}}()))}))()}sendMessage(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};Object(d.dispatch)(R({type:"test",event:e},t))}initJSDOM(){var e=this;return o()((function*(){yield N();const t=window.JSDOM.JSDOM;let s=document.location.origin;"null"===s&&(s=e.LOCALHOST_URL),e.dom=new t("<!DOCTYPE html>",{pretendToBeVisual:!0,url:s});["getComputedStyle"].forEach(t=>{e.oldWindow[t]=window[t],window[t]=e.dom.window[t]})}))()}setup(){var e=this;return o()((function*(){e.oldEnvVars=R({},e.manager.envVariables),e.manager.envVariables.NODE_ENV="test"}))()}teardown(){var e=this;return o()((function*(){const t=e.dom.window.document.defaultView;t.close(),Object.defineProperty(t,"document",{value:null}),e.dom=null,e.manager.envVariables=e.oldEnvVars,Object.keys(e.oldWindow).forEach(t=>{window[t]=e.oldWindow[t]}),e.oldWindow={};const s=e.getRuntimeGlobals();Object.keys(s).forEach(e=>{delete window[e]})}))()}runTests(){var e=this;let t=arguments.length>0&&void 0!==arguments[0]&&arguments[0];return o()((function*(){if(!e.watching&&!t)return;yield e.initJSDOM(),e.sendMessage(u.messages.TOTAL_TEST_START);let s=[];try{if(e.manager.preset.name===l.react.name)try{s=[yield e.manager.resolveModuleAsync({path:"./src/setupTests.js"})]}catch(t){s=[yield e.manager.resolveModuleAsync({path:"./src/setupTests.ts"})]}else if(e.manager.preset.name===l.reactTs.name)s=[yield e.manager.resolveModuleAsync({path:"./src/setupTests.ts"})];else if(e.manager.configurations.package){const t=e.manager.configurations.package.parsed;t&&t.jest&&t.jest.setupFilesAfterEnv&&(s=yield Promise.all(t.jest.setupFilesAfterEnv.map(t=>e.manager.resolveModuleAsync({path:t}))))}}catch(e){}s.length&&(yield Promise.all(s.map(t=>e.manager.transpileModules(t,!0)))),e.manager.modules&&e.findTests(e.manager.modules);const n=(yield e.transpileTests()).filter(e=>e);!function(){const e=Object(j.makeDescribe)(O.ROOT_DESCRIBE_BLOCK_NAME),t={currentDescribeBlock:e,expand:void 0,hasFocusedTests:!1,rootDescribeBlock:e,testTimeout:5e3};p.a.setState({assertionCalls:0,expectedAssertionsNumber:null,isExpectingAssertions:!1,suppressedErrors:[],testPath:null,currentTestName:null,snapshotState:null}),Object(O.setState)(t)}(),yield e.setup(),yield Promise.all(n.map(function(){var t=o()((function*(t){Object(d.dispatch)(d.actions.error.clear(t.path,"jest"));try{s.length&&s.forEach(t=>{e.manager.evaluateModule(t,{force:!0,globals:e.setTestGlobals(t)})}),e.manager.evaluateModule(t,{force:!0,globals:e.setTestGlobals(t)}),e.ranTests.add(t.path)}catch(s){e.ranTests.delete(t.path);const n=yield e.errorToCodeSandbox(s);e.sendMessage(u.messages.FILE_ERROR,{path:t.path,error:n})}}));return function(e){return t.apply(this,arguments)}}())),yield x(),yield e.teardown(),setTimeout(()=>{e.sendMessage(u.messages.TOTAL_TEST_END)})}))()}errorToCodeSandbox(e){return o()((function*(){const t=Object(E.parse)(e),s=yield Object(y.map)(t);return{name:e.name,message:e.message,stack:e.stack,matcherResult:Boolean(e.matcherResult),mappedErrors:s}}))()}getDescribeBlocks(e){let t=e;const s=[];for(;null!=t.parent;)s.push(t.parent.name),t=t.parent;return s.pop(),s.reverse()}testToCodeSandbox(e){var t=this;return o()((function*(){const s=e.name.split(":#:"),n=r()(s,2),a=n[0],o=n[1],i=yield Promise.all(e.errors.map(t.errorToCodeSandbox));return{name:o,path:a,duration:e.duration,status:e.status||"running",errors:i,blocks:t.getDescribeBlocks(e)}}))()}}},1:function(e,t){},2:function(e,t){}}]);