diff --git a/package.json b/package.json index b5790e22c0c8..2abe0e74e06c 100644 --- a/package.json +++ b/package.json @@ -127,7 +127,9 @@ "transform": { "^.+\\.js$": "/packages/babel-jest" }, - "setupTestFrameworkScriptFile": "/test_setup_file.js", + "setupTestFramework": [ + "/test_setup_file.js" + ], "snapshotSerializers": [ "/packages/pretty-format/build/plugins/convert_ansi.js" ], diff --git a/packages/jest-cli/src/cli/args.js b/packages/jest-cli/src/cli/args.js index c312674a2d9e..2620f050368f 100644 --- a/packages/jest-cli/src/cli/args.js +++ b/packages/jest-cli/src/cli/args.js @@ -426,7 +426,13 @@ export const options = { setupFiles: { description: 'The paths to modules that run some code to configure or ' + - 'set up the testing environment before each test. ', + 'set up the testing environment before each test.', + type: 'array', + }, + setupTestFramework: { + description: + 'Allows to setup the Jasmine environment from the global namespace, ' + + 'instead of injecting the module inside the context', type: 'array', }, setupTestFrameworkScriptFile: { diff --git a/packages/jest-config/src/index.js b/packages/jest-config/src/index.js index d070c3789173..4305850c62bb 100644 --- a/packages/jest-config/src/index.js +++ b/packages/jest-config/src/index.js @@ -142,6 +142,7 @@ const getConfigs = ( roots: options.roots, runner: options.runner, setupFiles: options.setupFiles, + setupTestFramework: options.setupTestFramework, setupTestFrameworkScriptFile: options.setupTestFrameworkScriptFile, skipNodeResolution: options.skipNodeResolution, snapshotSerializers: options.snapshotSerializers, diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index 412289e19071..472c983f4ac2 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -359,6 +359,7 @@ export default function normalize(options: InitialOptions, argv: Argv) { value = normalizeCollectCoverageOnlyFrom(options, key); break; case 'setupFiles': + case 'setupTestFramework': case 'snapshotSerializers': value = options[key] && diff --git a/packages/jest-config/src/valid_config.js b/packages/jest-config/src/valid_config.js index 5c6bf89f166d..c1c5fa9a9707 100644 --- a/packages/jest-config/src/valid_config.js +++ b/packages/jest-config/src/valid_config.js @@ -72,7 +72,7 @@ export default ({ runTestsByPath: false, runner: 'jest-runner', setupFiles: ['/setup.js'], - setupTestFrameworkScriptFile: '/test_setup_file.js', + setupTestFramework: ['/test_setup_file.js'], silent: true, skipNodeResolution: false, snapshotSerializers: ['my-serializer-module'], diff --git a/packages/jest-jasmine2/src/index.js b/packages/jest-jasmine2/src/index.js index 8000f0c5710c..724d0e03e921 100644 --- a/packages/jest-jasmine2/src/index.js +++ b/packages/jest-jasmine2/src/index.js @@ -112,6 +112,12 @@ async function jasmine2( runtime.requireModule(config.setupTestFrameworkScriptFile); } + if (config.setupTestFramework && config.setupTestFramework.length) { + config.setupTestFramework.forEach(module => { + require(module)(environment.global); + }); + } + runtime .requireModule(require.resolve('source-map-support'), 'source-map-support') .install({ diff --git a/test_setup_file.js b/test_setup_file.js index b6c5c3b99e16..e6613fb969a1 100644 --- a/test_setup_file.js +++ b/test_setup_file.js @@ -7,11 +7,17 @@ const jasmineReporters = require('jasmine-reporters'); -// Some of the `jest-runtime` tests are very slow and cause -// timeouts on travis -jest.setTimeout(70000); +module.exports = function(global) { + const {jasmine} = global; -if (global.jasmine && process.env.APPVEYOR_API_URL) { - // Running on AppVeyor, add the custom reporter. - jasmine.getEnv().addReporter(new jasmineReporters.AppVeyorReporter()); -} + if (jasmine) { + // Some of the `jest-runtime` tests are very slow and cause timeouts on + // Travis CI. + jasmine.DEFAULT_TIMEOUT_INTERVAL = 70000; + + // Running on AppVeyor, add the custom reporter. + if (process.env.APPVEYOR_API_URL) { + jasmine.getEnv().addReporter(new jasmineReporters.AppVeyorReporter()); + } + } +}; diff --git a/test_utils.js b/test_utils.js index d3bf95950d99..3fa55abc6271 100644 --- a/test_utils.js +++ b/test_utils.js @@ -84,6 +84,7 @@ const DEFAULT_PROJECT_CONFIG: ProjectConfig = { roots: [], runner: 'jest-runner', setupFiles: [], + setupTestFramework: [], setupTestFrameworkScriptFile: null, skipNodeResolution: false, snapshotSerializers: [], diff --git a/types/Argv.js b/types/Argv.js index 7125a0f75ce9..f9542351aafe 100644 --- a/types/Argv.js +++ b/types/Argv.js @@ -61,6 +61,7 @@ export type Argv = {| rootDir: string, roots: Array, setupFiles: Array, + setupTestFramework: Array, setupTestFrameworkScriptFile: string, silent: boolean, snapshotSerializers: Array, diff --git a/types/Config.js b/types/Config.js index 585cd27ee8ce..9dfa137b7ce4 100644 --- a/types/Config.js +++ b/types/Config.js @@ -116,6 +116,7 @@ export type InitialOptions = { runTestsByPath?: boolean, scriptPreprocessor?: string, setupFiles?: Array, + setupTestFramework?: Array, setupTestFrameworkScriptFile?: Path, silent?: boolean, skipNodeResolution?: boolean, @@ -218,6 +219,7 @@ export type ProjectConfig = {| roots: Array, runner: string, setupFiles: Array, + setupTestFramework: Array, setupTestFrameworkScriptFile: ?Path, skipNodeResolution: boolean, snapshotSerializers: Array,