节点模块没有被正确解析以进行测试?
Posted
技术标签:
【中文标题】节点模块没有被正确解析以进行测试?【英文标题】:Node modules not being parsed correctly for tests? 【发布时间】:2019-04-23 04:17:30 【问题描述】:我在一个应用程序中工作,并试图添加一个测试框架来自动化整个应用程序的测试。我正在使用 Vue、Electron、TypeScript 和 Node,并且无法获得任何实际使用组件来运行的测试。出于某种原因,Jest 似乎不想使用节点模块......我几乎没有运气在网上找到任何可以解决此问题的方法。
任何对此主题有任何了解的人,如果对如何解决在运行测试时导入这些节点模块的问题有任何指示或想法,我们将不胜感激。我无法想象这是不可能做到的,但我通过搜索确实找不到太多东西。
错误:
$ 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'
test.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",
【问题讨论】:
【参考方案1】:修复此特定错误
SyntaxError: 意外的令牌导入
我使用以下 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 config documentation、jest config documentation 和tsconfig documentation 会产生奇迹。
【讨论】:
以上是关于节点模块没有被正确解析以进行测试?的主要内容,如果未能解决你的问题,请参考以下文章
从 Nuxt.js 与 PhpStorm 中的节点模块解析 SCSS 中的 @import .css