Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do my jest tests run locally, but not on Travis?

I have a simple javascript project that is using ES6, plus import and export. Here is my .babelrc file

{
  "env": {
    "es": {
      "presets": [
        [
          "env",
          {
            "targets": {
              "browsers": ["last 2 versions"],
              "node": "current"
            },
            "modules": false
          }
        ]
      ],
      "ignore": ["**/*.test.js", "**/tests/*"]
    },
    "test": {
      "presets": ["env"]
    },
    "cjs": {
      "presets": [
        [
          "env",
          {
            "targets": {
              "browsers": ["last 2 versions"],
              "node": "current"
            }
          }
        ]
      ],
      "ignore": ["**/*.test.js", "**/tests/*"]
    }
  }
}

I run my tests with npm test, which runs this command out of my package.json: "test": "jest --notify".

Locally, everything runs fine and all my tests pass, but when I push to Travis, I get this error: SyntaxError: Unexpected token import. It does not recognize my import statements.

Here is my .travis.yml:

language: node_js
cache:
  yarn: true
node_js:
- '8.4'
script:
- npm test
env:
- NODE_ENV: feature/travis

Why is my .babelrc not working on Travis? What am I missing?

like image 363
jhamm Avatar asked Feb 03 '18 05:02

jhamm


People also ask

Do Jest tests run in a browser?

The answer is down to Jest not being a real browser. You have access to all of the various DOM APIs when writing Jest tests but these are provided by JSDOM and your tests are actually running in Node. Jest has no native way to run your tests in a browser environment out of the box.

Does Jest run all tests in parallel?

Each time a test run completes, the global environment is automatically reset for the next. Since tests are standalone and their execution order doesn't matter, Jest runs tests in parallel.

Do Jest test suites run in parallel?

Parallelism and concurrency What happens when running tests sequentially. To speed-up your tests, Jest can run them in parallel. By default, Jest will parallelise tests that are in different files.

Can you run Jest tests from the terminal?

You can run Jest directly from the CLI (if it's globally available in your PATH , e.g. by yarn global add jest or npm install jest --global ) with a variety of useful options.


1 Answers

This is likely happening because you have defined NODE_ENV: feature/travis in your travis.yml. This will cause Travis to run your tests as NODE_ENV=feature/travis jest --notify.

This generally shouldn't cause concern, but jest will automatically set the NODE_ENV to test if it is not already set.

It could be that you're issue is similar to the discussion taking place over at https://github.com/facebook/jest/issues/3370. There is some contention on whether Jest should be automatically setting the NODE_ENV to test or if it should just use development if no NODE_ENV is present (or just respect whatever NODE_ENV is already set if its set globally).

You can also see this logic in the jest-cli https://github.com/facebook/jest/blob/master/packages/jest-cli/bin/jest.js#L12-L14

So my recommendation is to test this without having your NODE_ENV set in your travis.yml to see if it successfully runs all of your tests.

like image 158
mootrichard Avatar answered Oct 12 '22 23:10

mootrichard