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-jest does use babel default env ("development") #1654

Closed
MoOx opened this Issue Sep 9, 2016 · 4 comments

Comments

Projects
None yet
3 participants
@MoOx
Contributor

MoOx commented Sep 9, 2016

Do you want to request a feature or report a bug?

I think I found a bug while converting some tests from AVA to Jest. (AVA also handle babel transformation, but jest does do the transform the same way, see below).

What is the current behavior?

I am using the first react example from the website homepage and I am getting an "Unexpected token" on the <a. This is because my babel configuration is not correctly interpreted.

Currently, it seems that babel-jest does not interpret babelrc/env.development configuration by default.
According to babelrc doc, when no BABEL_ENV / NODE_ENV are specified, "development" is used for the "env".

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal repository on GitHub that we can npm install and npm test.

Currently, I don't have any babel configuration by default. I am using webpack 2 & does not need imports to be transformed blahblah so here is my configuration

{
  "env": {
    "development": {
      "presets": [ "latest", "stage-2", "react" ]
    },
    "production": {
      "presets": [ "latest", "stage-2", "react" ]
    },
    "webpack-development": {
      "presets": [
        [ "latest",{"es2015":{"modules": false}} ], "stage-2", "react"
      ],
      "plugins": [ "react-hot-loader/babel" ]
    },
    "webpack-production": {
      "presets": [
        "react-optimize",
        [ "latest",{"es2015":{"modules": false}} ], "stage-2", "react"
      ]
    }
  }
}

As you can see, all my configs are in "env".

What is the expected behavior?

I expect jest to set BABEL_ENV to "development" if NODE_ENV==="" && BABEL_ENV === "".

If I launch jest with NODE_ENV=development it works.

Run Jest again with --debug and provide the full configuration it prints. Please mention your node and npm version and operating system.

Node v6.4.0
npm 3.10.3
ProductName:    Mac OS X
ProductVersion: 10.12
BuildVersion:   16A313a
$ jest --debug --no-cache src/modules/NavigationMenu/__tests__/index.js
jest version = 15.1.1
test framework = jasmine2
config = {
  "rootDir": "/Users/moox/Sync/Development/_EDITED_",
  "name": "-Users-moox-Sync-Development-_clients-mphasis-wyde-WynsureGroupAdmin",
  "setupFiles": [
    "/Users/moox/Sync/Development/_EDITED_/node_modules/babel-polyfill/lib/index.js"
  ],
  "testRunner": "/Users/moox/Sync/Development/_EDITED_/node_modules/jest-jasmine2/build/index.js",
  "scriptPreprocessor": "/Users/moox/Sync/Development/_EDITED_/node_modules/babel-jest/build/index.js",
  "usesBabelJest": true,
  "preprocessorIgnorePatterns": [],
  "automock": false,
  "bail": false,
  "browser": false,
  "cacheDirectory": "/var/folders/fp/xz51yg_945dc86f52ncz3jh00000gn/T/jest",
  "colors": false,
  "coveragePathIgnorePatterns": [
    "/node_modules/"
  ],
  "coverageReporters": [
    "json",
    "text",
    "lcov",
    "clover"
  ],
  "globals": {},
  "haste": {
    "providesModuleNodeModules": []
  },
  "mocksPattern": "__mocks__",
  "moduleDirectories": [
    "node_modules"
  ],
  "moduleFileExtensions": [
    "js",
    "json",
    "node"
  ],
  "moduleNameMapper": {},
  "modulePathIgnorePatterns": [],
  "noStackTrace": false,
  "notify": false,
  "preset": null,
  "resetModules": false,
  "testEnvironment": "jest-environment-jsdom",
  "testPathDirs": [
    "/Users/moox/Sync/Development/_EDITED_"
  ],
  "testPathIgnorePatterns": [
    "/node_modules/"
  ],
  "testRegex": "(/__tests__/.*|\\.(test|spec))\\.js$",
  "testURL": "about:blank",
  "timers": "real",
  "useStderr": false,
  "verbose": null,
  "watch": false,
  "cache": false,
  "watchman": true,
  "testcheckOptions": {
    "times": 100,
    "maxSize": 200
  }
}
 FAIL  src/modules/NavigationMenu/__tests__/index.js
  ● Test suite failed to run

    SyntaxError: /Users/moox/Sync/Development/_EDITED_/src/modules/NavigationMenu/__tests__/index.js: Unexpected token (6:4)
      4 | it("renders correctly", () => {
      5 |   const tree = renderer.create(
    > 6 |     <a page="http://www.facebook.com">{ "Facebook" }</a>
        |     ^
      7 |   ).toJSON()
      8 |   expect(tree).toMatchSnapshot()
      9 | })

      at Parser.pp.raise (node_modules/babylon/lib/parser/location.js:22:13)
      at Parser.pp.unexpected (node_modules/babylon/lib/parser/util.js:91:8)
      at Parser.pp.parseExprAtom (node_modules/babylon/lib/parser/expression.js:520:12)
      at Parser.pp.parseExprSubscripts (node_modules/babylon/lib/parser/expression.js:278:19)
      at Parser.pp.parseMaybeUnary (node_modules/babylon/lib/parser/expression.js:258:19)
      at Parser.pp.parseExprOps (node_modules/babylon/lib/parser/expression.js:188:19)
      at Parser.pp.parseMaybeConditional (node_modules/babylon/lib/parser/expression.js:165:19)
      at Parser.pp.parseMaybeAssign (node_modules/babylon/lib/parser/expression.js:128:19)
      at Parser.pp.parseExprListItem (node_modules/babylon/lib/parser/expression.js:1042:16)
      at Parser.pp.parseCallExpressionArguments (node_modules/babylon/lib/parser/expression.js:354:20)

