|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const co = require('co'); |
| 4 | +const SkeletonApp = require('../helpers/skeleton-app'); |
| 5 | +const chai = require('ember-cli-blueprint-test-helpers/chai'); |
| 6 | +const esprima = require('esprima'); |
| 7 | +const expect = chai.expect; |
| 8 | + |
| 9 | +describe('Acceptance: build', function() { |
| 10 | + this.timeout(30 * 1000); |
| 11 | + |
| 12 | + beforeEach(function() { |
| 13 | + this.app = new SkeletonApp(); |
| 14 | + }); |
| 15 | + |
| 16 | + afterEach(function() { |
| 17 | + this.app.teardown(); |
| 18 | + }); |
| 19 | + |
| 20 | + it('builds and rebuilds files', co.wrap(function*() { |
| 21 | + this.app.writeFile('app/app.ts', ` |
| 22 | + export function add(a: number, b: number) { |
| 23 | + return a + b; |
| 24 | + } |
| 25 | + `); |
| 26 | + |
| 27 | + let server = this.app.serve(); |
| 28 | + |
| 29 | + yield server.waitForBuild(); |
| 30 | + |
| 31 | + expectModuleBody(this.app, 'skeleton-app/app', ` |
| 32 | + exports.add = add; |
| 33 | + function add(a, b) { |
| 34 | + return a + b; |
| 35 | + } |
| 36 | + `); |
| 37 | + |
| 38 | + this.app.writeFile('app/app.ts', ` |
| 39 | + export const foo: string = 'hello'; |
| 40 | + `); |
| 41 | + |
| 42 | + yield server.waitForBuild(); |
| 43 | + |
| 44 | + expectModuleBody(this.app, 'skeleton-app/app', ` |
| 45 | + var foo = exports.foo = 'hello'; |
| 46 | + `); |
| 47 | + })); |
| 48 | +}); |
| 49 | + |
| 50 | +function extractModuleBody(script, moduleName) { |
| 51 | + let parsed = esprima.parseScript(script); |
| 52 | + let definition = parsed.body |
| 53 | + .filter(stmt => stmt.type === 'ExpressionStatement') |
| 54 | + .map(stmt => stmt.expression) |
| 55 | + .find(expr => |
| 56 | + expr.type === 'CallExpression' && |
| 57 | + expr.callee.type === 'Identifier' && |
| 58 | + expr.callee.name === 'define' && |
| 59 | + expr.arguments && |
| 60 | + expr.arguments[0] && |
| 61 | + expr.arguments[0].type === 'Literal' && |
| 62 | + expr.arguments[0].value === moduleName); |
| 63 | + |
| 64 | + let moduleDef = definition.arguments[2].body; |
| 65 | + |
| 66 | + // Strip `'use strict'` |
| 67 | + moduleDef.body.shift(); |
| 68 | + |
| 69 | + // Strip `__esModule` definition |
| 70 | + moduleDef.body.shift(); |
| 71 | + |
| 72 | + return moduleDef; |
| 73 | +} |
| 74 | + |
| 75 | +function expectModuleBody(app, name, body) { |
| 76 | + let src = app.readFile('dist/assets/skeleton-app.js'); |
| 77 | + let actual = extractModuleBody(src, name); |
| 78 | + let expected = esprima.parseScript(body); |
| 79 | + expect(actual.body).to.deep.equal(expected.body); |
| 80 | +} |
0 commit comments