javascript - Node modules not being parsed correctly for tests? - Stack Overflow

I'm working in an application and am trying to add a testing framework to automate testing across

I'm working in an application and am trying to add a testing framework to automate testing across the app. I'm using Vue, Electron, TypeScript, and Node and am having trouble getting any test that actually uses ponents to run. For some reason Jest doesn't seem to want to work with the node modules... I have had very little luck finding anything online that points to ways to fix this.

Anyone with any knowledge on the subject who has any pointers or ideas on how to resolve the issue with importing these node modules while running the tests would be greatly appreciated. I can't imagine that this is impossible to do but there really isn't much I can find through my searches.

Error:

$ npm run test

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

FAIL src/app/features/mdc-window/mdc-windowponent.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:
    .html

    Details:

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

    SyntaxError: Unexpected token import

    > 1 | import bModal from 'bootstrap-vue/es/ponents/modal/modal'
        | ^
      2 | import bTooltip from 'bootstrap-vue/es/ponents/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-windowponent.ts:1:1)
      at Object.<anonymous> (src/app/features/mdc-window/mdc-windowponent.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'
    }
  }
}

test.ts

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

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

{
  "pilerOptions": {
    "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/**/*"
  ]
}

relevant packages

"@types/jest": "^23.3.9",
"jest": "^23.6.0",
"ts-jest": "^23.10.4",

I'm working in an application and am trying to add a testing framework to automate testing across the app. I'm using Vue, Electron, TypeScript, and Node and am having trouble getting any test that actually uses ponents to run. For some reason Jest doesn't seem to want to work with the node modules... I have had very little luck finding anything online that points to ways to fix this.

Anyone with any knowledge on the subject who has any pointers or ideas on how to resolve the issue with importing these node modules while running the tests would be greatly appreciated. I can't imagine that this is impossible to do but there really isn't much I can find through my searches.

Error:

$ npm run test

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

FAIL src/app/features/mdc-window/mdc-window.ponent.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\ponents\modal\modal.js:3
    import bBtn from '../button/button';
    ^^^^^^

    SyntaxError: Unexpected token import

    > 1 | import bModal from 'bootstrap-vue/es/ponents/modal/modal'
        | ^
      2 | import bTooltip from 'bootstrap-vue/es/ponents/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.ponent.ts:1:1)
      at Object.<anonymous> (src/app/features/mdc-window/mdc-window.ponent.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'
    }
  }
}

test.ts

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

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

{
  "pilerOptions": {
    "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/**/*"
  ]
}

relevant packages

"@types/jest": "^23.3.9",
"jest": "^23.6.0",
"ts-jest": "^23.10.4",
Share Improve this question edited Nov 20, 2018 at 16:59 Daniel Turcich asked Nov 20, 2018 at 16:50 Daniel TurcichDaniel Turcich 1,8443 gold badges27 silver badges48 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

To fix this specific error

SyntaxError: Unexpected token import

I fixed this using the following 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'
  }
}

Note

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

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

this seemingly was what helped fix the issue, though if you pare the jest.config.js in this answer to my response i did add a few other options which may also be helpful for others.

A healthy amount of reading on the ts-jest config documentation, jest config documentation, and tsconfig documentation does wonders.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745304575a4621633.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信