节点模块未正确解析以进行测试?

问题描述 投票:0回答:2

我正在开发一个应用程序,并尝试添加一个测试框架来自动化整个应用程序的测试。我正在使用 Vue、Electron、TypeScript 和 Node,但在获取实际使用组件运行的任何测试时遇到问题。由于某种原因,Jest 似乎不想使用节点模块...我在网上找不到任何可以解决此问题的方法。

任何对此主题有任何了解的人,如果对如何解决运行测试时导入这些节点模块的问题有任何指示或想法,我们将不胜感激。我无法想象这是不可能做到的,但通过我的搜索我确实找不到太多东西。

错误:

$ npm run test

> [email protected] test C:\Users\daniel\Desktop\MDC\mdc
> jest --detectOpenHandles

FAIL src/app/features/mdc-window/mdc-window.component.test.ts
  ● Test suite failed to run

    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 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/en/configuration.html

    Details:

    C:\Users\daniel\Desktop\MDC\mdc\node_modules\bootstrap-vue\es\components\modal\modal.js:3
    import bBtn from '../button/button';
    ^^^^^^

    SyntaxError: Unexpected token import

    > 1 | import bModal from 'bootstrap-vue/es/components/modal/modal'
        | ^
      2 | import bTooltip from 'bootstrap-vue/es/components/tooltip/tooltip'
      3 | import throttle from 'lodash.throttle'
      4 | import Vue from 'vue'

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
      at Object.<anonymous> (src/app/features/mdc-window/mdc-window.component.ts:1:1)
      at Object.<anonymous> (src/app/features/mdc-window/mdc-window.component.test.ts:3:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        9.13s
Ran all test suites.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] test: `jest --detectOpenHandles`
npm ERR! Exit status 1

jest.config.js

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  verbose: true,
  testPathIgnorePatterns: [
    '/build/',
    '/config/',
    '/data/',
    '/dist/',
    '/node_modules/',
    '/test/',
    '/vendor/'
  ],
  globals: {
    'ts-jest': {
      tsConfig: './src/app/tsconfig.json'
    }
  }
}

测试.ts

import { expect } from 'chai'
import 'jest'
import { MDCWindowComponent } from './mdc-window.component'

const mdcWindowComponent = new MDCWindowComponent()

describe('Test: Set Title Function', () => {
  it('should set the variable title to the passed variable', () => {
    const title = 'this is a test'
    mdcWindowComponent.setTitle(title)
    expect(mdcWindowComponent.title).to.equal(title)
  })

tsconfig

{
  "compilerOptions": {
    "allowJs": true,
    "outDir": "./built/",
    "sourceMap": true,
    "strict": true,
    "moduleResolution": "node",
    "target": "ES2017",
    "experimentalDecorators": true,
    "noImplicitAny": false,
    "emitDecoratorMetadata": true,
    "allowSyntheticDefaultImports": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "esModuleInterop": true,
    "lib": ["es2017", "dom"]
  },
  "include": [
    "**/*",
    "../types/**/*",
    "../../node_modules/bootstrap-vue/**/*",
    "../../node_modules/electron/**/*"
  ]
}

相关套餐

"@types/jest": "^23.3.9",
"jest": "^23.6.0",
"ts-jest": "^23.10.4",
javascript node.js typescript automated-tests jestjs
2个回答
4
投票

修复此特定错误

语法错误:意外的令牌导入

我使用以下 jest.config.js 修复了此问题

module.exports = {
  preset: 'ts-jest/presets/js-with-ts',
  testEnvironment: 'node',
  verbose: true,
  moduleDirectories: ['node_modules', 'src'],
  modulePaths: ['<rootDir>/src', '<rootDir>/node_modules'],
  moduleFileExtensions: ['js', 'ts', 'json', 'node'],
  transformIgnorePatterns: ['node_modules/(?!(bootstrap-vue)/)'],
  testPathIgnorePatterns: [
    '/build/',
    '/config/',
    '/data/',
    '/dist/',
    '/node_modules/',
    '/test/',
    '/vendor/'
  ],
  globals: {
    'ts-jest': {
      tsConfig: './src/app/tsconfig.json'
    },
    NODE_ENV: 'test'
  }
}

注意

预设:'ts-jest/presets/js-with-ts',

transformIgnorePatterns: ['node_modules/(?!(bootstrap-vue)/)'],

这似乎有助于解决问题,但如果您将此答案中的 jest.config.js 与我的回复进行比较,我确实添加了一些其他选项,这可能对其他人也有帮助。

大量阅读 ts-jest 配置文档jest 配置文档tsconfig 文档会产生奇迹。


0
投票

丹尼尔斯图西奇的回答甚至不起作用。现在我根本无法测试我的组件?

© www.soinside.com 2019 - 2024. All rights reserved.