-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Make "globalSetup" and "globalTeardown" work together with "transform" #5164
Comments
I'm not sure we can do this, since the whole point of @cpojer thoughts on this? |
I think we should transform it if we can, but I’m not sure right now what code changes that would entail. It may make sense to split the transformer into a separate package, something I wanted to do for a while. |
Is there a workaround here? I'd like to be able to load some support code in globalSetup that uses ES6 modules and I'd prefer to not have to rewrite all of it back to requires/exports. |
No current workaround that I know of, sorry. PR welcome here, though 🙂 |
@glasser I'm currently working around this issue by using yarn add babel-register babel-pollyfil const { readFileSync } = require('fs')
const babelConfig = JSON.parse(readFileSync('./.babelrc', 'utf8'))
require('babel-register')(babelConfig)
require('babel-polyfill')
const { join } = require('path')
const ROOT = process.cwd()
// const TESTS = join(ROOT, '__tests__')
const JEST_ENV = join(ROOT, '__jest_env__')
module.exports = {
verbose: true,
transform: {
'^.+\\.jsx?$': 'babel-jest'
},
globalSetup: join(JEST_ENV, 'setup.js'),
globalTeardown: join(JEST_ENV, 'teardown.js'),
testEnvironment: join(JEST_ENV, 'server-environment.js')
} |
@opasno You seem to be using typescript, so try this method. In my case, I could easily solve the problem. I do not know if this method is useful in all cases, but I did the following things.
code will be like this: require("ts-node/register");
const {db} = require("../db");
module.exports = async function () {
return db.setup();
}; Note that entire setup/teardown codes will be executed in non-jest environment, so all modules and objects related to Jest can not be referred while setup/teardown. However, It is enough to do setup/teardown. |
Is anybody working on a PR for this already? Just encountered this issue and took me awhile to realise what was happening and track down this issue. At the very least, maybe the docs could be updated to indicate that transforms will not take place on |
If anyone runs into this, you can trivially work around by performing these two steps.
This assumes that you have "ts-node": "^5.0.1" in your package.json. No changes required in jest or ts-jest for this to work. |
If anyone is working on a PR for this, keep in mind that testEnvironment files are not transpiled either. |
Is this the same with setupTestFrameworkScriptFile? It is not transpiled? |
@tachang Most likely. It would be extremely easy to test, why don't you do that yourself? |
@WillAvudim your solution works greatly :) I have shortened it to a two-liner file: setup_hook.js: require("ts-node/register");
module.exports = require('./setup').default; This works, because I have the setup file declared with default export function: setup.ts: import { anything } from 'you-want';
export default async function() {
// do all the setup you want in TypeScript
} I hope someone finds this useful. |
@tachang I'm still not sure how to solve this. One thing is to transpile the setup file itself (that can be done pretty easily), but to also transpile files required from it forces us to implement Anybody wanna create a PoC using https://www.npmjs.com/package/pirates? |
We could also print a pretty error if we get a syntax error saying we don't transform. That's probably better than what we have now |
Should we always bail if there's a syntax error in the global setup, or should we respect bail flag in the config? |
would love to see this. I am using both babel and typescript, and not being able to use es6 for some global setup in test files in a centralized manner kind of sucks. is another workaround just importing the function in the tests you need as well? not as convenient but it would be transpiled right? |
@SimenB What is the approach we decided to go with? |
I amended @WillAvudim example to support tsconfig-paths
|
I don't use typescript, is there another solution? |
@shai32, you can do the same with require('@babel/register');
require('@babel/polyfill');
module.exports = require('./setup').default; |
This is available in Jest 24 |
Are https://jestjs.io/docs/en/configuration.html#modulenamemapper-object-string-string concidered if using global setup with typescript |
No, jest does not control the resolution algorithm in this case |
Small typo here on your yarn commands.
|
Based on @AlvSovereign and @sourcec0de work, adding const { readFileSync } = require('fs');
const babelConfig = require('./babel.config.js');
require('@babel/register')(babelConfig); in your jest.config.js worked for me |
No need for workarounds, the feature has been implemented |
You said it was implemented in jest 24, but we are using jest 24.8.0 and if I don't do that, it does not work. Am i missing something? Also, in the documentation, this is still present:
|
Transforming EDIT: We also transform everything not ignore via |
Thanks. I was not able to make it work using |
Variable added to global in global{Setup,Teardown} cannot be used in test cases?
|
@vifird that is correct, each test runs in its own sandboxed environment. |
@jeysal So is there a way to add global settings? except "globals" field in jest's configuration. |
Correct. We might add a way to send variables from |
@SimenB i have an error on my terminal without any extra information
Did i missing something ? below is my jest.config.js
|
using Jest 24.9 and had to |
I can confirm this is not working in 24.9. As a matter of fact, it only works up to 24.1 for me. Related: #9041 |
Please open up new issues (with reproductions) if you have a regression to report - this old issue is not the place for it |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Do you want to request a feature or report a bug?
Feature
What is the current behavior?
Modules defined in
globalSetup
andglobalTeardown
are not being transformed as defined intransform
configuration entry.What is the expected behavior?
Modules should be transformed.
Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.
jest 22.0.4
ts-jest 22.0.0
node 8.9.2
npm 5.5.1
OS Windows 10
The text was updated successfully, but these errors were encountered: