Unable to use Babel 7 with Jest

2019-07-13 14:43发布

问题:

So, I've tried following Jest's documentation on setting up Babel, including installing babel-core: 7.0.0-bridge.0 and such, but I've had no luck in getting it to work.

I've tried to dig through their issues on GitHub but many of the solutions there don't work for me, primarily because they are using a babelrc file where as I'm using babel.config.js, and due to Babel's incredibly hard to read documentation, I cannot for the life figure out how to convert what they are doing in rc files into the js equivalent.

Hopefully someone can provide a solution!

My configuration files are below as well as the error message I'm getting;

babel.config.js

module.exports = api => {
  api.cache(true);

  const presets = [
    [
      '@babel/preset-env',
      {
        targets: {
          node: true,
        },
      },
    ],
  ];

  return {
    presets,
  };
};

jest.config.js

module.exports = {
  testEnvironment: 'node',
  collectCoverage: true,
  verbose: true,
};

Error Msg:

Test suite failed to run

    tests/integration/mytest.test.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import request from 'supertest';
                                                                                                    ^^^^^^^

    SyntaxError: Unexpected identifier

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)