Skip to content

README: Suggest asset mocks #6169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Conversation

raineorshine
Copy link
Contributor

@raineorshine raineorshine commented Dec 24, 2020

What kind of change does this PR introduce? (check at least one)

  • Bugfix
  • Feature
  • Code style update
  • Refactor
  • Docs
  • Underlying tools
  • Other, please describe:

Does this PR introduce a breaking change? (check one)

  • Yes
  • No

Other information:

Using Vue v3 and vue add unit-jest I got SyntaxError: Invalid or unexpected token on an imported gif. Adding asset mocks using moduleNameMapper (StackOverflow) fixed it. Let me know if a different approach to this problem is recommended.

  moduleNameMapper: {
    '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
      '<rootDir>/src/__mock__/file-mock.js',
    '\\.(css|less)$': '<rootDir>/src/__mock__/style-mock.js',
  },

I just noticed it's also possible with a transform via jest-transform-stub. Not sure which is preferable.

FYI My dependency versions:

    "@vue/cli-plugin-babel": "~4.5.9",
    "@vue/cli-plugin-eslint": "~4.5.9",
    "@vue/cli-plugin-unit-jest": "^4.5.9",
    "@vue/cli-service": "~4.5.9",
    "@vue/compiler-sfc": "^3.0.4",
    "@vue/test-utils": "^2.0.0-0",
    "babel-eslint": "^10.1.0",
    "vue-jest": "^5.0.0-0",

@raineorshine
Copy link
Contributor Author

raineorshine commented Dec 24, 2020

I can also add this to the vue-cli documentation if desired. Looks to be the same content but not sure if they are kept in sync in an automated fashion.

@haoqunjiang
Copy link
Member

haoqunjiang commented Dec 25, 2020

Thanks for the suggestion!

But I think there's a better place to fix this issue:

'.+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$':

jest-transform-stub is already configured in the default preset. The problem is that the gif extension was missed out. Fixing the preset can silently solve the problem for everyone without making them aware of the underlying configuration.

raineorshine added a commit to raineorshine/vue-cli that referenced this pull request Dec 25, 2020
Existing:
- css
- styl
- less
- sass
- scss
- svg
- png
- jpg
- ttf
- woff
- woff2

Added:
- jpeg
- gif
- eot
- otf
- webp
- mp4
- webm
- wav
- mp3
- m4a
- aac
- oga
@raineorshine
Copy link
Contributor Author

Ah, I wasn't aware that jest-transform-stub was already included! Good call. Proposed in #6170.

haoqunjiang pushed a commit that referenced this pull request Dec 25, 2020
Existing:
- css
- styl
- less
- sass
- scss
- svg
- png
- jpg
- ttf
- woff
- woff2

Added:
- jpeg
- gif
- eot
- otf
- webp
- mp4
- webm
- wav
- mp3
- m4a
- aac
- oga
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants