2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-12-22 10:08:55 +00:00

Migrate Mocha tests to Jest

This commit is contained in:
Goh Jia Hao 2018-05-23 23:34:18 -07:00
parent 17ccda36f0
commit 949dcfadd8
14 changed files with 175 additions and 232 deletions

View File

@ -4,7 +4,7 @@ import runSequence from 'run-sequence';
import PATHS from './helpers/src-paths';
gulp.task('build', (callback) => {
runSequence('clean', ['build-cli', 'build-app', 'build-tests'], callback);
runSequence('clean', ['build-cli', 'build-app'], callback);
});
gulp.task('clean', (callback) => {

View File

@ -1,11 +0,0 @@
import gulp from 'gulp';
import runSequence from 'run-sequence';
import helpers from './helpers/gulp-helpers';
const { shellExec } = helpers;
gulp.task('prune', (done) => {
shellExec('npm prune', true, done);
});
gulp.task('test', callback => runSequence('prune', 'mocha', callback));

View File

@ -1,7 +0,0 @@
import gulp from 'gulp';
import PATHS from './../helpers/src-paths';
import helpers from './../helpers/gulp-helpers';
const { buildES6 } = helpers;
gulp.task('build-tests', done => buildES6(PATHS.TEST_SRC_JS, PATHS.TEST_DEST, done));

View File

@ -1,27 +0,0 @@
import gulp from 'gulp';
import istanbul from 'gulp-istanbul';
import { Instrumenter } from 'isparta';
import mocha from 'gulp-mocha';
import PATHS from './../helpers/src-paths';
gulp.task('mocha', (done) => {
gulp.src([PATHS.CLI_SRC_JS, '!src/cli.js'])
.pipe(istanbul({
instrumenter: Instrumenter,
includeUntested: true,
}))
.pipe(istanbul.hookRequire()) // Force `require` to return covered files
.on('finish', () => gulp.src(PATHS.TEST_SRC, { read: false })
.pipe(mocha({
require: 'babel-core/register',
recursive: true,
}))
.pipe(istanbul.writeReports({
dir: './coverage',
reporters: ['lcov'],
reportOpts: { dir: './coverage' },
}))
.on('end', done));
});
gulp.task('tdd', ['mocha'], () => gulp.watch(['src/**/*.js', 'test/**/*.js'], ['mocha']));

View File

@ -7,12 +7,7 @@ gulp.task('watch', ['build'], () => {
const handleError = function watch(error) {
log.error(error);
};
gulp.watch(PATHS.APP_ALL, ['build-app'])
.on('error', handleError);
gulp.watch(PATHS.APP_ALL, ['build-app']).on('error', handleError);
gulp.watch(PATHS.CLI_SRC_JS, ['build-cli'])
.on('error', handleError);
gulp.watch(PATHS.TEST_SRC_JS, ['build-tests'])
.on('error', handleError);
gulp.watch(PATHS.CLI_SRC_JS, ['build-cli']).on('error', handleError);
});

4
jest.config.js Normal file
View File

@ -0,0 +1,4 @@
module.exports = {
testMatch: ['**/src/**/?(*.)(test).js?(x)'],
testEnvironment: 'node',
};

View File

@ -13,13 +13,11 @@
"scripts": {
"dev-up": "npm install && (cd ./app && npm install) && npm run build",
"dev-up-win": "npm install & cd app & npm install & cd .. & npm run build",
"test": "jest && gulp test",
"jest": "jest",
"e2e": "gulp test",
"test": "jest",
"tdd": "gulp tdd",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"ci": "gulp build test && npm run lint",
"ci": "npm run lint && npm test",
"clean": "gulp clean",
"build": "gulp build",
"watch": "while true ; do gulp watch ; done",
@ -73,10 +71,7 @@
"eslint-plugin-import": "^2.8.0",
"gulp": "^3.9.1",
"gulp-babel": "^7.0.1",
"gulp-istanbul": "^1.1.3",
"gulp-mocha": "^5.0.0",
"gulp-sourcemaps": "^2.6.4",
"isparta": "^4.0.0",
"jest": "^22.1.4",
"regenerator-runtime": "^0.11.1",
"require-dir": "^1.0.0",
@ -97,10 +92,5 @@
}
]
]
},
"jest": {
"testMatch": [
"**/src/**/?(*.)(spec|test).js?(x)"
]
}
}

