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.

Jest HelloWorld Test Fails Out of the Box

See original GitHub issue

Version

3.0.0-beta.9

Reproduction link

https://github.com/rgant/vue-helloworld-test-fails

Steps to reproduce

Create new Vue project with Jest unit tests. Run jest.

What is expected?

Initial HelloWorld component test pass

What is actually happening?

Test fails


euphorbus:~/Programming/vue (master)$ vue create tmp2


Vue CLI v3.0.0-beta.9
? Please pick a preset: Manually select features
? Check the features needed for your project: Unit
? Pick a unit testing solution: Jest
? Where do you prefer placing config for Babel, PostCSS, ESLint, etc.? In dedicated config files
? Save this as a preset for future projects? No


Vue CLI v3.0.0-beta.9
โœจ  Creating project in /Users/rgant/Programming/vue/tmp2.
โš™  Installing CLI plugins. This might take a while...

yarn install v1.6.0
(node:9224) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
info No lockfile found.
[1/4] ๐Ÿ”  Resolving packages...
[2/4] ๐Ÿšš  Fetching packages...
[3/4] ๐Ÿ”—  Linking dependencies...
[4/4] ๐Ÿ“ƒ  Building fresh packages...
success Saved lockfile.
โœจ  Done in 19.86s.

๐Ÿš€  Invoking generators...
๐Ÿ“ฆ  Installing additional dependencies...

yarn install v1.6.0
(node:9230) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
[1/4] ๐Ÿ”  Resolving packages...
[2/4] ๐Ÿšš  Fetching packages...
[3/4] ๐Ÿ”—  Linking dependencies...

success Saved lockfile.
โœจ  Done in 4.39s.

โš“  Running completion hooks...
โ  
๐ŸŽ‰  Successfully created project tmp2.
๐Ÿ‘‰  Get started with the following commands:

 $ cd tmp2
 $ yarn serve

euphorbus:~/Programming/vue (master)$ cd tm
tmp/  tmp2/ 
euphorbus:~/Programming/vue (master)$ cd tmp2
euphorbus:~/Programming/vue/tmp2 (master)$ ls
jest.config.js node_modules   package.json   public         src            tests          yarn.lock
euphorbus:~/Programming/vue/tmp2 (master)$ ls tests/unit/HelloWorld.spec.js 
tests/unit/HelloWorld.spec.js
euphorbus:~/Programming/vue/tmp2 (master)$ cat tests/unit/HelloWorld.spec.js 
import { shallow } from '@vue/test-utils'
import HelloWorld from '@/components/HelloWorld.vue'

describe('HelloWorld.vue', () => {
  it('renders props.msg when passed', () => {
    const msg = 'new message'
    const wrapper = shallow(HelloWorld, {
      propsData: { msg }
    })
    expect(wrapper.text()).toMatch(msg)
  })
})
euphorbus:~/Programming/vue/tmp2 (master)$ which jest
node_modules/.bin/jest
euphorbus:~/Programming/vue/tmp2 (master)$ jest
 FAIL  tests/unit/HelloWorld.spec.js
  โ— Test suite failed to run

    /Users/rgant/Programming/vue/tmp2/tests/unit/HelloWorld.spec.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { shallow } from '@vue/test-utils';
                                                                                                    ^
    
    SyntaxError: Unexpected token {
      
      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:316:17)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.783s
Ran all test suites.
euphorbus:~/Programming/vue/tmp2 (master)$ cat package.json 
{
  "name": "tmp2",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve --open",
    "build": "vue-cli-service build",
    "test": "vue-cli-service test"
  },
  "dependencies": {
    "vue": "^2.5.16"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "^3.0.0-beta.9",
    "@vue/cli-plugin-unit-jest": "^3.0.0-beta.9",
    "@vue/cli-service": "^3.0.0-beta.9",
    "@vue/test-utils": "^1.0.0-beta.10",
    "babel-core": "^7.0.0-0",
    "babel-jest": "^22.0.4",
    "vue-template-compiler": "^2.5.13"
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ]

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:17 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
wolfeidaucommented, May 23, 2018

I fixed this by adding the following fragment to jest.config.js as per https://github.com/kidd3/SL/pull/4.

  "transformIgnorePatterns": [
    "node_modules/(?!jest-test)" + "node_modules/(?!(jest-test))"
  ],

Would it be worth doing a PR for this?

1reaction
wolfeidaucommented, May 23, 2018

@LinusBorg totally, would need some investigation. My initial understanding is that it is associated with how jest and babel work together. https://github.com/thymikee/jest-preset-angular/issues/66#issuecomment-323696947 has a note on this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest: mocking console.error - tests fails - Stack Overflow
The first test runs successfully. ยท The second test fails, complaining that the mock function was called zero times. ยท If I swap...
Read more >
Getting Started - Jest
Let's get started by writing a test for a hypothetical function ... is an opinionated tool and does offer some out-of-the box workflows....
Read more >
Jest Tutorial - JavaScript Unit Testing Using Jest Framework
In this Jest tutorial, we will learn about various Jest features, Jest matchers and how to use Jest framework for JavaScript Unit Testing....
Read more >
Jest testing: Top features and how to use them - LogRocket Blog
Two more useful options are a to run all the tests and f to run the failed tests again. Out-of-the-box ease. Another thing...
Read more >
Build and Test Modal using React features and DOM events ...
However, the last test will fail even if the specification is sound and application works properly. This is because when Jest runs its...
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