Skip to content
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

Babel not transforming custom testEnvironment #11032

Closed
ysfaran opened this issue Jan 27, 2021 · 2 comments
Closed

Babel not transforming custom testEnvironment #11032

ysfaran opened this issue Jan 27, 2021 · 2 comments

Comments

@ysfaran
Copy link

ysfaran commented Jan 27, 2021

🐛 Bug Report

With testEnvironment configuration option a custom test environment can be specified. Babel seems to skip the specified testEnvironment file and only transforms test files. This causes confusion since test code is transformed differently from the testEnvironment file.

To Reproduce

Steps to reproduce the behavior:

babel.config.js

module.exports = {
  presets: [["@babel/preset-env", { targets: { node: "current" } }]],
};

jest.config.js

module.exports = {
  rootDir: ".",
  testEnvironment: "<rootDir>/custom-environment.js",
};

custom-environment.js

const NodeEnvironment = require("jest-environment-node");

const test = {};
console.log("environment: " + test.a?.b);

class NodeEnvironmentFailFast extends NodeEnvironment {}

module.exports = NodeEnvironmentFailFast;

optional-chaining.spec.js

it("check optional chaining", () => {
  const test = {};
  console.log("test: " + test.a?.b);
});

package.json

{
  "name": "GoodPracticalProgrammingmacro",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "jest": "^26.6.3",
    "jest-environment-node": "^26.6.2",
    "@babel/preset-env": "7.12.11"
  }
}

Executing above test in a Node 12 environment (which doesn't support optional chaining) will fail with following output:

> jest
 FAIL  ./optional-chaining.test.js
  ● Test suite failed to run

    /home/runner/jest-playground/custom-environment.js:4
    console.log("environment: " + test.a?.b);
                                         ^

    SyntaxError: Unexpected token '.'

      at node_modules/jest-runner/build/runTest.js:259:7

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.077s, estimated 12s
Ran all test suites.
exit status 1

If you comment the line mentioned in the error output (console.log("environment: " + test.a?.b);) it will work just fine, which indicates that optional-chaining.test.js is transformed properly, while custom-environment.js isn't.

Expected behavior

Babel should also transform testEnvironment file (in this case custom-environment.js) just as any other file.

Link to repl or repo

https://repl.it/join/kbuocjuy-ysfaran

envinfo

root@bacf2e4aeae1:/app# npx envinfo --preset jest
npx: installed 1 in 0.912s

  System:
    OS: Linux 4.19 Debian GNU/Linux 9 (stretch) 9 (stretch)
    CPU: (6) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 12.20.1 - /usr/local/bin/node
    Yarn: 1.22.5 - /usr/local/bin/yarn
    npm: 6.14.10 - /usr/local/bin/npm
  npmPackages:
    jest: ^26.6.3 => 26.6.3
@ahnpnl
Copy link
Contributor

ahnpnl commented Jan 27, 2021

This has been done in #8751 under next tag

@github-actions
Copy link

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants