-
Notifications
You must be signed in to change notification settings - Fork 37
Add jest as a test runner #67
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* eslint-env node */ | ||
|
||
module.exports = { | ||
cache: true, | ||
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))'], | ||
}; |
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'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* eslint-env node */ | ||
global.requestAnimationFrame = callback => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this for? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Required for running react tests in node - they ask you to bring your own polyfills. |
||
setTimeout(callback, 0); | ||
}; |
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', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the es2015 preset required given that we require Node 8+? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll try getting rid of it. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Turns out this is required for modules to work. Might be able to do this with a smaller plugin, but since it's only for tests, I don't think it's too bad. |
||
'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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* eslint-env node */ | ||
const path = require('path'); | ||
const {spawn} = require('child_process'); | ||
|
||
module.exports.TestAppRuntime = function({dir = '.', watch = false, match}) { | ||
const state = {proc: null}; | ||
|
||
this.run = () => { | ||
this.stop(); | ||
|
||
let command = require.resolve('jest-cli/bin/jest.js'); | ||
let args = [ | ||
// '--no-cache', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm guessing this should be deleted? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can, though it's really useful for debugging. |
||
'--config', | ||
'./node_modules/fusion-cli/build/jest-config.js', | ||
]; | ||
|
||
if (watch) { | ||
args.push('--watch'); | ||
} | ||
|
||
if (match && match.length > 0) { | ||
args.push(match); | ||
} | ||
|
||
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; | ||
} | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* 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-run tests on file changes', | ||
}, | ||
match: { | ||
type: 'string', | ||
default: null, | ||
describe: 'Runs test files that match a given string', | ||
}, | ||
}; | ||
|
||
exports.run = async function({dir = '.', watch, match}) { | ||
const testRuntime = new TestAppRuntime({dir, watch, match}); | ||
|
||
await testRuntime.run(); | ||
|
||
return { | ||
stop() { | ||
testRuntime.stop(); | ||
}, | ||
}; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if people don't use enzyme in their tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's necessary, but I think we should probably enable by default? We can add a config to turn this off though.