Test Summary
 › Ran all tests matching "src/modules/NavigationMenu/__tests__/index.js".
 › 1 test suite failed, 0 tests passed (0 total in 1 test suite, run time 4.168s)

Here is the working line

NODE_ENV=development jest --debug --no-cache src/modules/NavigationMenu/__tests__/index.js
jest version = 15.1.1
test framework = jasmine2
config = {
  "rootDir": "/Users/moox/Sync/Development/_EDITED_",
  "name": "-Users-moox-Sync-Development-_clients-mphasis-wyde-WynsureGroupAdmin",
  "setupFiles": [
    "/Users/moox/Sync/Development/_EDITED_/node_modules/babel-polyfill/lib/index.js"
  ],
  "testRunner": "/Users/moox/Sync/Development/_EDITED_/node_modules/jest-jasmine2/build/index.js",
  "scriptPreprocessor": "/Users/moox/Sync/Development/_EDITED_/node_modules/babel-jest/build/index.js",
  "usesBabelJest": true,
  "preprocessorIgnorePatterns": [],
  "automock": false,
  "bail": false,
  "browser": false,
  "cacheDirectory": "/var/folders/fp/xz51yg_945dc86f52ncz3jh00000gn/T/jest",
  "colors": false,
  "coveragePathIgnorePatterns": [
    "/node_modules/"
  ],
  "coverageReporters": [
    "json",
    "text",
    "lcov",
    "clover"
  ],
  "globals": {},
  "haste": {
    "providesModuleNodeModules": []
  },
  "mocksPattern": "__mocks__",
  "moduleDirectories": [
    "node_modules"
  ],
  "moduleFileExtensions": [
    "js",
    "json",
    "node"
  ],
  "moduleNameMapper": {},
  "modulePathIgnorePatterns": [],
  "noStackTrace": false,
  "notify": false,
  "preset": null,
  "resetModules": false,
  "testEnvironment": "jest-environment-jsdom",
  "testPathDirs": [
    "/Users/moox/Sync/Development/_EDITED_"
  ],
  "testPathIgnorePatterns": [
    "/node_modules/"
  ],
  "testRegex": "(/__tests__/.*|\\.(test|spec))\\.js$",
  "testURL": "about:blank",
  "timers": "real",
  "useStderr": false,
  "verbose": null,
  "watch": false,
  "cache": false,
  "watchman": true,
  "testcheckOptions": {
    "times": 100,
    "maxSize": 200
  }
}
 PASS  src/modules/NavigationMenu/__tests__/index.js
  ✓ renders correctly (21ms)

Test Summary
 › Ran all tests matching "src/modules/NavigationMenu/__tests__/index.js".
 › 1 test passed (1 total in 1 test suite, 1 snapshot, run time 7.906s)
@MoOx

This comment has been minimized.

Show comment
Hide comment
@MoOx

MoOx Sep 9, 2016

Contributor

I finished by understanding that "test" is used. But this can be confusing when you don't know. Maybe it should just be added into the doc? (Near babel-jest part)

Contributor

MoOx commented Sep 9, 2016

I finished by understanding that "test" is used. But this can be confusing when you don't know. Maybe it should just be added into the doc? (Near babel-jest part)

@cpojer

This comment has been minimized.

Show comment
Hide comment
@cpojer

cpojer Sep 9, 2016

Contributor

Yeah you are right, we should add documentation for this. Jest sets the NODE_ENV to test if it isn't provided and otherwise let's you use a custom override. We don't use BABEL_ENV at all. I'm gonna close as wontfix but I'm happy to accept a PR for documentation.

Contributor

cpojer commented Sep 9, 2016

Yeah you are right, we should add documentation for this. Jest sets the NODE_ENV to test if it isn't provided and otherwise let's you use a custom override. We don't use BABEL_ENV at all. I'm gonna close as wontfix but I'm happy to accept a PR for documentation.

@cpojer cpojer closed this Sep 9, 2016

cpojer added a commit that referenced this issue Sep 10, 2016

Docs: add a note in Getting Started about babel-jest using NODE_ENV=t…
…est (#1659)

* Docs: add a note in Getting Started about babel-jest using NODE_ENV=test 

Ref #1654

* Docs: add a note in Getting Started about babel-jest using NODE_ENV=test (edit)
@AndersDJohnson

This comment has been minimized.

Show comment
Hide comment
@AndersDJohnson

AndersDJohnson Aug 3, 2017

@cpojer I think babel-core's transform should handle reading BABEL_ENV but you need to pass along env from the user's configuration into it, which seems like it may not be happening here? Unless we're relying on Babel to do that since we're not disabling babelrc option.

options = Object.assign({}, options, {

AndersDJohnson commented Aug 3, 2017

@cpojer I think babel-core's transform should handle reading BABEL_ENV but you need to pass along env from the user's configuration into it, which seems like it may not be happening here? Unless we're relying on Babel to do that since we're not disabling babelrc option.

options = Object.assign({}, options, {

tushardhole pushed a commit to tushardhole/jest that referenced this issue Aug 21, 2017

Docs: add a note in Getting Started about babel-jest using NODE_ENV=t…
…est (#1659)

* Docs: add a note in Getting Started about babel-jest using NODE_ENV=test 

Ref #1654

* Docs: add a note in Getting Started about babel-jest using NODE_ENV=test (edit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment