- 报错如下
- 原因 (来自于转载)
大意是jest的28以后的版本运行环境支持的是浏览器的export而不是default export,
但是sinon支持的是ESM模块而不是CommonJS模块,
Jest最开始就不支持ESM模块, 这就导致Jest编译Export的时候出错了
- 解决方案
在jest.config.js中配置对应的resolver
下面是resolver.ts的内容( 注意,这里也对我另外一个库uuid 进行了处理, 可以忽略)
module.exports = (path, options) => {
// Call the defaultResolver, so we leverage its cache, error handling, etc.
return options.defaultResolver(path, {
...options,
// Use packageFilter to process parsed `package.json` before the resolution (see https://www.npmjs.com/package/resolve#resolveid-opts-cb)
packageFilter: (pkg) => {
// This is a workaround for https://github.com/uuidjs/uuid/pull/616
//
// jest-environment-jsdom 28+ tries to use browser exports instead of default exports,
// but uuid and sinon only offers an ESM browser export and not a CommonJS one. Jest does not yet
// support ESM modules natively, so this causes a Jest error related to trying to parse
// "export" syntax.
//
// This workaround prevents Jest from considering uuid's and sinon's module-based exports at all;
// it falls back to uuid's and sinon's CommonJS+node "main" property.
//
// Once we're able to migrate our Jest config to ESM and a browser crypto
// implementation is available for the browser+ESM version of uuid and sinon to use (eg, via
// https://github.com/jsdom/jsdom/pull/3352 or a similar polyfill), this can go away.
if ( pkg.name === "sinon") {
delete pkg["exports"];
delete pkg["module"];
}
return pkg;
},
});
};
转载自fix: add `browser` condition to package.json by SimenB · Pull Request #616 · uuidjs/uuid · GitHub