Skip to content
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] npm run test command doesn't work with fresh new install #553

Open
tonimedina-pri opened this issue Jun 20, 2020 · 3 comments
Open

Comments

@tonimedina-pri
Copy link

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! appname@1.0.0 test: `jest`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the appname@1.0.0 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 npm@6.14.4
3 info using node@v14.4.0
4 verbose run-script [ 'pretest', 'test', 'posttest' ]
5 info lifecycle appname@1.0.0~pretest: appname@1.0.0
6 info lifecycle appname@1.0.0~test: appname@1.0.0
7 verbose lifecycle appname@1.0.0~test: unsafe-perm in lifecycle true
8 verbose lifecycle appname@1.0.0~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/php@7.2/sbin:/usr/local/opt/php@7.2/bin:/Users/username/.platformsh/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin
9 verbose lifecycle appname@1.0.0~test: CWD: /Users/username/Desktop/appname
10 silly lifecycle appname@1.0.0~test: Args: [ '-c', 'jest' ]
11 silly lifecycle appname@1.0.0~test: Returned: code: 1  signal: null
12 info lifecycle appname@1.0.0~test: Failed to exec test script
13 verbose stack Error: appname@1.0.0 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 appname@1.0.0
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 appname@1.0.0 test: `jest`
22 error Exit status 1
23 error Failed at the appname@1.0.0 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

@unikitty37
Copy link

I am also having this issue. Shouldn't the generator default to generating code that actually works?

@welluh
Copy link

welluh commented Sep 20, 2020

Had this exact same problem before. Fixed it by adding NODE_ENV=test to the package.json scripts.
"test": "NODE_ENV=test jest"

lyndseypadget added a commit to lyndseypadget/create-nuxt-app that referenced this issue Oct 30, 2020
@lyndseypadget
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants