首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试Konva时,"SyntaxError:不能使用模块外的导入语句“

测试Konva时,"SyntaxError:不能使用模块外的导入语句“
EN

Stack Overflow用户
提问于 2022-03-12 09:36:57
回答 1查看 504关注 0票数 0

我用konva.js和打字本写了一个国际象棋游戏。

现在我想用test测试它,但是在导入Konva时会发生错误。

这是错误信息。

我知道这可能是由esModule编译器引起的。

我曾尝试在transformIgnorePatterns中添加konva,但没有工作。

或者我应该嘲笑_mock_的konva。

我怎么解决的,谢谢大家。

代码语言:javascript
复制
    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript     
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    D:\code\chess\chess\node_modules\konva\lib\index-node.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { Konva } from './_FullInternals.js';
EN

回答 1

Stack Overflow用户

发布于 2022-03-13 02:42:09

这是我和jest.config.js一起工作的ts-jest

代码语言:javascript
复制
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'jsdom',
  moduleNameMapper: {
    '^konva': 'konva/konva',
  },
  globals: {
    'ts-jest': {
      diagnostics: false,
      useESM: true,
    },
  },
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71448556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档