question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Only in coverage mode, latest babel: 'import' and 'export' may appear only with 'sourceType: "module"'

See original GitHub issue

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

Bug

What is the current behavior?

Since updating to Babel 7.0-beta.46, Jest fails only when using --coverage with this error:

ERROR: 'import' and 'export' may appear only with 'sourceType: "module"' (8:0)
STACK: SyntaxError: 'import' and 'export' may appear only with 'sourceType: "module"' (1:0)
    at Parser.pp$5.raise (/Users/fakepath/node_modules/babylon/lib/index.js:4454:13)
    at Parser.pp$1.parseStatement (/Users/fakepath/node_modules/babylon/lib/index.js:1881:16)
    at Parser.parseStatement (/Users/fakepath/node_modules/babylon/lib/index.js:5910:22)
    at Parser.pp$1.parseBlockBody (/Users/fakepath/node_modules/babylon/lib/index.js:2268:21)
    at Parser.pp$1.parseTopLevel (/Users/fakepath/node_modules/babylon/lib/index.js:1778:8)
    at Parser.parse (/Users/adrien/fakepath/node_modules/babylon/lib/index.js:1673:17)
    at Object.parse (/Users/fakepath/node_modules/babylon/lib/index.js:7305:37)
    at Instrumenter.instrumentSync (/Users/fakepath/node_modules/istanbul-lib-instrument/dist/instrumenter.js:123:31)
    at exports.default (/Users/fakepath/node_modules/jest-cli/build/generate_empty_coverage.js:18:18)
    at Object.worker (/Users/fakepath/node_modules/jest-cli/build/reporters/coverage_worker.js:48:84)
Failed to collect coverage from /Users/fakepath/src/utils/Utils.js

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

Couldn’t manage to reproduce it in a minimal environment (yet). Still working on it.

What is the expected behavior?

Jest should work the same with or without --coverage

Please provide your exact Jest configuration

const path = require('path');

const configBase = {
  collectCoverageFrom: ['**/utils/*.js', '!**/lib/utils/**'],
  coverageDirectory: path.join(__dirname, '../../coverage'),
  moduleNameMapper: {
    '\\.(css|scss)$': 'identity-obj-proxy',
    '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': path.join(
      __dirname,
      'node_modules/shared/__mocks__/fileMock.js',
    ),
  },
  moduleDirectories: ['../../node_modules', 'node_modules'],
  modulePaths: [path.join(__dirname, 'client')],
  rootDir: __dirname,
  roots: [__dirname],
  testRegex: '\\.test\\.js$',
};

const configJUnit = process.env.JEST_JUNIT_OUTPUT
  ? {
      testResultsProcessor: 'jest-junit',
    }
  : {};

module.exports = Object.assign({}, configBase, configJUnit);

Run npx envinfo --preset jest in your project directory and paste the results here

  System:
    OS: macOS High Sierra 10.13.4
    CPU: x64 Intel(R) Core(TM) i7-7567U CPU @ 3.50GHz
  Binaries:
    Node: 8.11.1 - /usr/local/bin/node
    Yarn: 1.5.1 - /usr/local/bin/yarn
    npm: 5.6.0 - /usr/local/bin/npm

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:24

github_iconTop GitHub Comments

129reactions
loganfsmythcommented, Apr 23, 2018

Babel beta.45/beta.46 introduces a breaking change that will primarily affect monorepo users, and people compiling node_modules files. .babelrc searching now

  • Stops at the first package.json it finds relative to the file being compiled, so things in sub-packages will not find a root-level .babelrc
  • Only searches if the file being compiled is inside of the “root” package, which means it will skip searching for .babelrc files entirely for sub-packages in a repo

In most monorepos, that means that a config at the root will be ignored, and configs in sub-packages will also.

To have a config at the root of your monorepo, you should name the file babel.config.js, which is a new non-hierarchical config file that loads from the root directory. If you want to keep per-package configs, instead you’ll want to enable configs in packages/*/.babelrc to work in a monorepo by passing Babel

babelrcRoots: "packages/*"

in the programmatic options.

For people compiling node_modules this is also a change, because a .babelrc in one package will not affect that package’s node_modules. You’ll also want to rename your project-root .babelrc to a babel.config.js file exporting the config, to do that.

If your config doesn’t fall into one of those changes, then there could always still be a bug somewhere.

5reactions
shorif2000commented, Aug 17, 2019

is there an example repo?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cypress ParseError: 'import' and 'export' may appear only with ...
This error is caused by the presence of modern keywords like "import" and "export" when Cypress runs in the browser.
Read more >
'import' and 'export' may appear only with 'sourceType: module ...
It happens just after the npm initialization and Budo installation. I didn't install Babel, Gulp o anything yet. import pop from "../pop/index.
Read more >
Config Files - Babel.js
Babel has two parallel config file formats, which can be used together, ... .babelrc.json files only apply to files within their own package...
Read more >
'import' and 'export' may appear only with 'sourceType: module ...
Coding example for the question SyntaxError: 'import' and 'export' may appear only with 'sourceType: module' - Gulp-babel.js.
Read more >
babel-loader - webpack
Usage. webpack documentation: Loaders. Within your webpack configuration object, you'll need to add the babel-loader to the list of modules ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found