1

我正在尝试在使用 react-scripts 生成的 React 项目中运行单元测试,其中我添加了 ReScript 支持。

但是,当我运行测试时,我在转译的 javascript 代码中遇到了错误。

错误详情:

/Users/massimilianodacunzo/Projects/Elixir/test-project/apps/phoenix_react_web/assets/node_modules/bs-platform/lib/es6/array.js:3
import * as Curry from "./curry.js";
^^^^^^

SyntaxError: Cannot use import statement outside a module

  1 |
  2 |
> 3 | import * as $$Array from "../../../node_modules/bs-platform/lib/es6/array.js";
    | ^
  4 | import * as React from "react";
  5 |
  6 | function TestComponent(Props) {

  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
  at Object.<anonymous> (src/components/users/TestComponent.bs.js:3:1)

我要测试的组件:

应用程序

%%raw(`
import logo from './logo.svg';
import './App.css';
`)

@react.component
let make = () => {
    <div className="App">
        <TestComponent elements={["1", "2", "3"]} />
    </div>
}

临时组件.res

@react.component
let make = (
    ~elements: array<string>
) => {
    <ul>
        {
            React.array(
                elements 
                |> Array.map(e => 
                    <li key={e}>{React.string(e)}</li>)
            )
        }
    </ul>
}

生成的TestComponent.bs.js

import * as $$Array from "../../../node_modules/bs-platform/lib/es6/array.js";
import * as React from "react";

function TestComponent(Props) {
  var elements = Props.elements;
  return React.createElement("ul", undefined, $$Array.map((function (e) {
                    return React.createElement("li", {
                                key: e
                              }, e);
                  }), elements));
}

var make = TestComponent;

export {
  make ,
  
}
/* react Not a pure module */

我可以向react-scripts test脚本添加任何其他配置吗?

4

1 回答 1

2

根据glennsl的评论,我关注了 git 问题,发现我的 bsconfig.json 文件中的配置将 package-specs 模块指定为es6如果我将配置更改为commonjs,则不会出现测试错误。

{
    ...
    "package-specs": {
      "module": "commonjs",
      "in-source": true
    }
    ...
}

再次感谢glennsl为我指明了正确的方向。

于 2021-03-27T20:56:12.073 回答