-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Setup before tests but after framework loads #7119
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
Changes from 8 commits
0691d94
a93d759
4e5b902
4910e56
07b6cba
51fe135
c9367f8
af4ac93
1177706
f532ef1
4801f37
d419af3
4c67fa3
ebb14c1
db2ce7e
7b322df
6ba2beb
6053897
cd8e2ae
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,60 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
'use strict'; | ||
|
||
import path from 'path'; | ||
import {json as runWithJson} from '../runJest'; | ||
import {writeFiles} from '../Utils'; | ||
|
||
const DIR = path.resolve(__dirname, '../setup-tests-after-jest-config'); | ||
|
||
describe('setupTestsAfterJest', () => { | ||
it('requires multiple setup files before each file in the suite', () => { | ||
const pkgJson = { | ||
jest: { | ||
setupTestsAfterJest: ['./setup1.js', './setup2.js'], | ||
}, | ||
}; | ||
|
||
writeFiles(DIR, { | ||
'package.json': JSON.stringify(pkgJson, null, 2), | ||
}); | ||
|
||
const result = runWithJson('setup-tests-after-jest-config', [ | ||
'test1.test.js', | ||
'test2.test.js', | ||
]); | ||
|
||
expect(result.json.numTotalTests).toBe(2); | ||
expect(result.json.numPassedTests).toBe(2); | ||
expect(result.json.testResults.length).toBe(2); | ||
expect(result.status).toBe(0); | ||
}); | ||
|
||
it('requires setup files *after* the test runners are required', () => { | ||
const pkgJson = { | ||
jest: { | ||
setupTestsAfterJest: ['./setup_hooks_into_runner.js'], | ||
}, | ||
}; | ||
|
||
writeFiles(DIR, { | ||
'package.json': JSON.stringify(pkgJson, null, 2), | ||
}); | ||
|
||
const result = runWithJson('setup-tests-after-jest-config', [ | ||
'runner_patch.test.js', | ||
]); | ||
|
||
expect(result.json.numTotalTests).toBe(1); | ||
expect(result.json.numPassedTests).toBe(1); | ||
expect(result.json.testResults.length).toBe(1); | ||
expect(result.status).toBe(0); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
'use strict'; | ||
|
||
describe('setupFile', () => { | ||
it('patches jasmine in setup file', () => { | ||
expect(global.describeDefined).toBe(true); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
'use strict'; | ||
|
||
describe('test', () => { | ||
it('has predefined global variable', () => { | ||
expect(global.definedInSetupFile).toEqual(true); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
'use strict'; | ||
|
||
describe('test', () => { | ||
it('has predefined global variable', () => { | ||
expect(global.definedInSetupFile).toEqual(true); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"jest": { | ||
"setupTestsAfterJest": [ | ||
"./setup_hooks_into_runner.js" | ||
] | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
global.definedInSetupFile = true; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
global.definedInSetupFile = true; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
global.describeDefined = !!global.describe; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,12 @@ const jestAdapter = async ( | |
runtime.requireModule(config.setupTestFrameworkScriptFile); | ||
} | ||
|
||
if (config.setupTestsAfterJest.length) { | ||
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 code will run setup files after any file set in
Contributor
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. Hmm.. you bring up a good point! I lean more towards option 2 since both serve the same purpose. I can't think of a case where I would have both set in my configuration. With that, another idea I'd like to suggest is that if both are set, we could log a warning indicating that 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. Just curious, how's the behaviour like if you pass in a wrong path to a file? Is that error handled or is that handled by the runtime. It would be good to gracefully handle that scenario 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 about changing line 77 to: config.setupTestsAfterJest.push(config.setupTestFrameworkScriptFile); 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. @rickhanlonii Awesome. We would need to include info in the docs that mentions the module set using 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. Yeah agreed, can probably add it to the doocs here https://jestjs.io/docs/en/configuration#setuptestframeworkscriptfile-string What's the original implementation? Do you mean just line 77? |
||
for (let i = 0; i < config.setupTestsAfterJest.length; i++) { | ||
runtime.requireModule(config.setupTestsAfterJest[i]); | ||
} | ||
} | ||
|
||
runtime.requireModule(testPath); | ||
const results = await runAndTransformResultsToJestFormat({ | ||
config, | ||
|
Uh oh!
There was an error while loading. Please reload this page.