代码之家  ›  专栏  ›  技术社区  ›  Daniel Turcich

没有为测试正确分析节点模块?

  •  2
  • Daniel Turcich  · 技术社区  · 6 年前

    我在一个应用程序中工作,并试图添加一个测试框架来自动测试整个应用程序。我正在使用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.CONT.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",
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Daniel Turcich    6 年前

    修复此特定错误

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

    我使用以下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:'节点模块/(?!(bootstrap vue)/)'],

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

    一个健康的阅读量 ts-jest config documentation ,请 jest config documentation ,和 tsconfig documentation 创造奇迹。