question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unit testing failure with Jest?

See original GitHub issue

My test suite will fail to run if I’m trying to test a component which uses vuepdf. This is with Jest on a project set up with vue-cli:

` ● Test suite failed to run

C:\frontend\uc-front\node_modules\vue-pdf\src\vuePdfNoSss.vue:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){<style src="./annotationLayer.css"></style>

SyntaxError: Unexpected token <`

I don’t get this error if I don’t import vue-pdf in the component.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
mobseancommented, Aug 5, 2021

Came across the exact same problem. My solution:

adding to my jest.config.js:

const esModules = ['vue-pdf', 'vue-resize-sensor'].join('|')

module.exports = {
  ....
  transformIgnorePatterns: [`<rootDir>/node_modules/(?!(${esModules}))`]
}

1reaction
Kelhencommented, Sep 11, 2018

made it work by adding vue-pdf and vue-resize-sensor to my jest config like following

jest.config.js

transformIgnorePatterns: [
    '<rootDir>/node_modules/(?!vue-pdf/|vue-resize-sensor/)'
  ]

Then also in my my jest config I added a mocks for the worker like explained here https://github.com/wojtekmaj/react-pdf/issues/67#issuecomment-372118170

jest.config.js

  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/src/$1',
    '.+\\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$': 'identity-obj-proxy',
    '\\.worker.js': '<rootDir>/__mocks__/workerMock.js'
  }

create the following file ‘__mocks__/workerMock.js’ in you project and make the path above match it

workerMock.js

module.exports = Object.create(null);
Read more comments on GitHub >

github_iconTop Results From Across the Web

In Jest, how can I make a test fail? - Stack Overflow
Method-1. You can wrap your promise function within expect and tell jest the function should reject with the given error. If the someOperation() ......
Read more >
Troubleshooting - Jest
Use this guide to resolve issues with Jest. Tests are Failing and You Don't Know Why​. Try using the debugging support built into...
Read more >
Jest explicitly or arbitrarily force fail() a test - Code with Hugo
Output of the test run shows that if the code doens't throw, the test suite will fail, which is desired behaviour: FAIL src/fail-throws- ......
Read more >
Successfully Throwing Async Errors with the Jest Testing Library
My mission now, was to unit test that when validateUploadedFile() threw an error due to some invalid import data, the setUploadError() function ...
Read more >
How to find the root cause of a failing test if Jest doesn't tell ...
In our case, there's an option called --detectOpenHandles that solves our problem. By using this command, Jest should print the actual exception ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found