diff --git a/packages/stack/compiler/package.json b/packages/stack/compiler/package.json index fbeeeb15a..f88b5880f 100644 --- a/packages/stack/compiler/package.json +++ b/packages/stack/compiler/package.json @@ -34,9 +34,10 @@ "clean": "npm run reset", "lint": "npm-run-all lint:*", "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", + "test": "jest", "typecheck": "tsc", "watch": "run-p watch:*", "watch:typecheck": "npm run typecheck -- --preserveWatchOutput --watch" @@ -47,7 +48,13 @@ "embark-utils": "^4.1.1" }, "devDependencies": { + "@babel/core": "7.6.4", + "babel-jest": "24.9.0", + "clone-deep": "4.0.0", + "cross-env": "5.2.0", "embark-solo": "^4.1.1", + "embark-testing": "^4.1.1", + "jest": "24.9.0", "npm-run-all": "4.1.5", "rimraf": "3.0.0", "tslint": "5.16.0", @@ -57,5 +64,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/stack/compiler/test/compiler.spec.js b/packages/stack/compiler/test/compiler.spec.js new file mode 100644 index 000000000..71fb57768 --- /dev/null +++ b/packages/stack/compiler/test/compiler.spec.js @@ -0,0 +1,74 @@ +import sinon from 'sinon'; +import assert from 'assert'; +import { File, Types } from 'embark-utils'; +import { fakeEmbark } from 'embark-testing'; +import Compiler from '../src/'; + +const { embark, plugins } = fakeEmbark(); + +// 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('stack/compiler', () => { + + let compiler, doneCb; + + beforeEach(() => { + compiler = new Compiler(embark, { plugins }); + }); + + afterEach(() => { + embark.teardown(); + sinon.restore(); + }); + + test('it should use registered compiler', done => { + const fooCompiler = sinon.spy((files, options, cb) => cb(null, { + contractA: 'someResultA', + contractB: 'someResultB', + contractC: 'someResultC', + })); + + embark.plugins.createPlugin('fooCompiler').registerCompiler('.foo', fooCompiler); + + embark.events.request('compiler:contracts:compile', [ + new File({filename: 'foo.foo', type: Types.dappFile, path: 'foo.foo'}), + new File({filename: 'foo2.foo', type: Types.dappFile, path: 'foo2.foo'}), + new File({filename: 'foo3.foo', type: Types.dappFile, path: 'foo3.foo'}), + ], () => { + assert(fooCompiler.called); + done(); + }); + }); + + test('it should iterate over available compilers to find a match for a given source file', done => { + const fooCompiler = sinon.spy((files, options, cb) => cb(null, { fooContract: 'foo', })); + const barCompiler = sinon.spy((files, options, cb) => cb(null, { barContract: 'bar', })); + + embark.plugins.createPlugin('fooCompiler').registerCompiler('.foo', fooCompiler); + embark.plugins.createPlugin('barCompiler').registerCompiler('.bar', barCompiler); + + embark.events.request('compiler:contracts:compile', [ + new File({filename: 'foo.bar', type: Types.dappFile, path: 'foo.bar'}), + ], () => { + assert(fooCompiler.notCalled); + assert(barCompiler.called); + done(); + }); + }); + + test('it should not compile source files if there is no matching compiler', done => { + const fooCompiler = sinon.spy((files, options, cb) => cb(null, { fooContract: 'foo', })); + embark.plugins.createPlugin('fooCompiler').registerCompiler('.foo', fooCompiler); + + const files = [ + new File({filename: 'foo.bar', type: Types.dappFile, path: 'foo.bar'}) + ]; + + embark.events.request('compiler:contracts:compile', files, () => { + files.forEach(file => assert(!file.compiled)); + done(); + }); + }); +});