简体   繁体   English

外部类型的导入/导出在 jest 单元测试中失败(vue2)

[英]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.我还尝试使用tscrollup-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 ).由于某些未知原因, tscrollup-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.我知道这看起来像是将多个问题合二为一,但我只是在寻找解决方案。

  • either resolve jest's problem in importing my package (again, what I have works quite well in the actual app - it only fails in tests)要么解决导入我的 package 时开玩笑的问题(同样,我在实际应用程序中运行良好 - 它仅在测试中失败)
  • or find a way to keep interfaces while bundling my exports with rollup and declaring 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.

相关问题 ReferenceError: VUE_APP_CONFIG 未定义 Jest 单元测试失败 - ReferenceError: VUE_APP_CONFIG is not defined Jest unit tests failing Jest TypeScript 测试在 Jenkins 中失败 - Jest TypeScript tests failing in Jenkins 如何在 jest 单元测试中使用 vue3 + typescript 在 shallowMount() 中添加假商店? - How to add fake store in shallowMount() using vue3 + typescript in jest unit tests? 使用Jest测试Typescript,如何测试内部函数? 我的导入/导出使我失败 - Testing Typescript with Jest, how do I test an internal function? My import/export is failing me Vue3 组合 API 单元测试未能捕获发射/事件/渲染 - Vue3 Composition API unit tests failing to catch emits/events/renders 单元测试期间 Jest 的问题 - 未实现 getContext() - Issue with Jest during the unit tests - getContext() is not implemented Jest单元测试中的Angular Circular Dependency问题 - Angular Circular Dependency issue in Jest unit tests Angular2 CLI Karma单元测试失败 - Angular2 CLI Karma Unit Tests Failing Angular 单元测试因未定义/无提供程序而失败 - Angular Unit tests failing with undefined / no provider Mocking vue-router 在 Vue 3 的 Jest 测试中的 useRoute() - Mocking vue-router's useRoute() in Jest tests in Vue 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM