为什么 import 一个库后,就 syntax error, unexpected token 'export' 了?

2021-01-19 10:20:28 +08:00
 yazoox

nodejs, react, typescript

node v14.7.0
react 17.0
jest 26.6.3

这是一个测试,主要用来测试入口文件的。 我不清楚为什么写这个测试,都是 legacy (几年前的,写的人也走了)的代码。
也不太好删除,否则,得去“解释”为什么把这个测试删除。(大家都懂的)

我的理解,这个测试主要就是,看看能否正常把 index module 给 load 进来,不出错就行。

describe("tests", () => {
  describe("bootstraping the app", () => {
    it("test react render method", () => {
      const app = document.createElement("div");
      app.setAttribute("id", "app");
      document.body.appendChild(app);

      const bootStrapper = () => {
        require("../index");
      };

      expect(bootStrapper).not.toThrowError();

      document.body.removeChild(app);
    });
  });
});

然后,index.tsx 文件,就是我们程序的入口文件,引用 react,然后 render app.

import * as React from "react";
... blabla


现在,有新的需要,我添加了 一个 import

import * as React from "react";
import { Inspector } from "react-dev-inspector";

<inspector>
  <app />
</inspector>

用这个 wrap 原来的 app.

项目都能够正常编译,运行。所有的功能使用下来都正常。但就是上面这个测试跑不过。
提示出错的位置,就是那个 import 语句那里。

    expect(received).not.toThrowError()

    Error name:    "SyntaxError"
    Error message: "Unexpected token 'export'"

不是很清楚,是 module 导出有问题,还是 import 有问题。
google 了半天,没有找到类似问题或者合适的答案。

特来请教一下。 谢谢

p.s. https://stackoverflow.com/questions/38296667/getting-unexpected-token-export

2309 次点击
所在节点    JavaScript
9 条回复
yaphets666
2021-01-19 10:25:02 +08:00
import { Inspector } from "react-dev-inspector";
试下把大括号去掉
import Inspector from "react-dev-inspector";
yazoox
2021-01-19 10:40:36 +08:00
@yaphets666
我查看了一下 react-dev-inspector 的文件 index.d.ts ,就一行。
```
export * from './Inspector';
```
所以,你说的方法不行。不过,我试了一下

import * as devinspector from "react-dev-inspector";
blabla...

yarn start 能够编译通过,程序能运行。但是,test 还是过不了。错误和前面一样的。
magicdawn
2021-01-19 11:45:40 +08:00
项目跑起来和测试不是一个配置呗.
建议检查 tsconfig.json compileOptions.module / babel 相关的 includes
alan0liang
2021-01-19 12:15:11 +08:00
应该是没有设好 babel-loader 的 excludes 吧,默认情况下我记得 babel-loader 会跳过转译 node_modules,需要自己在 webpack config 里设一下

#1 不对,这个包用的确实是 named export 而不是 default export,需要花括号
yazoox
2021-01-20 10:24:33 +08:00
@alan0liang
项目运行需要编译,所以会跑 webpack,但是 jest 好像没有,直接运行的
我们有个 jest.config.js 的配置文件 ,里面倒是有 babel 相关

module.exports = {
preset: "ts-jest",
testEnvironment: "jsdom",
reporters: ["default", "jest-junit"],
globals: {
"ts-jest": {
babelConfig: {
plugins: ["dynamic-import-node", "babel-plugin-styled-components"]
},
tsconfig: "<rootDir>/tsconfig.jest.json"
}
}
}



@magicdawn
tsconfig.jest.json 只比项目运行的 jest 文件多下面这一项配置

{
"extends": "./tsconfig.json",
"compilerOptions": {
"esModuleInterop": true
}
}
zthxxx
2021-01-21 11:02:22 +08:00
@yazoox jest 如果要跑 jsx / tsx 文件,最好也编译一下,保证和 webpack loader 处理后一致,如 jest 有 transform 字段配置,通常会配置 ts-jest 和 babel

https://jestjs.io/docs/en/configuration#transform-objectstring-pathtotransformer--pathtotransformer-object
zthxxx
2021-01-21 11:09:46 +08:00
zthxxx
2021-01-21 14:44:32 +08:00
或者在 jest 环境下就不去 import 这个库呢,因为这个库导出的给 react 的组件是 es module 类型的 (导出的给 node 用的 plugin 这些才是 commonjs 的),所以在你的 jest 下如果不经过 babel / esm 这些转换就不支持
yazoox
2021-01-21 21:16:30 +08:00
@zthxxx 明白你的意思了,我再试试。thx

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/746186

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX