Skip to content

Commit

Permalink
Unit tests refactored and added e2e tests environment
Browse files Browse the repository at this point in the history
  • Loading branch information
szwacz committed Aug 15, 2016
1 parent 1d4322a commit 0377dba
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
node_modules
*.log
.DS_Store
Thumbs.db
*.log
*.autogenerated

# ignore everything in 'app' folder what had been generated from 'src' folder
Expand Down
14 changes: 14 additions & 0 deletions e2e/hello_world.e2e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { expect } from 'chai';
import testUtils from './utils';

describe('application launch', function () {

beforeEach(testUtils.beforeEach);
afterEach(testUtils.afterEach);

it('shows hello world text on screen after launch', function () {
return this.app.client.getText('#greet').then(function (text) {
expect(text).to.equal('Hello World!');
});
});
});
22 changes: 22 additions & 0 deletions e2e/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import electron from 'electron';
import { Application } from 'spectron';

var beforeEach = function () {
this.app = new Application({
path: electron,
args: ['app'],
waitTimeout: 10000,
});
return this.app.start();
};

var afterEach = function () {
if (this.app && this.app.isRunning()) {
return this.app.stop();
}
};

export default {
beforeEach: beforeEach,
afterEach: afterEach,
};
3 changes: 2 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';

require('./tasks/build');
require('./tasks/build_app');
require('./tasks/build_tests');
require('./tasks/start');
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
"prerelease": "gulp build --env=production",
"release": "build --x64 --publish never",
"start": "gulp start",
"pretest": "gulp build --env=test",
"test": "electron-mocha app/app.js --renderer --require source-map-support/register"
"pretest": "gulp build-unit --env=test",
"test": "electron-mocha app/specs.js.autogenerated --renderer --require source-map-support/register",
"pree2e": "gulp build-e2e --env=test",
"e2e": "mocha app/e2e.js.autogenerated --require source-map-support/register"
},
"devDependencies": {
"chai": "^3.5.0",
Expand All @@ -44,8 +46,10 @@
"gulp-plumber": "^1.1.0",
"gulp-util": "^3.0.6",
"gulp-watch": "^4.3.5",
"mocha": "^3.0.2",
"rollup": "^0.34.7",
"source-map-support": "^0.4.2",
"spectron": "^3.3.0",
"yargs": "^4.2.0"
}
}
2 changes: 1 addition & 1 deletion src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ app.on('ready', function () {

mainWindow.loadURL('file://' + __dirname + '/app.html');

if (env.name !== 'production') {
if (env.name === 'development') {
mainWindow.openDevTools();
}
});
Expand Down
32 changes: 6 additions & 26 deletions tasks/build.js → tasks/build_app.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,23 @@
'use strict';

var pathUtil = require('path');
var gulp = require('gulp');
var less = require('gulp-less');
var watch = require('gulp-watch');
var batch = require('gulp-batch');
var plumber = require('gulp-plumber');
var jetpack = require('fs-jetpack');

var bundle = require('./bundle');
var generateSpecsEntryFile = require('./generate_specs_entry_file');
var utils = require('./utils');

var projectDir = jetpack;
var srcDir = projectDir.cwd('./src');
var destDir = projectDir.cwd('./app');

// -------------------------------------
// Tasks
// -------------------------------------

var bundleApplication = function () {
return Promise.all([
bundle(srcDir.path('background.js'), destDir.path('background.js')),
bundle(srcDir.path('app.js'), destDir.path('app.js')),
]);
};

var bundleSpecs = function () {
return generateSpecsEntryFile().then(function (specEntryPointPath) {
return bundle(specEntryPointPath, destDir.path('app.js'));
});
};
var srcDir = jetpack.cwd('./src');
var destDir = jetpack.cwd('./app');

gulp.task('bundle', function () {
if (utils.getEnvName() === 'test') {
return bundleSpecs();
}
return bundleApplication();
return Promise.all([
bundle(srcDir.path('background.js'), destDir.path('background.js')),
bundle(srcDir.path('app.js'), destDir.path('app.js')),
]);
});

gulp.task('less', function () {
Expand Down
43 changes: 43 additions & 0 deletions tasks/build_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

var gulp = require('gulp');
var jetpack = require('fs-jetpack');
var bundle = require('./bundle');

// Spec files are scattered through the whole project. Here we're searching
// for them and generate one entry file which will run all the tests.
var generateEntryFile = function (dir, destFileName, filePattern) {
var fileBanner = "// This file is generated automatically.\n"
+ "// All modifications will be lost.\n";

return dir.findAsync('.', { matching: filePattern })
.then(function (specPaths) {
var fileContent = specPaths.map(function (path) {
return 'import "./' + path.replace(/\\/g, '/') + '";';
}).join('\n');
return dir.writeAsync(destFileName, fileBanner + fileContent);
})
.then(function () {
return dir.path(destFileName);
});
};

gulp.task('build-unit', ['environment'], function () {
var srcDir = jetpack.cwd('src');
var destDir = jetpack.cwd('app');

return generateEntryFile(srcDir, 'specs.js.autogenerated', '*.spec.js')
.then(function (entryFilePath) {
return bundle(entryFilePath, destDir.path('specs.js.autogenerated'));
});
});

gulp.task('build-e2e', ['build'], function () {
var srcDir = jetpack.cwd('e2e');
var destDir = jetpack.cwd('app');

return generateEntryFile(srcDir, 'e2e.js.autogenerated', '*.e2e.js')
.then(function (entryFilePath) {
return bundle(entryFilePath, destDir.path('e2e.js.autogenerated'));
});
});
24 changes: 0 additions & 24 deletions tasks/generate_specs_entry_file.js

This file was deleted.

0 comments on commit 0377dba

Please sign in to comment.