Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Commit

Permalink
Implement jest as a test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinGrandon committed Dec 12, 2017
1 parent de34ba7 commit 31fcf24
Show file tree
Hide file tree
Showing 8 changed files with 1,059 additions and 49 deletions.
14 changes: 14 additions & 0 deletions build/jest-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* eslint-env node */

module.exports = {
cache: false,
rootDir: process.cwd(),
setupFiles: [
'<rootDir>/node_modules/fusion-cli/build/jest-framework-shims.js',
'<rootDir>/node_modules/fusion-cli/build/jest-framework-setup.js',
],
transform: {
'^.+\\.js$': '<rootDir>/node_modules/fusion-cli/build/jest-transformer.js',
},
transformIgnorePatterns: ['/node_modules/(?!(fusion-cli.*build))'],
};
10 changes: 10 additions & 0 deletions build/jest-framework-setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* eslint-env node */
import {configure} from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

// Setup Enzyme for all Jest tests
configure({adapter: new Adapter()});

const testEnv = process.env.ENV || 'browser';
global.__NODE__ = testEnv === 'node';
global.__BROWSER__ = testEnv === 'browser';
4 changes: 4 additions & 0 deletions build/jest-framework-shims.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* eslint-env node */
global.requestAnimationFrame = callback => {
setTimeout(callback, 0);
};
23 changes: 23 additions & 0 deletions build/jest-transformer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* eslint-env node */

const transformer = require('babel-jest').createTransformer({
presets: [
'babel-preset-flow',
'babel-preset-es2015',
'babel-preset-react',
].map(require.resolve),
});

const originalProcessFn = transformer.process;

transformer.process = function(src, filename, config, transformOptions) {
return originalProcessFn.call(
transformer,
src,
filename,
config,
transformOptions
);
};

module.exports = transformer;
46 changes: 46 additions & 0 deletions build/test-app-runtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* eslint-env node */
const path = require('path');
const {spawn} = require('child_process');

module.exports.TestAppRuntime = function({dir = '.'}) {
const state = {proc: null};

this.run = () => {
this.stop();

let command = require.resolve('jest-cli/bin/jest.js');
let args = [
'--no-cache',
'--config',
'./node_modules/fusion-cli/build/jest-config.js',
];

return new Promise((resolve, reject) => {
state.proc = spawn(command, args, {
cwd: path.resolve(process.cwd(), dir),
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
env: Object.assign({}, process.env),
});

state.proc.on('error', reject);
state.proc.on('exit', (code, signal) => {
if (code) {
return reject(new Error(`Test exited with code ${code}`));
}

if (signal) {
return reject(new Error(`Test process exited with signal ${signal}`));
}

return resolve();
});
});
};

this.stop = () => {
if (state.proc) {
state.proc.kill();
state.proc = null;
}
};
};
28 changes: 28 additions & 0 deletions commands/test-app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-env node */
const {TestAppRuntime} = require('../build/test-app-runtime');

exports.desc = 'Run browser tests, using Jest';
exports.builder = {
dir: {
type: 'string',
default: '.',
describe: 'Root path for the application relative to CLI CWD',
},
watch: {
type: 'boolean',
default: false,
describe: 'Automatically re-profile your application on changes',
},
};

exports.run = async function({dir = '.', watch}) {
const testRuntime = new TestAppRuntime({dir, watch});

await testRuntime.run();

return {
stop() {
testRuntime.stop();
},
};
};
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,30 @@
},
"dependencies": {
"@nadiia/file-loader": "^1.0.0-beta.5",
"babel-core": "^6.22.1",
"babel-core": "^6.26.0",
"babel-jest": "^21.2.0",
"babel-loader": "^7.1.0",
"babel-plugin-external-helpers": "^6.22.0",
"babel-plugin-istanbul": "^4.0.0",
"babel-plugin-istanbul": "^4.1.5",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-plugin-transform-async-generator-functions": "^6.22.0",
"babel-plugin-transform-class-properties": "^6.23.0",
"babel-plugin-transform-cup-globals": "^1.0.0-rc.2",
"babel-plugin-transform-object-rest-spread": "^6.23.0",
"babel-plugin-transform-react-jsx": "^6.23.0",
"babel-preset-env": "^1.1.8",
"babel-preset-es2015": "^6.24.1",
"babel-preset-flow": "^6.23.0",
"babel-preset-jest": "^21.2.0",
"babel-preset-react": "^6.23.0",
"browser-unhandled-rejection": "^1.0.1",
"case-sensitive-paths-webpack-plugin": "^1.1.4",
"chalk": "^1.1.3",
"compose-middleware": "3.0.0",
"compression-webpack-plugin": "^0.4.0",
"core-js": "^2.5.1",
"enzyme": "^3.2.0",
"enzyme-adapter-react-16": "^1.1.0",
"es6-object-assign": "^1.1.0",
"express": "^4.15.2",
"fast-async": "^6.3.0",
Expand All @@ -39,6 +44,8 @@
"iltorb": "^1.3.1",
"imagemin": "^5.3.1",
"imagemin-svgo": "^5.2.2",
"jest": "^21.2.1",
"jest-cli": "^21.2.1",
"just-snake-case": "^1.0.0",
"koa": "^2.3.0",
"koa-mount": "^3.0.0",
Expand Down
Loading

0 comments on commit 31fcf24

Please sign in to comment.