-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature/add-first-class-debugging-support-for-tests #1360
Changes from all commits
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#!/usr/bin/env node | ||
var spawn = require('cross-spawn'); | ||
var getDebugFlag = require('../utils/getDebugFlag'); | ||
var script = process.argv[2]; | ||
var args = process.argv.slice(3); | ||
|
||
|
@@ -8,9 +9,14 @@ case 'build': | |
case 'eject': | ||
case 'start': | ||
case 'test': | ||
var scriptArgs = [require.resolve('../scripts/' + script)].concat(args); | ||
var debugFlag = getDebugFlag(args); | ||
if (debugFlag) { | ||
scriptArgs.unshift(debugFlag); | ||
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. note, running |
||
} | ||
var result = spawn.sync( | ||
'node', | ||
[require.resolve('../scripts/' + script)].concat(args), | ||
scriptArgs, | ||
{stdio: 'inherit'} | ||
); | ||
process.exit(result.status); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const babelJestDefaultConfig = require('./babelJestDefaultConfig'); | ||
const assign = require('object-assign'); | ||
module.exports = assign(babelJestDefaultConfig, { sourceMaps: true }); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
presets: [require.resolve('babel-preset-react-app')], | ||
babelrc: false | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,8 +7,5 @@ | |
*/ | ||
|
||
const babelJest = require('babel-jest'); | ||
|
||
module.exports = babelJest.createTransformer({ | ||
presets: [require.resolve('babel-preset-react-app')], | ||
babelrc: false | ||
}); | ||
const babelJestDebugConfig = require('./babelJestDebugConfig'); | ||
module.exports = babelJest.createTransformer(babelJestDebugConfig); | ||
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. this was probably supposed to be |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
const babelJest = require('babel-jest'); | ||
const babelJestDebugConfig = require('./babelJestDebugConfig'); | ||
module.exports = babelJest.createTransformer(babelJestDebugConfig); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,11 @@ | |
*/ | ||
// @remove-on-eject-end | ||
|
||
/** | ||
* Greetings! If you are here attempting to start a debugging session, please | ||
* ensure that your debugger of choice is configured to enable source maps, | ||
* otherwise your code may appear mangled by babel! | ||
*/ | ||
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. 🎉 greet the debugging user with some 🌶 🔥 tips |
||
process.env.NODE_ENV = 'test'; | ||
process.env.PUBLIC_URL = ''; | ||
|
||
|
@@ -20,21 +25,32 @@ require('dotenv').config({silent: true}); | |
|
||
const jest = require('jest'); | ||
const argv = process.argv.slice(2); | ||
const debugFlag = require('../utils/getDebugFlag')(argv); | ||
const isDebug = !!debugFlag; | ||
const isRunInBand = argv.indexOf('--runInBand') > -1 || argv.indexOf('-i') > -1 | ||
|
||
// Watch unless on CI or in coverage mode | ||
if (!process.env.CI && argv.indexOf('--coverage') < 0) { | ||
if (!process.env.CI && argv.indexOf('--coverage') < 0 && !isDebug) { | ||
argv.push('--watch'); | ||
} | ||
|
||
// Force debug into single worker | ||
if (isDebug) { | ||
if (!isRunInBand) { | ||
argv.push('--runInBand') | ||
} | ||
argv.splice(argv.indexOf(debugFlag), 1) | ||
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. any of the node debug flags are not valid input to |
||
} | ||
|
||
// @remove-on-eject-begin | ||
// This is not necessary after eject because we embed config into package.json. | ||
const createJestConfig = require('../utils/createJestConfig'); | ||
const path = require('path'); | ||
const paths = require('../config/paths'); | ||
argv.push('--config', JSON.stringify(createJestConfig( | ||
relativePath => path.resolve(__dirname, '..', relativePath), | ||
path.resolve(paths.appSrc, '..'), | ||
false | ||
))); | ||
argv.push('--config', JSON.stringify(createJestConfig({ | ||
resolve: relativePath => path.resolve(__dirname, '..', relativePath), | ||
rootDir: path.resolve(paths.appSrc, '..'), | ||
isDebug: isDebug | ||
}))); | ||
// @remove-on-eject-end | ||
jest.run(argv); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,11 +12,18 @@ | |
const fs = require('fs'); | ||
const paths = require('../config/paths'); | ||
|
||
module.exports = (resolve, rootDir, isEjecting) => { | ||
module.exports = (opts) => { | ||
const resolve = opts.resolve; | ||
const rootDir = opts.rootDir || null; | ||
const isEjecting = opts.isEjecting || false; | ||
const isDebug = opts.isDebug || false; | ||
|
||
// Use this instead of `paths.testsSetup` to avoid putting | ||
// an absolute filename into configuration after ejecting. | ||
const setupTestsFile = fs.existsSync(paths.testsSetup) ? '<rootDir>/src/setupTests.js' : undefined; | ||
|
||
const babelTransform = isEjecting | ||
? '<rootDir>/node_modules/babel-jest' | ||
: resolve('config/jest/babelTransform' + (isDebug ? 'Debug' : '') + '.js') | ||
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. @Timer, 👀, this is where/why there were two babelTransform files. we are passing the babel config as a file path, therefore, to enable sourceMaps, i added a different file. if there's another way to get those babel settings passed to jest, we could pursue that. we could also set something in the ENV to drop it down to one transform file? 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. Ok I hadn't reviewed deep enough to have this understanding. I'll snoop around and see what our best option(s) are. 😄 |
||
// TODO: I don't know if it's safe or not to just use / as path separator | ||
// in Jest configs. We need help from somebody with Windows to determine this. | ||
const config = { | ||
|
@@ -29,9 +36,7 @@ module.exports = (resolve, rootDir, isEjecting) => { | |
testEnvironment: 'node', | ||
testURL: 'http://localhost', | ||
transform: { | ||
'^.+\\.(js|jsx)$': isEjecting ? | ||
'<rootDir>/node_modules/babel-jest' | ||
: resolve('config/jest/babelTransform.js'), | ||
'^.+\\.(js|jsx)$': babelTransform, | ||
'^.+\\.css$': resolve('config/jest/cssTransform.js'), | ||
'^(?!.*\\.(js|jsx|css|json)$)': resolve('config/jest/fileTransform.js'), | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
const DEBUG_FLAGS = [ 'debug', '--debug-brk', '--inspect' ]; | ||
|
||
module.exports = function getDebugFlag(argv) { | ||
for (var i in argv) { | ||
if (argv.hasOwnProperty(i)) { | ||
for (var j in DEBUG_FLAGS) { | ||
if (DEBUG_FLAGS.hasOwnProperty(j)) { | ||
if (argv[i] === DEBUG_FLAGS[j]) { | ||
return DEBUG_FLAGS[j]; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return null; | ||
} |
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.
For this to work after the eject, you'll probably have to add the right path here.