Skip to content

Node acceptance tests, round 2 #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ module.exports = {
env: {
mocha: true,
},
rules: {
'node/no-unpublished-require': 'off',
},
},
],
};
86 changes: 86 additions & 0 deletions node-tests/acceptance/build-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
'use strict';

const co = require('co');
const SkeletonApp = require('../helpers/skeleton-app');
const chai = require('ember-cli-blueprint-test-helpers/chai');
const esprima = require('esprima');
const expect = chai.expect;

describe('Acceptance: build', function() {
this.timeout(30 * 1000);

beforeEach(function() {
this.app = new SkeletonApp();
});

afterEach(function() {
this.app.teardown();
});

it('builds and rebuilds files', co.wrap(function*() {
this.app.writeFile('app/app.ts', `
export function add(a: number, b: number) {
return a + b;
}
`);

let server = this.app.serve();

yield server.waitForBuild();

expectModuleBody(this.app, 'skeleton-app/app', `
exports.add = add;
function add(a, b) {
return a + b;
}
`);

this.app.writeFile('app/app.ts', `
export const foo: string = 'hello';
`);

yield server.waitForBuild();

expectModuleBody(this.app, 'skeleton-app/app', `
var foo = exports.foo = 'hello';
`);
}));

it('fails the build when noEmitOnError is set and an error is emitted', co.wrap(function*() {
this.app.writeFile('app/app.ts', `import { foo } from 'nonexistent';`);

yield expect(this.app.build()).to.be.rejectedWith(`Cannot find module 'nonexistent'`);
}));
});

function extractModuleBody(script, moduleName) {
let parsed = esprima.parseScript(script);
let definition = parsed.body
.filter(stmt => stmt.type === 'ExpressionStatement')
.map(stmt => stmt.expression)
.find(expr =>
expr.type === 'CallExpression' &&
expr.callee.type === 'Identifier' &&
expr.callee.name === 'define' &&
expr.arguments &&
expr.arguments[0] &&
expr.arguments[0].type === 'Literal' &&
expr.arguments[0].value === moduleName);

let moduleDef = definition.arguments[2].body;

// Strip `'use strict'`
moduleDef.body.shift();

// Strip `__esModule` definition
moduleDef.body.shift();

return moduleDef;
}

function expectModuleBody(app, name, body) {
let src = app.readFile('dist/assets/skeleton-app.js');
let actual = extractModuleBody(src, name);
let expected = esprima.parseScript(body);
expect(actual.body).to.deep.equal(expected.body);
}
Empty file.
Empty file.
9 changes: 9 additions & 0 deletions node-tests/fixtures/skeleton-app/config/environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-env node */
'use strict';

module.exports = function(environment) {
return {
environment,
modulePrefix: 'skeleton-app'
};
};
7 changes: 7 additions & 0 deletions node-tests/fixtures/skeleton-app/ember-cli-build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

const EmberApp = require('ember-cli/lib/broccoli/ember-app');

module.exports = function(defaults) {
return new EmberApp(defaults, {}).toTree();
};
14 changes: 14 additions & 0 deletions node-tests/fixtures/skeleton-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "skeleton-app",
"devDependencies": {
"ember-cli": "*",
"ember-cli-htmlbars": "*",
"ember-cli-babel": "*",
"ember-source": "*",
"loader.js": "*",
"typescript": "*"
},
"ember-addon": {
"paths": [".."]
}
}
15 changes: 15 additions & 0 deletions node-tests/fixtures/skeleton-app/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "ES6",
"allowJs": false,
"moduleResolution": "node",
"noEmitOnError": true,
"baseUrl": ".",
"paths": {
"skeleton-app/*": ["app/*"]
}
},
"include": [
"app"
]
}
105 changes: 105 additions & 0 deletions node-tests/helpers/skeleton-app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
'use strict';

const fs = require('fs-extra');
const path = require('path');
const mktemp = require('mktemp');
const execa = require('execa');
const EventEmitter = require('events').EventEmitter;

module.exports = class SkeletonApp {
constructor() {
this._watched = null;
this.root = mktemp.createDirSync('test-skeleton-app-XXXXXX');
fs.copySync(`${__dirname}/../fixtures/skeleton-app`, this.root);
}

build() {
return this._ember(['build']);
}

serve() {
if (this._watched) {
throw new Error('Already serving');
}

return this._watched = new WatchedBuild(this._ember(['serve']));
}

updatePackageJSON(callback) {
let pkgPath = `${this.root}/package.json`;
let pkg = fs.readJSONSync(pkgPath);
fs.writeJSONSync(pkgPath, callback(pkg) || pkg, { spaces: 2 });
}

writeFile(filePath, contents) {
let fullPath = `${this.root}/${filePath}`;
fs.ensureDirSync(path.dirname(fullPath));
fs.writeFileSync(fullPath, contents, 'utf-8');
}

readFile(path) {
return fs.readFileSync(`${this.root}/${path}`, 'utf-8');
}

removeFile(path) {
return fs.unlinkSync(`${this.root}/${path}`);
}

teardown() {
if (this._watched) {
this._watched.kill();
}

this._cleanupRootDir({ retries: 1 });
}

_ember(args) {
let ember = require.resolve('ember-cli/bin/ember');
return execa('node', [ember].concat(args), { cwd: this.root });
}

_cleanupRootDir(options) {
let retries = options && options.retries || 0;

try {
fs.removeSync(this.root);
} catch (error) {
if (retries > 0) {
// Windows doesn't necessarily kill the process immediately, so
// leave a little time before trying to remove the directory.
setTimeout(() => this._cleanupRootDir({ retries: retries - 1 }), 250);
} else {
// eslint-disable-next-line no-console
console.warn(`Warning: unable to remove skeleton-app tmpdir ${this.root} (${error.code})`);
}
}
}
}

class WatchedBuild extends EventEmitter {
constructor(ember) {
super();
this._ember = ember;
this._ember.stdout.on('data', (data) => {
let output = data.toString();
if (output.includes('Build successful')) {
this.emit('did-rebuild');
}
});

this._ember.catch((error) => {
this.emit('did-error', error);
});
}

waitForBuild() {
return new Promise((resolve, reject) => {
this.once('did-rebuild', resolve);
this.once('did-error', reject);
});
}

kill() {
this._ember.kill();
}
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"@types/node": "^9.6.5",
"@types/qunit": "^2.0.31",
"broccoli-asset-rev": "^2.6.0",
"co": "^4.6.0",
"ember-cli": "~3.1.4",
"ember-cli-app-version": "^3.1.3",
"ember-cli-babel": "^6.6.0",
Expand All @@ -93,7 +94,9 @@
"eslint": "^4.17.0",
"eslint-plugin-ember": "^5.0.3",
"eslint-plugin-node": "^6.0.1",
"esprima": "^4.0.0",
"loader.js": "^4.2.3",
"mktemp": "^0.4.0",
"mocha": "^5.0.0",
"testdouble": "^3.5.0",
"typescript": "^2.7.2"
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5288,7 +5288,7 @@ [email protected], [email protected], "mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdirp@^0.5.1:
dependencies:
minimist "0.0.8"

mktemp@~0.4.0:
mktemp@^0.4.0, mktemp@~0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/mktemp/-/mktemp-0.4.0.tgz#6d0515611c8a8c84e484aa2000129b98e981ff0b"

Expand Down