-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
Jest tests can't process import statement #1584
Comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Adding Full module.exports = {
moduleFileExtensions: ['js', 'jsx', 'json', 'vue'],
transform: {
'^.+\\.vue$': 'vue-jest',
'.+\\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$':
'jest-transform-stub',
'^.+\\.(js|jsx)?$': 'babel-jest'
},
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1'
},
snapshotSerializers: ['jest-serializer-vue'],
testMatch: [
'<rootDir>/(tests/unit/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx))'
],
transformIgnorePatterns: ['<rootDir>/node_modules/']
}; For now everything seems to be good. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Is everyone here using windows? I didn't reproduce the error on mac. |
Note: I can't reproduce on the dev environment. |
I'm using Ubuntu 18.04 |
Reproduced on mac here. ^ began erroring. What ended up working for me was globally installing the latest |
This comment has been minimized.
This comment has been minimized.
I did a full clean as proposed by @joebartels above, and I also updated (through the UI) to RC3, but to no avail. Same errors. It seems to appear only in my tests involving vuex. |
I’m running on Mac. I configured this for vuejs. And it worked for me. |
I can't reproduce the issue on Windows 10 & Node 10.4.1:
|
This comment has been minimized.
This comment has been minimized.
That's it ? Closed ? Well, I am on a Mac, with RC3, and still have the exact same errors. I tried everything that is suggested in this thread. For the archive, here is my latest
|
Since it was failing on my newly created test file, I decided to remove it. But after 2 min, not happy with this situation, I wanted to retry, so I put it back. And all green ! Tested again, all green. There must be something fishy with a cache somewhere, no? |
@onekiloparsec that loosely resembles what happened to me. |
|
its cannot find the import and getting this error FAIL src/tests/actions/expenses.test.js RUNS src/tests/add.test.js ● Test suite failed to run
RUNS src/tests/add.test.js RUNS src/tests/add.test.js RUNS src/tests/add.test.js PASS src/tests/add.test.js (5.007s) RUNS src/tests/add.test.js Test Suites: 1 failed, 1 passed, 2 total |
@onigunn @onekiloparsec Can't find the jest.config.js file! |
Same error on Create React App with jest-puppeteer e2e test. sign_in.test.js: const signIn = require('./jest.sign_in')
describe('sign_in', () => {
beforeEach(async () => {
await page.goto('http://localhost:3000')
})
it('should log in auth form', async () => {
try {
await signIn(page)
} catch (err) {
console.warn(`Unexpected test error: ${err}`)
}
}, 4000)
}) jest.sign_in.js:
|
Surely it's a ES6 module syntax transpiling issue. Your solution, as mine goin to work for any boilerplate, I think. But i wont eject webpack from CRA and hasnt got .babelrc, babel.config and jest.config, as you, @mohsenuss91. Try to my solution |
@WebKieth I'm using VueJs and I'm trying with: But still getting this:
|
@mohsenuss91 aw, as u see I dont use built-in test utilities (like vue's shallowMount or react's renderer), I use puppeteer instructions - It's just another way to test. My React Tools dont do anything inside test scripts - so I dont import it. I import only required repeated actions. |
@marcusnunes I'm using windows10, and still got this : `({"Object.":function(module,exports,require,__dirname,__filename,global,jest){import { shallowMount } from '@vue/test-utils';
Test Suites: 1 failed, 1 total Here is my jest.config.js file: |
I'm currently stuck on something I believe is related. My import { mount } from '@vue/test-utils'
import Quiz from '@/Quiz.vue'
describe('Quiz', () => {
test('is a Vue instance', () => {
const wrapper = mount(Quiz)
expect(wrapper.isVueInstance()).toBeTruthy()
})
}) The ......
<script>
import { mapState } from 'vuex'
import Loading from '~/components/Quiz/Loading'
import QuizStep from '~/components/Quiz/Step'
import NameStep from '~/components/Quiz/StepType/Name'
import GradeStep from '~/components/Quiz/StepType/Grade'
...... When I run $ yarn test
yarn run v1.22.4
$ jest
FAIL components/__tests__/components.spec.js
● Test suite failed to run
Cannot find module '~/components/Quiz/Loading' from 'Quiz.vue'
Require stack:
components/Quiz.vue
components/__tests__/components.spec.js
38 | <script>
39 | import { mapState } from 'vuex'
> 40 | import Loading from '~/components/Quiz/Loading'
| ^
41 | import QuizStep from '~/components/Quiz/Step'
42 | import NameStep from '~/components/Quiz/StepType/Name'
43 | import GradeStep from '~/components/Quiz/StepType/Grade'
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:299:11)
at components/Quiz.vue:40:1
at Object.<anonymous> (components/Quiz.vue:456:3)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 3.041s
Ran all test suites. I have bashed my head the whole day trying to get it to work but nothing. I can provide further info/configs if required. For tests on other components that do not have any imports from my |
@kaypon have you tried absolute paths and including the .vue extension? |
@kaypon i have the same problem , did you solve it yet? |
ES modules are not supported in Node. Because Jest is running in Node, when it hits this import statement, we're getting that syntax error. .babelrc.js
|
This worked for me, thank you! @sirlancelot |
I use
|
None of this worked for me, i am using vuejs |
This issue is over two years old now. It is very likely not "fixable" by the team per se. Most folks have found this comment to be the most helpful:
This is probably worth closing and locking with this as the last comment as it's far too broad. cc: @Akryum |
Test fail. SyntaxError: Cannot use import statement outside a module. at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14) Jest encountered an unexpected token. Babel config |
No go for me. Still stuck at And yeah |
A fix that worked for me: replace |
I am getting this error with webpack config, when it detects
There is a preset option in there, where I added jest.config.json:
|
I'm getting this same error when I try to import json into my component. |
Not sure what the status of this one is, but I think there is a simple solution that I haven't seen mentioned: the only two things I needed to do for happy jest testing is:
My babelrc is simply:
I am using nuxt 2, that is not configured to use babelrc (default behavior), so YMMV.. but this change above should make testing work fine. Some other things that might apply: in jest.config.js this may be a nuxt thing? just allows for aliased imports (
And other jest v28 stuff:
|
For me, changing the transform to use Perhaps that should be updated in https://v1.test-utils.vuejs.org/guides/using-with-typescript.html#processing-single-file-components-in-jest ? |
It happens with |
Version
3.0.0-rc.2
Reproduction link
https://github.com/ijdickinson/vue-cli-jest-problem
Steps to reproduce
This is a similar problem to 1475, but I can't find a way to resolve it. I created a new app with vue-cli, selecting Jest tests. I added a simple POJO (
src/models/model.js
) and a test for it (tests/model.spec.js
).To repro the problem, just
npm run test:unit
What is expected?
Tests to pass, or at least run
What is actually happening?
The text was updated successfully, but these errors were encountered: