gpt4 book ai didi

javascript - Node 模块没有被正确解析以进行测试?

转载 作者:搜寻专家 更新时间:2023-10-30 21:07:58 25 4
gpt4 key购买 nike

我在一个应用程序中工作,我正在尝试添加一个测试框架以在整个应用程序中自动进行测试。我正在使用 Vue、Electron、TypeScript 和 Node,但无法获得任何实际使用组件运行的测试。出于某种原因,Jest 似乎不想使用 Node 模块......我在网上找到任何指向解决此问题的方法的运气都很差。

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

错误:

$ npm run test

> mdc@1.0.0 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! mdc@1.0.0 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)
})

配置

{
"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",

最佳答案

修复这个特定的错误

SyntaxError: Unexpected token import

我使用以下 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'
}
}

注意

preset: 'ts-jest/presets/js-with-ts',

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

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

ts-jest config documentation 上保持健康的阅读量, jest config documentation , 和 tsconfig documentation创造奇迹。

关于javascript - Node 模块没有被正确解析以进行测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53397773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com