Skip to content

Commit

Permalink
[WIP] Allow setting a test environment per file
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Feb 10, 2017
1 parent 7ca7dac commit a43e1bb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 55 deletions.
127 changes: 72 additions & 55 deletions packages/jest-cli/src/runTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,74 +13,91 @@ 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,
setGlobal,
} = 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;
2 changes: 2 additions & 0 deletions packages/jest-config/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -48,6 +49,7 @@ const readRawConfig = (argv, root) => {
};

module.exports = {
getTestEnvironment,
normalize,
readConfig,
};

0 comments on commit a43e1bb

Please sign in to comment.