View File

@ -0,0 +1,39 @@
import fs from 'fs';
import os from 'os';
import path from 'path';
import convertToIcns from './convertToIcns';
// Prerequisite for test: to use OSX with sips, iconutil and imagemagick convert
function testConvertPng(pngName, done) {
if (os.platform() !== 'darwin') {
// Skip png conversion tests, OSX is required
done();
return;
}
convertToIcns(
path.join(__dirname, '../../', 'test-resources', pngName),
(error, icnsPath) => {
if (error) {
done(error);
return;
}
const stat = fs.statSync(icnsPath);
expect(stat.isFile()).toBe(true);
done();
},
);
}
describe('Get Icon Module', () => {
test('Can convert a rgb png to icns', (done) => {
testConvertPng('iconSample.png', done);
});
test('Can convert a grey png to icns', (done) => {
testConvertPng('iconSampleGrey.png', done);
});
});

83
src/index.test.js Normal file
View File

@ -0,0 +1,83 @@
import tmp from 'tmp';
import fs from 'fs';
import path from 'path';
import async from 'async';
import nativefier from './index';
const PLATFORMS = ['darwin', 'linux'];
tmp.setGracefulCleanup();
function checkApp(appPath, inputOptions, callback) {
try {
let relPathToConfig;
switch (inputOptions.platform) {
case 'darwin':
relPathToConfig = path.join(
'google-test-app.app',
'Contents/Resources/app',
);
break;
case 'linux':
relPathToConfig = 'resources/app';
break;
case 'win32':
relPathToConfig = 'resources/app';
break;
default:
throw new Error('Unknown app platform');
}
const nativefierConfigPath = path.join(
appPath,
relPathToConfig,
'nativefier.json',
);
const nativefierConfig = JSON.parse(fs.readFileSync(nativefierConfigPath));
expect(inputOptions.targetUrl).toBe(nativefierConfig.targetUrl);
// app name is not consistent for linux
// assert.strictEqual(inputOptions.appName, nativefierConfig.name,
// 'Packaged app must have the same name as the input parameters');
callback();
} catch (exception) {
callback(exception);
}
}
describe('Nativefier Module', () => {
jest.setTimeout(240000);
test('Can build an app from a target url', (done) => {
async.eachSeries(
PLATFORMS,
(platform, callback) => {
const tmpObj = tmp.dirSync({ unsafeCleanup: true });
const tmpPath = tmpObj.name;
const options = {
name: 'google-test-app',
targetUrl: 'http://google.com',
out: tmpPath,
overwrite: true,
platform: null,
};
options.platform = platform;
nativefier(options, (error, appPath) => {
if (error) {
callback(error);
return;
}
checkApp(appPath, options, (err) => {
callback(err);
});
});
},
(error) => {
done(error);
},
);
});
});

View File

@ -0,0 +1,44 @@
import _ from 'lodash';
import inferUserAgent from './inferUserAgent';
const TEST_RESULT = {
darwin:
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36',
mas:
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36',
win32:
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36',
linux:
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36',
};
function testPlatform(platform) {
return inferUserAgent('0.37.1', platform).then((userAgent) => {
expect(userAgent).toBe(TEST_RESULT[platform]);
});
}
describe('Infer User Agent', () => {
test('Can infer userAgent for all platforms', (done) => {
const testPromises = _.keys(TEST_RESULT).map(platform => testPlatform(platform));
Promise.all(testPromises)
.then(() => {
done();
})
.catch((error) => {
done(error);
});
});
test('Connection error will still get a user agent', (done) => {
jest.setTimeout(6000);
const TIMEOUT_URL = 'http://www.google.com:81/';
inferUserAgent('1.6.7', 'darwin', TIMEOUT_URL)
.then((userAgent) => {
expect(userAgent).toBe('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36');
done();
})
.catch(done);
});
});

View File

@ -1,2 +0,0 @@
env:
mocha: true

View File

@ -1,42 +0,0 @@
// need to subtract 2 from source maps
import 'source-map-support/register';
import chai from 'chai';
import fs from 'fs';
import os from 'os';
import path from 'path';
import convertToIcns from './../../lib/helpers/convertToIcns';
const { assert } = chai;
const log = require('loglevel');
// Prerequisite for test: to use OSX with sips, iconutil and imagemagick convert
function testConvertPng(pngName, done) {
convertToIcns(path.join(__dirname, '../../', 'test-resources', pngName), (error, icnsPath) => {
if (error) {
done(error);
return;
}
const stat = fs.statSync(icnsPath);
assert.isTrue(stat.isFile(), 'Output icns file should be a path');
done();
});
}
describe('Get Icon Module', () => {
it('Can convert icons', () => {
if (os.platform() !== 'darwin') {
log.warn('Skipping png conversion tests, OSX is required');
return;
}
it('Can convert a rgb png to icns', (done) => {
testConvertPng('iconSample.png', done);
});
it('Can convert a grey png to icns', (done) => {
testConvertPng('iconSampleGrey.png', done);
});
});
});

View File

@ -1,75 +0,0 @@
import tmp from 'tmp';
import chai from 'chai';
import fs from 'fs';
import path from 'path';
import async from 'async';
import nativefier from './../../lib/index';
const PLATFORMS = ['darwin', 'linux'];
tmp.setGracefulCleanup();
const { assert } = chai;
function checkApp(appPath, inputOptions, callback) {
try {
let relPathToConfig;
switch (inputOptions.platform) {
case 'darwin':
relPathToConfig = path.join('google-test-app.app', 'Contents/Resources/app');
break;
case 'linux':
relPathToConfig = 'resources/app';
break;
case 'win32':
relPathToConfig = 'resources/app';
break;
default:
throw new Error('Unknown app platform');
}
const nativefierConfigPath = path.join(appPath, relPathToConfig, 'nativefier.json');
const nativefierConfig = JSON.parse(fs.readFileSync(nativefierConfigPath));
assert.strictEqual(inputOptions.targetUrl, nativefierConfig.targetUrl, 'Packaged app must have the same targetUrl as the input parameters');
// app name is not consistent for linux
// assert.strictEqual(inputOptions.appName, nativefierConfig.name,
// 'Packaged app must have the same name as the input parameters');
callback();
} catch (exception) {
callback(exception);
}
}
describe('Nativefier Module', function testNativefierModule() {
this.timeout(240000);
it('Can build an app from a target url', (done) => {
async.eachSeries(PLATFORMS, (platform, callback) => {
const tmpObj = tmp.dirSync({ unsafeCleanup: true });
const tmpPath = tmpObj.name;
const options = {
name: 'google-test-app',
targetUrl: 'http://google.com',
out: tmpPath,
overwrite: true,
platform: null,
};
options.platform = platform;
nativefier(options, (error, appPath) => {
if (error) {
callback(error);
return;
}
checkApp(appPath, options, (err) => {
callback(err);
});
});
}, (error) => {
done(error);
});
});
});

View File

@ -1,48 +0,0 @@
import chai from 'chai';
import _ from 'lodash';
import inferUserAgent from './../../lib/infer/inferUserAgent';
const { assert } = chai;
const TEST_RESULT = {
darwin: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36',
mas: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36',
win32: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36',
linux: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36',
};
function testPlatform(platform) {
return inferUserAgent('0.37.1', platform)
.then((userAgent) => {
assert.equal(userAgent, TEST_RESULT[platform], 'Correct user agent should be inferred');
});
}
describe('Infer User Agent', function testInferUserAgent() {
this.timeout(15000);
it('Can infer userAgent for all platforms', (done) => {
const testPromises = _.keys(TEST_RESULT).map(platform => testPlatform(platform));
Promise
.all(testPromises)
.then(() => {
done();
})
.catch((error) => {
done(error);
});
});
it('Connection error will still get a user agent', (done) => {
const TIMEOUT_URL = 'http://www.google.com:81/';
inferUserAgent('1.6.7', 'darwin', TIMEOUT_URL)
.then((userAgent) => {
assert.equal(
userAgent,
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
'Expect default user agent on connection error',
);
done();
})
.catch(done);
});
});