标签: babel-jest

加载配置时出错 - 您似乎正在使用本机 ECMAScript 模块配置文件 (Jest)

更新。

当我发出拉取请求时出现此错误。有一个 github 工作流审计,它对拉取请求运行检查,并从另一个存储库加载测试文件。

- name: Run Audits
      run: npx jest audits/ch-2 --json --outputFile=audits/ch-2.json --noStackTrace


Test suite failed to run

    /Users/frankukachukwu/StudioProjects/covid-19-estimator-tksilicon-js/babel.config.js: Error while loading config - You appear to be using a native ECMAScript module configuration file, which is only supported when running Babel asynchronously.
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

node.js express jestjs babeljs babel-jest

19
推荐指数
2
解决办法
7554
查看次数

使用 Jest 测试 ES6 类会引发“不是构造函数”错误

我在这里发现了类似的问题,但似乎没有答案。

我正在尝试使用 Jest 测试 ES6 类,如下所示:

// src/myclass.js
export default class MyClass {
    constructor(options) {
        // currently this is empty while I debug this problem
    }
}
Run Code Online (Sandbox Code Playgroud)

和测试:

// test/myclass.test.js
import { MyClass } from '../src/myclass.js';

describe("Test Constructor", () => {

    test("doesn't throw error when constructed", async () => {
        expect(() => {
            const testMyClass = new MyClass();
        }).not.toThrowError();
    }

});
Run Code Online (Sandbox Code Playgroud)

当我运行测试时,Jest 抛出一个错误说:

类型错误:_myClass.MyClass 不是构造函数

我最好的猜测是这是 babel 配置的问题,但我似乎无法弄清楚。如果我更改MyClass为函数而不是类并删除导出/导入(即类前的做事方式),那么它会按预期工作。

这是我在 package.json 中的配置:

"devDependencies": {
    "@babel/core": "^7.1.2",
    "@babel/preset-env": …
Run Code Online (Sandbox Code Playgroud)

javascript jestjs babeljs babel-jest es6-class

18
推荐指数
2
解决办法
2万
查看次数

运行Jest测试时出现意外的令牌'import'错误?

我已经多次问过这个问题,但我遇到的所有解决方案似乎都不适合我.我在尝试为Vue应用程序运行Jest测试时遇到以下错误.

Jest encountered an unexpected token

This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

Here's what you can do:
 • 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 …
Run Code Online (Sandbox Code Playgroud)

configuration vue.js jestjs babel-jest

16
推荐指数
2
解决办法
2万
查看次数

console.error 错误:无法解析 CSS 样式表

使用jest运行测试时出现以下错误。

  console.error
    Error: Could not parse CSS stylesheet
        at exports.createStylesheet ([PERSONAL_PATH]/node_modules/jsdom/lib/jsdom/living/helpers/stylesheets.js:34:21)
        at HTMLStyleElementImpl._updateAStyleBlock ([PERSONAL_PATH]/node_modules/jsdom/lib/jsdom/living/nodes/HTMLStyleElement-impl.js:68:5)
        at HTMLStyleElementImpl._childTextContentChangeSteps ([PERSONAL_PATH]/node_modules/jsdom/lib/jsdom/living/nodes/HTMLStyleElement-impl.js:36:12)
        at HTMLStyleElementImpl._insert ([PERSONAL_PATH]/node_modules/jsdom/lib/jsdom/living/nodes/Node-impl.js:832:14)
        at HTMLStyleElementImpl._preInsert ([PERSONAL_PATH]/node_modules/jsdom/lib/jsdom/living/nodes/Node-impl.js:768:10)
        at HTMLStyleElementImpl._append ([PERSONAL_PATH]/node_modules/jsdom/lib/jsdom/living/nodes/Node-impl.js:868:17)
        at HTMLStyleElementImpl.appendChild ([PERSONAL_PATH]/node_modules/jsdom/lib/jsdom/living/nodes/Node-impl.js:610:17)
        at HTMLStyleElement.appendChild ([PERSONAL_PATH]/node_modules/jsdom/lib/jsdom/living/generated/Node.js:395:60)
        at StyleSheet.insert ([PERSONAL_PATH]/node_modules/@emotion/sheet/dist/sheet.cjs.dev.js:121:11)
        at Object.insert ([PERSONAL_PATH]/node_modules/@emotion/cache/dist/cache.cjs.dev.js:168:19) {width:-webkit-fit-content;width:-moz-fit-content;width:fit-content;}/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ[BLOB]== */

      at VirtualConsole.<anonymous> (node_modules/jsdom/lib/jsdom/virtual-console.js:29:45)
      at exports.createStylesheet (node_modules/jsdom/lib/jsdom/living/helpers/stylesheets.js:38:63)
      at HTMLStyleElementImpl._updateAStyleBlock (node_modules/jsdom/lib/jsdom/living/nodes/HTMLStyleElement-impl.js:68:5)
      at HTMLStyleElementImpl._childTextContentChangeSteps (node_modules/jsdom/lib/jsdom/living/nodes/HTMLStyleElement-impl.js:36:12)
      at HTMLStyleElementImpl._insert (node_modules/jsdom/lib/jsdom/living/nodes/Node-impl.js:832:14)
      at HTMLStyleElementImpl._preInsert (node_modules/jsdom/lib/jsdom/living/nodes/Node-impl.js:768:10)
Run Code Online (Sandbox Code Playgroud)

我尝试在packages.json上设置jsdom版本,还尝试更新jest包。没有改变错误。它实际上并没有导致测试失败,但它污染了笑话输出

如果需要更多详细信息,请在评论中询问,而不是我编辑此问题。

编辑

这是测试组件的 sass 文件:

$spacing-medium: 2.4rem
$spacing-small: 1.6rem
$color-green: #3aa537 …
Run Code Online (Sandbox Code Playgroud)

javascript css sass jestjs babel-jest

16
推荐指数
3
解决办法
2万
查看次数

Next.js 和 Jest:语法错误:无法在模块外部使用 import 语句

我正在使用 TypeScript 开发 Next.js 项目,并使用 Jest 和 React 测试库进行测试。但是,我遇到 SyntaxError : Cannot use import statements external a module for elements where I import rehype-raw

\n

据我了解,Jest不支持ES6,因此node_modules可能需要转换。这可以使用配置transformIgnorePatterns。例如,如果rehype-raw导致此错误,"transformIgnorePatterns": ["node_modules/(?!rehype-raw)/"]则应允许转换rehype-raw但不允许转换其他模块。从而解决这个错误。

\n

但是,这对我不起作用。但我不知道为什么以及如何解决这个问题。我发现没有建议的解决方案可以解决这个问题。我在下面附上了我的错误输出jest.config.jsbabel.rc文件。

\n

错误输出

\n
 FAIL  test/pages/companies/[id].test.tsx                  \n  \xe2\x97\x8f Test suite failed to run\n\n    Jest encountered an unexpected token\n\n    [...]\n\n    Details:\n\n    /path/frontend-job/node_modules/rehype-raw/index.js:7\n    import {raw} from 'hast-util-raw'\n    ^^^^^^\n\n    SyntaxError: Cannot use import statement outside a module\n\n      3 | import …
Run Code Online (Sandbox Code Playgroud)

javascript typescript jestjs babel-jest next.js

16
推荐指数
2
解决办法
1万
查看次数

React JS Jest导致"SyntaxError:Unexpected token".

我现在使用反应JEST来测试代码.如果一个组件是单个的,而不是导入任何其他组件,"npm test"运行顺利,现在,我想要一起测试多个组件,我立即得到了这个错误:

SyntaxError: Unexpected token .
Run Code Online (Sandbox Code Playgroud)

似乎只要反应是导入别的东西,比如这个:

require( './style/fixed-data-table.css' );
require( './style/jnpr-datatable.scss' );
Run Code Online (Sandbox Code Playgroud)

然后使用jest抛出意外的标记"." 错误.

我的设置一定有问题,但在哪里?我的Package.json包含:

 "jest": {
   "unmockedModulePathPatterns": [
     "<rootDir>/node_modules/react/",
     "<rootDir>/node_modules/react-dom/",
     "<rootDir>/node_modules/react-addons-test-utils/"
   ]
 }
Run Code Online (Sandbox Code Playgroud)

而.babelrc已经在根目录中了.还包括babel-jest.谢谢

reactjs jestjs babel-jest

15
推荐指数
2
解决办法
8397
查看次数

Jest 在使用 React TypeScript 时遇到意外标记

我正在构建一个可重用的 React 组件,而不使用 React-App,而且我对 Jest 很陌生。我不断收到这个消息。我在 Stackoverflow 上尝试了几种帖子解决方案,但目前陷入困境:

\n

\xe2\x97\x8f 测试套件运行失败

\n
Jest encountered an unexpected token\n\nThis usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.\n\nBy default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".\n\nHere's what you can do:\n \xe2\x80\xa2 If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.\n \xe2\x80\xa2 To have some of your "node_modules" …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs jestjs babel-jest

15
推荐指数
1
解决办法
3万
查看次数

.flat 不仅仅是开玩笑的功能

当我开玩笑地运行我的测试时,我遇到了上述错误;

Error: Uncaught [TypeError: array.map(...).flat is not a function]
Run Code Online (Sandbox Code Playgroud)

按照该问题的解决方案,https://github.com/kulshekhar/ts-jest/issues/828

我已经安装core-js了依赖项并将其放入jest.config.js

setupFiles: ['core-js'],
Run Code Online (Sandbox Code Playgroud)

我会收到另一个错误;

Error: Uncaught [Error: Not supported]
Run Code Online (Sandbox Code Playgroud)

这只会发生在玩笑中,我在我的应用程序和故事书上使用 babel 和 webpack,在flat.

我的 jest.config.js

const PATH = require('./path')

module.exports = {
  setupFilesAfterEnv: ['./rtl.setup.js'],
  moduleFileExtensions: ['js'],
  verbose: true,
  moduleNameMapper: {
    '@components(.*)$': `${PATH.source}/components/$1`
  },
  transform: {
    '^.+\\.js$': 'babel-jest'
  }
}
Run Code Online (Sandbox Code Playgroud)

jestjs babeljs babel-jest babel-polyfill core-js

13
推荐指数
3
解决办法
7529
查看次数

从node_modules组件导入Jest意外的令牌; 巴贝尔未能跑?

我正在试图弄清楚如何让Jest在我的环境中工作,而我遇到的问题是这个项目在node_modules的子目录中有一堆自定义组件.

我收到这个错误:



    FAIL  src/mantle/tools/searchindexer/apps/DataMover/js/components/__test__/GenericJobsTable.test.jsx
      ? Test suite failed to run

        /Users/rob/repos/mesa/ui/node_modules/iggy-common/components/IggyTable.jsx:1
        ({"Object.":function(module,exports,require,__dirname,__filename,global,jest){import React, {PropTypes} from "react";
                                                                                                 ^^^^^^
        SyntaxError: Unexpected token import

          at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:289:17)
          at Object. (src/mantle/tools/searchindexer/apps/DataMover/js/components/JobsTable/GenericJobsTable.jsx:7:18)
          at Object. (src/mantle/tools/searchindexer/apps/DataMover/js/components/__test__/GenericJobsTable.test.jsx:5:25)

    Test Suites: 1 failed, 1 total
    Tests:       0 total
    Snapshots:   0 total
    Time:        2.4s
    Ran all test suites matching "GenericJobsTable".

Run Code Online (Sandbox Code Playgroud)

我运行jest ^20.0.3,并babel-jest ^20.0.3在7.7.1的NodeJS.

在我的package.json中,这是我拥有的jest配置部分:



      "jest": {
        "verbose": true,
        "transform": {
          "^.+\\.jsx$": "babel-jest"
        },
        "moduleFileExtensions": [
          "js",
          "jsx"
        ],
        "moduleDirectories": [
          "node_modules"
        ]
      }

Run Code Online (Sandbox Code Playgroud)

我将我的根.babelrc定义为: …

jestjs babel-jest

12
推荐指数
1
解决办法
7260
查看次数

Jest transformIgnorePatterns无效

我花了很长时间查看有关此问题的其他问题并查看Github上的其他项目,但似乎没有任何答案对我有用.

我正在我的项目中加载第三方库,当运行Jest测试时,我收到错误

export default portalCommunication;
^^^^^^

SyntaxError: Unexpected token export

> 1 | import portalCommunication from 'mathletics-portal-communication-service';
Run Code Online (Sandbox Code Playgroud)

我已尝试以多种方式更新我的Jest配置以使其转换此库但我总是得到相同的错误.

这是我当前的jest.config.js文件:

module.exports = {
    moduleNameMapper: {
        '\\.(css|scss)$': 'identity-obj-proxy',
        '\\.svg$': '<rootDir>/test/mocks/svg-mock.js'
    },
    setupFiles: ['./test/test-setup.js'],
    transformIgnorePatterns: [
        '<rootDir>/node_modules/(?!mathletics-portal-communication-service)'
    ]
};
Run Code Online (Sandbox Code Playgroud)

我还尝试添加transform属性来对这个mathletics-portal-communication-service目录运行babel-jest.

请帮忙!

javascript reactjs jest babel-jest

12
推荐指数
4
解决办法
3435
查看次数