在Lerna Monorepo中运行Mocha测试无法编译导入

时间:2019-07-01 15:27:36

标签: javascript mocha babel es6-modules lerna

错误报告

当前行为

~/######/######/packages/core
$ yarn test

/Users/######/######/######/packages/primitives/dist/primitives.js:1
import styled, { css } from 'styled-components';
       ^^^^^^

SyntaxError: Unexpected identifier

我们有一个monorepo设置,其中有一个使用Babel 7和Rollup to esm编译的软件包。我们正在使用setup.js文件运行Mocha测试,该文件在测试运行之前运行babel寄存器。由于某种原因,在所有其他代码和node_modules都可以编译的时候,似乎其他程序包(原始)没有被编译。

输入代码

require('@babel/register')({
  presets: ['@babel/preset-env', '@babel/preset-flow', '@babel/preset-react'],
  plugins: [
    '@babel/plugin-proposal-export-default-from',
    '@babel/plugin-proposal-logical-assignment-operators',
    [
      '@babel/plugin-proposal-optional-chaining',
      {
        loose: false,
      },
    ],
    [
      '@babel/plugin-proposal-pipeline-operator',
      {
        proposal: 'minimal',
      },
    ],
    [
      '@babel/plugin-proposal-nullish-coalescing-operator',
      {
        loose: false,
      },
    ],
    '@babel/plugin-proposal-do-expressions',
    [
      '@babel/plugin-proposal-decorators',
      {
        legacy: true,
      },
    ],
    '@babel/plugin-proposal-function-sent',
    '@babel/plugin-proposal-export-namespace-from',
    '@babel/plugin-proposal-numeric-separator',
    '@babel/plugin-proposal-throw-expressions',
    '@babel/plugin-syntax-dynamic-import',
    '@babel/plugin-syntax-import-meta',
    [
      '@babel/plugin-proposal-class-properties',
      {
        loose: false,
      },
    ],
    '@babel/plugin-proposal-json-strings',
    '@babel/plugin-transform-modules-commonjs',
    '@babel/plugin-transform-runtime',
  ],
});

const { configure } = require('enzyme');
const mock = require('mock-require');
const { JSDOM } = require('jsdom');
const Adapter = require('enzyme-adapter-react-16');

require('ignore-styles').default(['.md', '.css', '.scss', '.png']);

configure({ adapter: new Adapter() });
const jsdom = new JSDOM('<!doctype html><html><body></body></html>');

function copyProps(src, target) {
  const props = Object.getOwnPropertyNames(src)
    .filter((prop) => typeof target[prop] === 'undefined')
    .map((prop) => Object.getOwnPropertyDescriptor(src, prop));
  Object.defineProperties(target, props);
}

global.window = jsdom.window;
global.self = {
  addEventListener: () => {},
  postMessage: () => {},
};
global.document = jsdom.window.document;
global.navigator = {
  userAgent: 'node.js',
};
global.CustomEvent = jsdom.window.CustomEvent;

/* global window */
copyProps(window, global);

mock('Utilities/IconClasses', '../utils/IconClasses');
mock('Utilities/Mobile', '../utils/Mobile');
mock('Utilities/Deprecated', '../utils/Deprecated');
mock('Utilities/ISOString', '../utils/ISOString');
mock('Utilities/position', '../utils/position');
mock('Utilities/Validate', '../utils/Validate');
mock('Internal/IconWrapper', '../internal/IconWrapper');

预期的行为/代码 我的期望是所有模块将在尝试运行摩卡测试之前进行编译。

Babel配置(.babelrc,package.json,cli命令)

{
  presets: ['@babel/preset-env', '@babel/preset-flow', '@babel/preset-react'],
  plugins: [
    '@babel/plugin-proposal-export-default-from',
    '@babel/plugin-proposal-logical-assignment-operators',
    [
      '@babel/plugin-proposal-optional-chaining',
      {
        loose: false,
      },
    ],
    [
      '@babel/plugin-proposal-pipeline-operator',
      {
        proposal: 'minimal',
      },
    ],
    [
      '@babel/plugin-proposal-nullish-coalescing-operator',
      {
        loose: false,
      },
    ],
    '@babel/plugin-proposal-do-expressions',
    [
      '@babel/plugin-proposal-decorators',
      {
        legacy: true,
      },
    ],
    '@babel/plugin-proposal-function-sent',
    '@babel/plugin-proposal-export-namespace-from',
    '@babel/plugin-proposal-numeric-separator',
    '@babel/plugin-proposal-throw-expressions',
    '@babel/plugin-syntax-dynamic-import',
    '@babel/plugin-syntax-import-meta',
    [
      '@babel/plugin-proposal-class-properties',
      {
        loose: false,
      },
    ],
    '@babel/plugin-proposal-json-strings',
    '@babel/plugin-transform-modules-commonjs',
    '@babel/plugin-transform-runtime',
  ],
}

环境 -Babel版本:v7.4.5 -节点/ npm版本:v10.16.0 -作业系统:OSX 10.14.5 -Monorepo:Lerna -您如何使用Babel:注册

0 个答案:

没有答案