LoginSignup
1
1

More than 3 years have passed since last update.

SyntaxError: Cannot use import statement outside a moduleをtransformIgnorePatternsで解消した

Posted at

Vue Test Utilsとjestでテストを書いていたら、以下のエラーが出ました。

FAIL  tests/unit/Step2/template.spec.js
  ● 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:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
     • 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:

    /Users/okada/src/front/node_modules/@vueform/multiselect/dist/multiselect.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import{toRefs as e,ref as l,computed as t, …
                                                                                             ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      36 | <script>
      37 | import { computed } from "vue";
    > 38 | import Multiselect from "@vueform/multiselect";
         | ^
      39 | import Condition from "./Condition";
      40 | 
      41 | export default {

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
      at Object.<anonymous> (src/components/Step2/MultiSelect.vue:38:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        5.226 s
Ran all test suites matching /\/Users\/okada\/src\/front\/tests\/unit\/Step2\/template.spec.js/i with tests matching "Step2\.vue".

@vueform/multiselectが問題のようです。
おそらく以下をやればよいのかなと思うものの、いまいち書き方がわからない。

• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.

jestのドキュメントを読むが難しい…

検索してこのページをみつけました。

真似して、jest.config.jsに以下を追記すると解決しました。

transformIgnorePatterns: ["/node_modules/(?!@vueform/multiselect)"],

jest.config.js全文はこんな感じです(関係ない記述もあります)

module.exports = {
  preset: "@vue/cli-plugin-unit-jest",
  setupFiles: ["./jest.setup.js"],
  transformIgnorePatterns: [
    "/node_modules/(?!@vueform/multiselect|vue3-carousel/dist/carousel.css)",
  ],
};
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1