diff --git a/packages/core/code-runner/package.json b/packages/core/code-runner/package.json index 6b638163a..63dc16057 100644 --- a/packages/core/code-runner/package.json +++ b/packages/core/code-runner/package.json @@ -38,31 +38,36 @@ "lint": "npm-run-all lint:*", "lint:js": "eslint src/", "lint:ts": "tslint -c tslint.json \"src/**/*.ts\"", - "qa": "npm-run-all lint _typecheck _build", + "qa": "npm-run-all lint _typecheck _build test", "reset": "npx rimraf dist embark-*.tgz package", - "solo": "embark-solo" + "solo": "embark-solo", + "test": "jest" }, "eslintConfig": { "extends": "../../../.eslintrc.json" }, "dependencies": { - "@babel/runtime-corejs3": "7.7.4", + "@babel/runtime-corejs3": "7.7.7", "@types/async": "3.0.3", - "async": "2.6.1", - "colors": "1.3.2", - "core-js": "3.4.3", + "async": "3.1.0", + "colors": "1.4.0", + "core-js": "3.6.2", "embark-core": "^5.0.0", "embark-logger": "^5.0.0", "embark-utils": "^5.0.0", "embarkjs": "^5.0.0", "fs-extra": "8.1.0", - "parse-json": "4.0.0", - "vm2": "3.6.4", + "parse-json": "5.0.0", + "vm2": "3.8.4", "web3": "1.2.4" }, "devDependencies": { + "@babel/core": "7.7.7", + "babel-jest": "24.9.0", "embark-solo": "^5.0.0", + "embark-testing": "^5.0.0", "eslint": "5.7.0", + "jest": "24.9.0", "npm-run-all": "4.1.5", "rimraf": "3.0.0", "tslint": "5.20.1", @@ -72,5 +77,20 @@ "node": ">=10.17.0 <12.0.0", "npm": ">=6.11.3", "yarn": ">=1.19.1" + }, + "jest": { + "collectCoverage": true, + "testEnvironment": "node", + "testMatch": [ + "**/test/**/*.js" + ], + "transform": { + "\\.(js|ts)$": [ + "babel-jest", + { + "rootMode": "upward" + } + ] + } } } diff --git a/packages/core/code-runner/src/index.ts b/packages/core/code-runner/src/index.ts index 0442b3783..c143bd2cd 100644 --- a/packages/core/code-runner/src/index.ts +++ b/packages/core/code-runner/src/index.ts @@ -38,7 +38,7 @@ class CodeRunner { private registerCommands() { this.events.setCommandHandler("runcode:getContext", (cb) => { - cb(this.vm.options.sandbox); + cb(null, this.vm.options.sandbox); }); this.events.setCommandHandler("runcode:eval", this.evalCode.bind(this)); } diff --git a/packages/core/code-runner/test/code-runner.spec.js b/packages/core/code-runner/test/code-runner.spec.js new file mode 100644 index 000000000..7238a7892 --- /dev/null +++ b/packages/core/code-runner/test/code-runner.spec.js @@ -0,0 +1,45 @@ +import assert from 'assert'; +import sinon from 'sinon'; +import { fakeEmbark } from 'embark-testing'; +import CodeRunner from '../src/'; + +// Due to our `DAPP_PATH` dependency in `embark-utils` `dappPath()`, we need to +// ensure that this environment variable is defined. +process.env.DAPP_PATH = 'something'; + +describe('core/code-runner', () => { + + const { embark, plugins } = fakeEmbark(); + + let codeRunner, doneCb; + + beforeEach(() => { + codeRunner = new CodeRunner(embark); + doneCb = sinon.fake(); + }); + + afterEach(() => { + embark.teardown(); + sinon.restore(); + }); + + test('it should register variables and eval code in the VM', async () => { + const testVar = { + foo: 'bar' + }; + await embark.events.request2('runcode:register', 'testVar', testVar); + const context = await embark.events.request2('runcode:getContext'); + assert.equal(context['testVar'], testVar); + }); + + test('it should run code in the VM', async () => { + const testVar = { + foo: 'bar' + }; + await embark.events.request2('runcode:register', 'testVar', testVar); + // `runcode:eval` throws a `ReferenceError` if `testVar` wasn't registered + // in the VM. + await embark.events.request2('runcode:eval', `testVar.foo = 'bar';`); + }); +}); + diff --git a/packages/core/code-runner/tsconfig.json b/packages/core/code-runner/tsconfig.json index 2d3a546cf..9b46c1fd5 100644 --- a/packages/core/code-runner/tsconfig.json +++ b/packages/core/code-runner/tsconfig.json @@ -13,6 +13,9 @@ { "path": "../../embarkjs/embarkjs" }, + { + "path": "../../utils/testing" + }, { "path": "../core" }, diff --git a/packages/embark/src/cmd/dashboard/repl.js b/packages/embark/src/cmd/dashboard/repl.js index 6b89ec2c8..c015714c9 100644 --- a/packages/embark/src/cmd/dashboard/repl.js +++ b/packages/embark/src/cmd/dashboard/repl.js @@ -107,7 +107,7 @@ class REPL { this.ipc.client.on('console:history:save', this.addHistory.bind(this)); } - this.events.request("runcode:getContext", (context) => { + this.events.request("runcode:getContext", (err, context) => { this.replServer.context = context; }); diff --git a/yarn.lock b/yarn.lock index a3c0abcbc..b31505d43 100644 --- a/yarn.lock +++ b/yarn.lock @@ -65,6 +65,26 @@ semver "^5.4.1" source-map "^0.5.0" +"@babel/core@7.7.7": + version "7.7.7" + resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.7.7.tgz#ee155d2e12300bcc0cff6a8ad46f2af5063803e9" + integrity sha512-jlSjuj/7z138NLZALxVgrx13AOtqip42ATZP7+kYl53GvDV6+4dCek1mVUo8z8c8Xnw/mx2q3d9HWh3griuesQ== + dependencies: + "@babel/code-frame" "^7.5.5" + "@babel/generator" "^7.7.7" + "@babel/helpers" "^7.7.4" + "@babel/parser" "^7.7.7" + "@babel/template" "^7.7.4" + "@babel/traverse" "^7.7.4" + "@babel/types" "^7.7.4" + convert-source-map "^1.7.0" + debug "^4.1.0" + json5 "^2.1.0" + lodash "^4.17.13" + resolve "^1.3.2" + semver "^5.4.1" + source-map "^0.5.0" + "@babel/core@^7.0.0", "@babel/core@^7.4.5": version "7.5.5" resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.5.5.tgz#17b2686ef0d6bc58f963dddd68ab669755582c30" @@ -117,6 +137,16 @@ lodash "^4.17.13" source-map "^0.5.0" +"@babel/generator@^7.7.7": + version "7.7.7" + resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.7.7.tgz#859ac733c44c74148e1a72980a64ec84b85f4f45" + integrity sha512-/AOIBpHh/JU1l0ZFS4kiRCBnLi6OTHzh0RPk3h9isBxkkqELtQNFi1Vr/tiG9p1yfoUdKVwISuXWQR+hwwM4VQ== + dependencies: + "@babel/types" "^7.7.4" + jsesc "^2.5.1" + lodash "^4.17.13" + source-map "^0.5.0" + "@babel/helper-annotate-as-pure@^7.0.0": version "7.0.0" resolved "https://registry.yarnpkg.com/@babel/helper-annotate-as-pure/-/helper-annotate-as-pure-7.0.0.tgz#323d39dd0b50e10c7c06ca7d7638e6864d8c5c32" @@ -526,6 +556,11 @@ resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.7.4.tgz#75ab2d7110c2cf2fa949959afb05fa346d2231bb" integrity sha512-jIwvLO0zCL+O/LmEJQjWA75MQTWwx3c3u2JOTDK5D3/9egrWRRA0/0hk9XXywYnXZVVpzrBYeIQTmhwUaePI9g== +"@babel/parser@^7.7.7": + version "7.7.7" + resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.7.7.tgz#1b886595419cf92d811316d5b715a53ff38b4937" + integrity sha512-WtTZMZAZLbeymhkd/sEaPD8IQyGAhmuTuvTzLiCFM7iXiVdY0gc0IaI+cW0fh1BnSMbJSzXX6/fHllgHKwHhXw== + "@babel/plugin-proposal-async-generator-functions@^7.2.0": version "7.2.0" resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-async-generator-functions/-/plugin-proposal-async-generator-functions-7.2.0.tgz#b289b306669dce4ad20b0252889a15768c9d417e" @@ -1758,6 +1793,14 @@ core-js-pure "^3.0.0" regenerator-runtime "^0.13.2" +"@babel/runtime-corejs3@7.7.7": + version "7.7.7" + resolved "https://registry.yarnpkg.com/@babel/runtime-corejs3/-/runtime-corejs3-7.7.7.tgz#78fcbd472daec13abc42678bfc319e58a62235a3" + integrity sha512-kr3W3Fw8mB/CTru2M5zIRQZZgC/9zOxNSoJ/tVCzjPt3H1/p5uuGbz6WwmaQy/TLQcW31rUhUUWKY28sXFRelA== + dependencies: + core-js-pure "^3.0.0" + regenerator-runtime "^0.13.2" + "@babel/runtime@7.3.4": version "7.3.4" resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.3.4.tgz#73d12ba819e365fcf7fd152aed56d6df97d21c83" @@ -6709,6 +6752,11 @@ colors@1.3.2: resolved "https://registry.yarnpkg.com/colors/-/colors-1.3.2.tgz#2df8ff573dfbf255af562f8ce7181d6b971a359b" integrity sha512-rhP0JSBGYvpcNQj4s5AdShMeE5ahMop96cTeDl/v9qQQm2fYClE2QXZRi8wLzc+GmXSxdIqqbOIAhyObEXDbfQ== +colors@1.4.0: + version "1.4.0" + resolved "https://registry.yarnpkg.com/colors/-/colors-1.4.0.tgz#c50491479d4c1bdaed2c9ced32cf7c7dc2360f78" + integrity sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA== + colors@^1.1.2, colors@^1.2.1: version "1.3.3" resolved "https://registry.yarnpkg.com/colors/-/colors-1.3.3.tgz#39e005d546afe01e01f9c4ca8fa50f686a01205d" @@ -7114,6 +7162,11 @@ core-js@3.4.3, core-js@^3.0.1, core-js@^3.0.4, core-js@^3.3.4: resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.4.3.tgz#09ea102412a368d5f73d24f082e41ac90c633a49" integrity sha512-BVvHidX8uAmLCYPfLpXTEex7jz1uZJ1mW+shhIsBdA716O8Fg6TOwSgenSyO/bvEtnGdOTpKRZPSh4bSVI1k9w== +core-js@3.6.2: + version "3.6.2" + resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.6.2.tgz#2799ea1a59050f0acf50dfe89b916d6503b16caa" + integrity sha512-hIE5dXkRzRvnZ5vhkRfQxUvDxQZmD9oueA08jDYRBKJHx+VIl/Pne/e0A4x9LObEEthC/TqiZybUoNM4tRgnKg== + core-js@^1.0.0: version "1.2.7" resolved "https://registry.yarnpkg.com/core-js/-/core-js-1.2.7.tgz#652294c14651db28fa93bd2d5ff2983a4f08c636" @@ -15682,14 +15735,7 @@ parse-json@4.0.0, parse-json@^4.0.0: error-ex "^1.3.1" json-parse-better-errors "^1.0.1" -parse-json@^2.2.0: - version "2.2.0" - resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-2.2.0.tgz#f480f40434ef80741f8469099f8dea18f55a4dc9" - integrity sha1-9ID0BDTvgHQfhGkJn43qGPVaTck= - dependencies: - error-ex "^1.2.0" - -parse-json@^5.0.0: +parse-json@5.0.0, parse-json@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-5.0.0.tgz#73e5114c986d143efa3712d4ea24db9a4266f60f" integrity sha512-OOY5b7PAEFV0E2Fir1KOkxchnZNCdowAJgQ5NuxjpBKTRP3pQhwkrkxqQjeoKJ+fO7bCpmIZaogI4eZGDMEGOw== @@ -15699,6 +15745,13 @@ parse-json@^5.0.0: json-parse-better-errors "^1.0.1" lines-and-columns "^1.1.6" +parse-json@^2.2.0: + version "2.2.0" + resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-2.2.0.tgz#f480f40434ef80741f8469099f8dea18f55a4dc9" + integrity sha1-9ID0BDTvgHQfhGkJn43qGPVaTck= + dependencies: + error-ex "^1.2.0" + parse-ms@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/parse-ms/-/parse-ms-2.1.0.tgz#348565a753d4391fa524029956b172cb7753097d" @@ -21129,6 +21182,11 @@ vm2@3.6.4: resolved "https://registry.yarnpkg.com/vm2/-/vm2-3.6.4.tgz#88b27a9f328a0630671841363692a57d24dead33" integrity sha512-LFj8YL9DyGn+fwgG2J+10HyuIpdIRHHN8/3NwKoc2e2t2Pr0aXV/2OSODceDR7NP7VNr8RTqmxHRYcwbNvpbwg== +vm2@3.8.4: + version "3.8.4" + resolved "https://registry.yarnpkg.com/vm2/-/vm2-3.8.4.tgz#882be9135467942d84d1af80b6e6549b57c69041" + integrity sha512-5HThl+RBO/pwE9SF0kM4nLrpq5vXHBNk4BMX27xztvl0j1RsZ4/PMVJAu9rM9yfOtTo5KroL7XNX3031ExleSw== + w3c-hr-time@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/w3c-hr-time/-/w3c-hr-time-1.0.1.tgz#82ac2bff63d950ea9e3189a58a65625fedf19045"