jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.31k stars 6.47k forks source link

babel-jest does use babel default env ("development") #1654

Closed MoOx closed 8 years ago

MoOx commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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.

AndersDJohnson commented 7 years ago

@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. https://github.com/facebook/jest/blob/ab6f4c3001a2cd917cc4df95e42dfc72f23c5a00/packages/babel-jest/src/index.js#L70

AndersDJohnson commented 7 years ago

References:

http://facebook.github.io/jest/docs/getting-started.html#babel-integration

http://facebook.github.io/jest/docs/configuration.html#transform-object-string-string

https://www.npmjs.com/package/babel-core#options

github-actions[bot] commented 3 years ago

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.