From 949dcfadd836f249670291ecad8683dc2d29a5a1 Mon Sep 17 00:00:00 2001 From: Goh Jia Hao Date: Wed, 23 May 2018 23:34:18 -0700 Subject: [PATCH] Migrate Mocha tests to Jest --- gulp/build.js | 2 +- gulp/test.js | 11 ---- gulp/tests/build-tests.js | 7 --- gulp/tests/mocha.js | 27 ---------- gulp/watch.js | 9 +--- jest.config.js | 4 ++ package.json | 14 +----- src/helpers/convertToIcns.test.js | 39 +++++++++++++++ src/index.test.js | 83 +++++++++++++++++++++++++++++++ src/infer/inferUserAgent.test.js | 44 ++++++++++++++++ test/module/.eslintrc.yml | 2 - test/module/getIconSpec.js | 42 ---------------- test/module/indexSpec.js | 75 ---------------------------- test/module/inferUserAgentSpec.js | 48 ------------------ 14 files changed, 175 insertions(+), 232 deletions(-) delete mode 100644 gulp/test.js delete mode 100644 gulp/tests/build-tests.js delete mode 100644 gulp/tests/mocha.js create mode 100644 jest.config.js create mode 100644 src/helpers/convertToIcns.test.js create mode 100644 src/index.test.js create mode 100644 src/infer/inferUserAgent.test.js delete mode 100644 test/module/.eslintrc.yml delete mode 100644 test/module/getIconSpec.js delete mode 100644 test/module/indexSpec.js delete mode 100644 test/module/inferUserAgentSpec.js diff --git a/gulp/build.js b/gulp/build.js index 7b832fb..74bad1f 100644 --- a/gulp/build.js +++ b/gulp/build.js @@ -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) => { diff --git a/gulp/test.js b/gulp/test.js deleted file mode 100644 index e52f50a..0000000 --- a/gulp/test.js +++ /dev/null @@ -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)); diff --git a/gulp/tests/build-tests.js b/gulp/tests/build-tests.js deleted file mode 100644 index 98786e8..0000000 --- a/gulp/tests/build-tests.js +++ /dev/null @@ -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)); diff --git a/gulp/tests/mocha.js b/gulp/tests/mocha.js deleted file mode 100644 index 37cc65c..0000000 --- a/gulp/tests/mocha.js +++ /dev/null @@ -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'])); diff --git a/gulp/watch.js b/gulp/watch.js index 7350cf7..df96118 100644 --- a/gulp/watch.js +++ b/gulp/watch.js @@ -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); }); diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..92d109f --- /dev/null +++ b/jest.config.js @@ -0,0 +1,4 @@ +module.exports = { + testMatch: ['**/src/**/?(*.)(test).js?(x)'], + testEnvironment: 'node', +}; diff --git a/package.json b/package.json index 36304ca..5433eba 100644 --- a/package.json +++ b/package.json @@ -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)" - ] } } diff --git a/src/helpers/convertToIcns.test.js b/src/helpers/convertToIcns.test.js new file mode 100644 index 0000000..5f3e664 --- /dev/null +++ b/src/helpers/convertToIcns.test.js @@ -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); + }); +}); diff --git a/src/index.test.js b/src/index.test.js new file mode 100644 index 0000000..3b26b27 --- /dev/null +++ b/src/index.test.js @@ -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); + }, + ); + }); +}); diff --git a/src/infer/inferUserAgent.test.js b/src/infer/inferUserAgent.test.js new file mode 100644 index 0000000..c847612 --- /dev/null +++ b/src/infer/inferUserAgent.test.js @@ -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); + }); +}); diff --git a/test/module/.eslintrc.yml b/test/module/.eslintrc.yml deleted file mode 100644 index 9808c3b..0000000 --- a/test/module/.eslintrc.yml +++ /dev/null @@ -1,2 +0,0 @@ -env: - mocha: true diff --git a/test/module/getIconSpec.js b/test/module/getIconSpec.js deleted file mode 100644 index effa240..0000000 --- a/test/module/getIconSpec.js +++ /dev/null @@ -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); - }); - }); -}); diff --git a/test/module/indexSpec.js b/test/module/indexSpec.js deleted file mode 100644 index 6ad3bd3..0000000 --- a/test/module/indexSpec.js +++ /dev/null @@ -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); - }); - }); -}); - diff --git a/test/module/inferUserAgentSpec.js b/test/module/inferUserAgentSpec.js deleted file mode 100644 index 3b7d4a0..0000000 --- a/test/module/inferUserAgentSpec.js +++ /dev/null @@ -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); - }); -});