0

I have a Nuxt app that runs with pnpm and I'd like to test it using Jest and the vee-validate library.

Whenever I launch my tests, I get this error:

FAIL  test/integration/components/ui/RegistrationForm.test.ts
  ● Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • 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/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    /my-project/node_modules/.pnpm/vee-validate@3.4.14_vue@2.6.14/node_modules/vee-validate/dist/rules.js:889
    export { alpha, alpha_dash, alpha_num, alpha_spaces, between, confirmed, digits, dimensions, double, email, excluded, ext, image, integer, is, is_not, length, max, max_value, mimes, min, min_value, numeric, oneOf, regex, required, required_if, size };
    ^^^^^^

    SyntaxError: Unexpected token 'export'

      34 | } from '@nuxtjs/composition-api';
      35 | import { extend, ValidationProvider } from 'vee-validate';
    > 36 | import {
         | ^
      37 |   min_value as minValue,
      38 |   max_value as maxValue,
      39 | } from 'vee-validate/dist/rules';

      at Runtime.createScriptFromCode (node_modules/.pnpm/jest-runtime@28.1.1/node_modules/jest-runtime/build/index.js:1796:14)
      at Object.<anonymous> (components/form/fields/CalendarField.vue:36:1)
      at Object.<anonymous> (components/form/fields/FormField.vue:14:1)

I tried to add the option transformIgnorePattern inside jest.config.js but it's not working at all.

This is what I tried:

transformIgnorePatterns: ['/node_modules/(?!vee-validate/dist/rules)']

transformIgnorePatterns: ['/node_modules/(?!(.*vee-validate/dist/rules))/']

The only option that works for me is to import the UMD build but I lose all the benefit of tree-shaking.

Sources

https://github.com/logaretm/vee-validate/issues/2310 https://github.com/facebook/jest/issues/2081#issuecomment-699558143 https://qiita.com/tamonmon/items/6392c1680ef498a8c816

Théo Lavaux
  • 1,364
  • 3
  • 22
  • 46

1 Answers1

1

I ran into the same problem and was able to fix it by adjusting my jest.config.js file. I did three things there:

  1. add "vee-validate/dist/rules": "babel-jest", to transform: {}
  2. add the file path to transformIgnorePatterns like this: transformIgnorePatterns: ["/node_modules/", "/components/views/*", "node_modules/vee-validate/dist/rules"],
  3. add isolatedModules to true in ts-jest like: globals: { "ts-jest": { isolatedModules: true, babelConfig: true, }, },

Since this response is late to for the op, I hope this helps atleast someone :)