Skip to content
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

Docs: Troubleshoot import statement #2812

Merged
merged 3 commits into from
Aug 10, 2021

Conversation

greenbourne277
Copy link
Contributor

Summary

When setting up a project with @vue/cli (https://github.com/vuejs/vue-cli) with version 4.5.13 with ts-jest I faced the problem that the test could not be executed for my component because one of the modules doesn't need to be transpiled.
I found this answer after searching for a long time on the web and therefore thought it should be added to the documentation of the project.

Test plan

There are no changes in the code. In can be tested as follows:

  1. Setup a vue project with vue-cli: vue create project
  2. Select Vue2.x, typescript, jest and babel
  3. Add the vue-class-component package via npm install --save vue vue-class-component
  4. Adapt the HelloWorld.vue component:
<script lang="ts">
import Vue from "vue";
import Component from "../../node_modules/vue-class-component/lib";

@Component
export default class HelloWorld extends Vue {
  name = "HelloWorld"
};
</script>
  1. Run the test via npm run test:unit and verify the error message.
  2. To fix this issue adapt the jest.config.js as following:
module.exports = {
  preset: "@vue/cli-plugin-unit-jest/presets/typescript-and-babel",
  transformIgnorePatterns: ["node_modules/(?!(vue-class-component))"]
};

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

TROUBLESHOOTING.md Show resolved Hide resolved
SyntaxError: Cannot use import statement outside a module

### SOLUTION
One of the node modules that is used doesn't need to be transpiled.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls put a blank line between ### SOLUTION and the description below


```shell
SyntaxError: Cannot use import statement outside a module
20 | <script lang="ts">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would strip out the stuffs related to Vue, in this case this line 54, 55

```
In this case **some-module** is the problem.

This module needs to be ignored in the transformation process. Add the following to the
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually transformIgnorePatterns means:

I don't want to transform x,y,z modules. 

However, the error SyntaxError: Cannot use import statement outside a module has a different meaning.

It means

This module is not in the syntax which Jest can execute. You need to ask Jest to transform it.

The regex in your example has a ! which tells Jest to NOT ignore those modules in transformation step, which means Jest needs to transform those modules.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing this out and your explanation. I will change it.

@coveralls
Copy link

coveralls commented Aug 9, 2021

Pull Request Test Coverage Report for Build 1115888734

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 94.366%

Totals Coverage Status
Change from base Build 1106605705: 0.0%
Covered Lines: 1084
Relevant Lines: 1110

💛 - Coveralls

@ahnpnl
Copy link
Collaborator

ahnpnl commented Aug 10, 2021

You would need to run lint-prettier :)

Copy link
Collaborator

@ahnpnl ahnpnl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@ahnpnl ahnpnl merged commit 12aaa91 into kulshekhar:master Aug 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants