From a43e1bb6eac69696f637c118edc87aa3eed995dc Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 11 Feb 2017 00:45:27 +0100 Subject: [PATCH] [WIP] Allow setting a test environment per file --- packages/jest-cli/src/runTest.js | 127 +++++++++++++++++------------- packages/jest-config/src/index.js | 2 + 2 files changed, 74 insertions(+), 55 deletions(-) diff --git a/packages/jest-cli/src/runTest.js b/packages/jest-cli/src/runTest.js index 19027e4f679a..aa5fb5a60004 100644 --- a/packages/jest-cli/src/runTest.js +++ b/packages/jest-cli/src/runTest.js @@ -13,7 +13,10 @@ import type {Path, Config} from 'types/Config'; import type {TestResult} from 'types/TestResult'; import type {Resolver} from 'types/Resolve'; +const fs = require('fs'); +const {getTestEnvironment} = require('jest-config'); const BufferedConsole = require('./lib/BufferedConsole'); +const promisify = require('./lib/promisify'); const { Console, NullConsole, @@ -21,66 +24,80 @@ const { } = require('jest-util'); const getConsoleOutput = require('./reporters/getConsoleOutput'); +const testEnvironmenPragmaRegex = /@testEnvironment\s+(.*)/; + function runTest(path: Path, config: Config, resolver: Resolver) { - /* $FlowFixMe */ - const TestEnvironment = require(config.testEnvironment); - /* $FlowFixMe */ - const TestRunner = require(config.testRunner); - /* $FlowFixMe */ - const ModuleLoader = require(config.moduleLoader || 'jest-runtime'); + return promisify(fs.readFile)(path, 'utf8').then(data => { + let testEnvironment = config.testEnvironment; + + const testEnvironmentPragma = data.match(testEnvironmenPragmaRegex); - const env = new TestEnvironment(config); - const TestConsole = - config.verbose - ? Console - : (config.silent - ? NullConsole - : BufferedConsole + if (testEnvironmentPragma && testEnvironmentPragma.length > 1) { + testEnvironment = getTestEnvironment( + Object.assign({}, config, {testEnvironment: testEnvironmentPragma[1]}) ); - const testConsole = new TestConsole( - config.useStderr ? process.stderr : process.stdout, - process.stderr, - (type, message) => getConsoleOutput( - config.rootDir, - !!config.verbose, - // 4 = the console call is burried 4 stack frames deep - BufferedConsole.write([], type, message, 4), - ), - ); - setGlobal(env.global, 'console', testConsole); - const runtime = new ModuleLoader(config, env, resolver); - const start = Date.now(); - return TestRunner(config, env, runtime, path) - .then((result: TestResult) => { - const testCount = - result.numPassingTests + - result.numFailingTests + - result.numPendingTests; - result.perfStats = {end: Date.now(), start}; - result.testFilePath = path; - result.coverage = runtime.getAllCoverageInfo(); - result.console = testConsole.getBuffer(); - result.skipped = testCount === result.numPendingTests; - return result; - }) - .then( - result => Promise.resolve().then(() => { - env.dispose(); - if (config.logHeapUsage) { - if (global.gc) { - global.gc(); - } - result.memoryUsage = process.memoryUsage().heapUsed; - } + } + + /* $FlowFixMe */ + const TestEnvironment = require(testEnvironment); + /* $FlowFixMe */ + const TestRunner = require(config.testRunner); + /* $FlowFixMe */ + const ModuleLoader = require(config.moduleLoader || 'jest-runtime'); - // Delay the resolution to allow log messages to be output. - return new Promise(resolve => setImmediate(() => resolve(result))); - }), - err => Promise.resolve().then(() => { - env.dispose(); - throw err; - }), + const env = new TestEnvironment(config); + const TestConsole = + config.verbose + ? Console + : (config.silent + ? NullConsole + : BufferedConsole + ); + const testConsole = new TestConsole( + config.useStderr ? process.stderr : process.stdout, + process.stderr, + (type, message) => getConsoleOutput( + config.rootDir, + !!config.verbose, + // 4 = the console call is buried 4 stack frames deep + BufferedConsole.write([], type, message, 4), + ), ); + setGlobal(env.global, 'console', testConsole); + const runtime = new ModuleLoader(config, env, resolver); + const start = Date.now(); + return TestRunner(config, env, runtime, path) + .then((result: TestResult) => { + const testCount = + result.numPassingTests + + result.numFailingTests + + result.numPendingTests; + result.perfStats = {end: Date.now(), start}; + result.testFilePath = path; + result.coverage = runtime.getAllCoverageInfo(); + result.console = testConsole.getBuffer(); + result.skipped = testCount === result.numPendingTests; + return result; + }) + .then( + result => Promise.resolve().then(() => { + env.dispose(); + if (config.logHeapUsage) { + if (global.gc) { + global.gc(); + } + result.memoryUsage = process.memoryUsage().heapUsed; + } + + // Delay the resolution to allow log messages to be output. + return new Promise(resolve => setImmediate(() => resolve(result))); + }), + err => Promise.resolve().then(() => { + env.dispose(); + throw err; + }), + ); + }); } module.exports = runTest; diff --git a/packages/jest-config/src/index.js b/packages/jest-config/src/index.js index 624847bfe4fd..6f525094857e 100644 --- a/packages/jest-config/src/index.js +++ b/packages/jest-config/src/index.js @@ -15,6 +15,7 @@ const loadFromFile = require('./loadFromFile'); const loadFromPackage = require('./loadFromPackage'); const normalize = require('./normalize'); const setFromArgv = require('./setFromArgv'); +const {getTestEnvironment} = require('./utils'); const readConfig = (argv: Object, packageRoot: string) => readRawConfig(argv, packageRoot) @@ -48,6 +49,7 @@ const readRawConfig = (argv, root) => { }; module.exports = { + getTestEnvironment, normalize, readConfig, };