create-nuxt-app
create-nuxt-app copied to clipboard
[Jest] npm run test command doesn't work with fresh new install
Hi guys,
I get the following error when running npx create-nuxt-app appname && cd appname && npm run test
.
FAIL test/Logo.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:
• 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/username/Desktop/appname/test/Logo.spec.js:1
import { mount } from '@vue/test-utils';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Runtime._execModule (node_modules/jest-runtime/build/index.js:1166:56)
Running coverage on untested files...Failed to collect coverage from /Users/username/Desktop/appname/pages/index.vue
ERROR: /Users/username/Desktop/appname/pages/index.vue: 'import' and 'export' may only appear at the top level (31:0)
29 | //
30 | //
> 31 | import Logo from '~/components/Logo.vue';
| ^
32 | export default {
33 | components: {
34 | Logo
STACK: SyntaxError: /Users/username/Desktop/appname/pages/index.vue: 'import' and 'export' may only appear at the top level (31:0)
29 | //
30 | //
> 31 | import Logo from '~/components/Logo.vue';
| ^
32 | export default {
33 | components: {
34 | Logo
at Parser._raise (/Users/username/Desktop/appname/node_modules/@babel/parser/lib/index.js:754:17)
at Parser.raiseWithData (/Users/username/Desktop/appname/node_modules/@babel/parser/lib/index.js:747:17)
at Parser.raise (/Users/username/Desktop/appname/node_modules/@babel/parser/lib/index.js:741:17)
at Parser.parseStatementContent (/Users/username/Desktop/appname/node_modules/@babel/parser/lib/index.js:11284:18)
at Parser.parseStatement (/Users/username/Desktop/appname/node_modules/@babel/parser/lib/index.js:11193:17)
at Parser.parseBlockOrModuleBlockBody (/Users/username/Desktop/appname/node_modules/@babel/parser/lib/index.js:11768:25)
at Parser.parseBlockBody (/Users/username/Desktop/appname/node_modules/@babel/parser/lib/index.js:11754:10)
at Parser.parseBlock (/Users/username/Desktop/appname/node_modules/@babel/parser/lib/index.js:11738:10)
at Parser.parseFunctionBody (/Users/username/Desktop/appname/node_modules/@babel/parser/lib/index.js:10745:24)
at Parser.parseFunctionBodyAndFinish (/Users/username/Desktop/appname/node_modules/@babel/parser/lib/index.js:10728:10)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 0 | 0 | 0 | 0 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 1.276 s
Ran all test suites.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] test: `jest`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/username/.npm/_logs/2020-06-20T18_31_43_020Z-debug.log
This is the content of that .log file
0 info it worked if it ends with ok
1 verbose cli [
1 verbose cli '/usr/local/Cellar/node/14.4.0/bin/node',
1 verbose cli '/usr/local/bin/npm',
1 verbose cli 'run',
1 verbose cli 'test'
1 verbose cli ]
2 info using [email protected]
3 info using [email protected]
4 verbose run-script [ 'pretest', 'test', 'posttest' ]
5 info lifecycle [email protected]~pretest: [email protected]
6 info lifecycle [email protected]~test: [email protected]
7 verbose lifecycle [email protected]~test: unsafe-perm in lifecycle true
8 verbose lifecycle [email protected]~test: PATH: /usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/Users/username/Desktop/appname/node_modules/.bin:/usr/local/sbin:/usr/local/opt/[email protected]/sbin:/usr/local/opt/[email protected]/bin:/Users/username/.platformsh/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin
9 verbose lifecycle [email protected]~test: CWD: /Users/username/Desktop/appname
10 silly lifecycle [email protected]~test: Args: [ '-c', 'jest' ]
11 silly lifecycle [email protected]~test: Returned: code: 1 signal: null
12 info lifecycle [email protected]~test: Failed to exec test script
13 verbose stack Error: [email protected] test: `jest`
13 verbose stack Exit status 1
13 verbose stack at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:332:16)
13 verbose stack at EventEmitter.emit (events.js:315:20)
13 verbose stack at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack at ChildProcess.emit (events.js:315:20)
13 verbose stack at maybeClose (internal/child_process.js:1051:16)
13 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:287:5)
14 verbose pkgid [email protected]
15 verbose cwd /Users/username/Desktop/appname
16 verbose Darwin 19.5.0
17 verbose argv "/usr/local/Cellar/node/14.4.0/bin/node" "/usr/local/bin/npm" "run" "test"
18 verbose node v14.4.0
19 verbose npm v6.14.4
20 error code ELIFECYCLE
21 error errno 1
22 error [email protected] test: `jest`
22 error Exit status 1
23 error Failed at the [email protected] test script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]
This was not happening before, only since I updated "create-nuxt-app" to version 3.0.0. Any help will be truly appreciated. Thanks
I am also having this issue. Shouldn't the generator default to generating code that actually works?
Had this exact same problem before. Fixed it by adding NODE_ENV=test
to the package.json scripts.
"test": "NODE_ENV=test jest"
I experienced this error in our team's Nuxt project and decided to generate a fresh one for comparison purposes. I wasn't able to run the tests successfully in that new project, leading me here.
The fix @welluh proposed worked for me... in both projects. I created PR #649 for this change. However, please note:
I am running Node 12.18.3 on macOs 10.14.6. Our team's Nuxt project (without the proposed fix) tests fine on my co-worker's machine and also on Jenkins. The version of Node is the same on both, although the operating system might differ. The merge checks for the PR fail on Ubuntu only. I'm unclear if these are real failures, or why they're failing. The ci output is sparse.
I don't use Vue CLI, but the note in this section suggests that users can no longer run jest directly. This seems like it could be the underlying cause, or at least related.