0

how are you ?

I'm having a issue in testing Vuex stuff with Quasar.

Testing components is working normally, but when I started to test my store, I got it.

spec file:

/test/jest/__tests__/store/auth/mutations.spec.js

import { store } from 'src/store';
import { mutations } from 'src/store/auth';
import mutations from 'src/store/auth/mutations';

All those ways I tried to import my store stuff, I got this error:

({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { store } from 'quasar/wrappers';

SyntaxError: Cannot use import statement outside a module

how can I import my store in my spec files?

2 Answers 2

1
import { store } from 'src/store';
import { mutations } from 'src/store/auth';
import mutations from 'src/store/auth/mutations';

just use require

const store = require('src/store');
const mutations = require('src/store/auth)';
const mutations = require('src/store/auth/mutations');

you need type=module in package.json to use import

Sign up to request clarification or add additional context in comments.

Comments

0

Your problem is that import statements are not supported by plain JavaScript, therefore neither by Node. import is an addition called ES Modules. Since Jest runs your tests in Node, you are getting SyntaxError since import { store } from 'quasar/wrappers' is invalid JS syntax.

The solution is that you need to tell Jest to use Babel (or similar tool) to transform your test file before trying to execute it in Node.

This requires some configuration in your jest.config.js, and also you'd need to install babel-jest, babel and related packages and create a .babelrc or babel.config.js to tell Babel what to do with your test files exactly.

The actual right configuration may depend on many factors, but as a starting point refer to these:

Also I'd recommend to consider using Quasar CLI, which gives you a handy way to automatically set up Jest for your Quasar project, including a simple example test you can use as a starting reference: https://testing.quasar.dev/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.