[英]import/export of external types failing in jest unit tests (vue2)
I'm trying to create a package which exports some types, enums, consts and interfaces I use across multiple projects.我正在尝试创建一个 package,它导出我在多个项目中使用的一些类型、枚举、常量和接口。
So I created a main.ts
and I put all the exports there, specify it in package.json: "main": "main.ts"
.所以我创建了一个main.ts
并将所有导出放在那里,在 package.json: "main": "main.ts"
中指定它。
One of my exports needs some mapbox-gl
types.我的一个出口需要一些mapbox-gl
类型。 Obviously, I don't want my package to include mapbox-gl
types.显然,我不希望我的 package 包含mapbox-gl
类型。 So I did the following:所以我做了以下事情:
"peerDependencies": {
"mapbox-gl": "^1.13.0"
},
"devDependencies": {
"@types/mapbox-gl": "^1.13.0",
}
Inside main.ts
, I do:在main.ts
里面,我做的是:
import mapboxgl from "mapbox-gl";
export interface DataSourceOptions {
shouldLoad?: boolean;
name: string;
map: mapboxgl.Map;
layer: mapboxgl.Layer;
}
I published my package, imported it in my project and it works as expected.我发布了我的 package,将其导入到我的项目中,它按预期工作。 Until I try to test any of the components using this package.直到我尝试使用这个 package 测试任何组件。
Jest is throwing the following error: Jest 抛出以下错误:
D:\path\to\project\node_modules\some-custom-package\main.ts:1 ({"Object.":function(module,exports,require,__dirname,__filename,global,jest){import mapboxgl from "mapbox-gl"; SyntaxError: Cannot use import statement outside a module D:\path\to\project\node_modules\some-custom-package\main.ts:1 ({"Object.":function(module,exports,require,__dirname,__filename,global,jest){import mapboxgl from " mapbox-gl"; SyntaxError: 不能在模块外使用 import 语句
I tried to fix this by specifying my package in the transformIgnorePatterns
of jest.config.js
:我试图通过在 jest.config.js 的transformIgnorePatterns
中指定我的jest.config.js
来解决这个问题:
transformIgnorePatterns: [
"<rootDir>/node_modules/(?!(@mapbox/mapbox-gl-draw" +
"|some-custom-package" +
")/)",
],
However, I'm still seeing the same error.但是,我仍然看到同样的错误。
I've also tried bundling my package with rollup
, using either tsc
and rollup-plugin-typescript2
plugins, because I know in rollup
I can use externals
to declare mapbox-gl
as such.我还尝试使用tsc
和rollup-plugin-typescript2
插件将我的 package 与rollup
捆绑在一起,因为我知道在rollup
中我可以使用externals
来声明mapbox-gl
。 For some unknown reason, neither tsc
nor rollup-plugin-typescript2
seem to declare my interfaces, they only declare const
s, type
s and enum
s (possibly related ).由于某些未知原因, tsc
和rollup-plugin-typescript2
似乎都没有声明我的接口,它们只声明了const
s、 type
s 和enum
s(可能相关)。
I know it looks like multiple questions into one but I'm only looking for a solution.我知道这看起来像是将多个问题合二为一,但我只是在寻找解决方案。
mapbox-gl
as external或者找到一种方法来保留接口,同时将我的导出与汇总捆绑在一起并将mapbox-gl
声明为外部A possible workaround I see would be to circumvent the mapboxgl import in my package, by making the types I use from it dynamic:我看到的一个可能的解决方法是通过使我从中使用的类型动态化来绕过我的 package 中的 mapboxgl 导入:
export interface DataSourceOptions<M,L> {
shouldLoad?: boolean;
name: string;
map: M;
layer: L;
}
and, in my project go: options: DataSourceOptions<mapboxgl.Map, mapboxgl.Layer>
, but I don't really like this solution, to be fair.并且,在我的项目 go: options: DataSourceOptions<mapboxgl.Map, mapboxgl.Layer>
中,但公平地说,我不太喜欢这个解决方案。 It circumvents the problem, it doesn't solve it.它规避了问题,并没有解决问题。
I was able to fix it by setting:我能够通过设置来修复它:
"type": "module"
in the the modules' package.json
, together with adding it to transformIgnorePatterns
in jest.config.js
of the project consuming it, as shown above.在模块的package.json
中,同时将其添加到使用它的项目的jest.config.js
中的transformIgnorePatterns
,如上所示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.