nuxt / test-utils

πŸ§ͺ Test utilities for Nuxt
http://nuxt.com/docs/getting-started/testing
MIT License
323 stars 84 forks source link

fix(runtime-utils): support options api data, computed + methods #963

Closed inouetakuya closed 1 month ago

inouetakuya commented 1 month ago

πŸ”— Linked issue

fixes #961

❓ Type of change

πŸ“š Description

As described in #961, mountSuspended did not support the Options API data, computed, and methods, so we have fixed this to support them.

inouetakuya commented 1 month ago

debug: Comment out supporting options api in renderSuspended

-> Test fails.

$ pnpm prepack && pnpm test:examples
 WARN  Unsupported engine: wanted: {"node":">=18.20.4"} (current: {"node":"v18.19.0","pnpm":"9.11.0"})

> @nuxt/test-utils@3.14.2 prepack /Users/inouetakuya/src/github.com/inouetakuya/test-utils
> unbuild

β„Ή Building test-utils                                                                                                             10:01:58
β„Ή Cleaning dist directory: ./dist                                                                                                 10:01:58
  61β”œβ”€ .nuxt/test/jy8ffo/output/server/chunks/_/error-500.mjs (4.77 kB) (2.02 kB gzip)
β”‚   β”œβ”€ .nuxt/test/jy8ffo/output/server/chunks/routes/renderer.mjs.map (960 B) (402 B gzip)
β”‚   β”œβ”€ .nuxt/test/jy8ffo/output/server/chunks/runtime.mjs (166 kB) (40.5 kB gzip)
β”‚   β”œβ”€ .nuxt/test/jy8ffo/output/server/chunks/runtime.mjs.map (6.18 kB) (1.04 kB gzip)
β”‚   β”œβ”€ .nuxt/test/jy8ffo/output/server/chunks/virtual/_virtual_spa-template.mjs (94 B) (100 B gzip)
β”‚   β”œβ”€ .nuxt/test/jy8ffo/output/server/chunks/virtual/_virtual_spa-template.mjs.map (112 B) (111 B gzip)
β”‚   β”œβ”€ .nuxt/test/jy8ffo/output/server/index.mjs (291 B) (193 B gzip)
β”‚   └─ .nuxt/test/jy8ffo/output/server/package.json (600 B) (299 B gzip)
  Listening on http://127.0.0.1:50352
β”‚ [nitro] βœ” You can preview this build using node .nuxt/test/jy8ffo/output/server/index.mjs
β”‚ Ξ£ Total size: 1.69 MB (410 kB gzip)
β”‚ > example-app-vitest-full@ test:happy-dom /Users/inouetakuya/src/github.com/inouetakuya/test-utils/examples/app-vitest-full
β”‚ > VITEST_DOM_ENV=happy-dom pnpm test:unit --run
β”‚ > example-app-vitest-full@ test:unit /Users/inouetakuya/src/github.com/inouetakuya/test-utils/examples/app-vitest-full
β”‚ > vitest "--run"
β”‚ From custom module!
β”‚  RUN  v2.1.2 /Users/inouetakuya/src/github.com/inouetakuya/test-utils/examples/app-vitest-full
β”‚ stdout | tests/nuxt/mock-vue-router.spec.ts
β”‚ <Suspense> is an experimental feature and its API will likely change.
β”‚ stdout | tests/nuxt/mount-suspended.spec.ts
β”‚ <Suspense> is an experimental feature and its API will likely change.
β”‚ stdout | tests/nuxt/mock-component-1.spec.ts
β”‚ <Suspense> is an experimental feature and its API will likely change.kuya/test-utils/examples/app-playwright/.nuxt/test/5jkthk/analyze/.…
β”‚ stdout | tests/nuxt/routing.spec.ts
β”‚ <Suspense> is an experimental feature and its API will likely change.
β”‚ stdout | tests/nuxt/server.spec.ts:1 β€Ί test
  [Desktop Chrome] β€Ί basic.test.ts:17:1 β€Ί testing
β”‚ stdout | tests/nuxt/mock-indexeddb.spec.tst
β”‚ <Suspense> is an experimental feature and its API will likely change.
β”‚ stdout | tests/nuxt/auto-import.spec.ts
β”‚ <Suspense> is an experimental feature and its API will likely change.
β”‚ stdout | tests/nuxt/auto-import-mock.spec.ts
β”‚ <Suspense> is an experimental feature and its API will likely change.
β”‚ stdout | tests/nuxt/render-suspended.spec.ts
β”‚ <Suspense> is an experimental feature and its API will likely change.
β”‚  βœ“ tests/nuxt/auto-import.spec.ts  (3 tests) 9msuetakuya/src/github.com/inouetakuya/test-utils
β”‚  βœ“ tests/nuxt/mock-component-1.spec.ts  (1 test) 24msr test --filter example-app-cucumber
β”‚  βœ“ tests/nuxt/mock-indexeddb.spec.ts  (1 test) 28ms
β”‚  βœ“ tests/nuxt/auto-import-mock.spec.ts  (2 tests) 8ms
β”‚  βœ“ tests/nuxt/mock-vue-router.spec.ts  (1 test) 7ms
β”‚  βœ“ tests/nuxt/routing.spec.ts  (3 tests) 130ms
β”‚  βœ“ tests/nuxt/server.spec.ts  (4 tests) 83msxperimental-vm-modules node_modules/jest/bin/jest.js
β”‚ stderr | tests/nuxt/render-suspended.spec.ts > renderSuspended > should render asyncData and other options api properties within nuxt su…
β”‚ [Vue warn]: Unhandled error during execution of render function
β”‚   at <OptionsApiPage > c.test.ts:17:1 β€Ί testing
β”‚   at <RenderHelper> asic.test.ts:3:1 β€Ί test
β”‚   at <Anonymous ref="VTU_COMPONENT" > β€Ί testing
β”‚   at <VTUROOT>s transformed.
β”‚  βœ“ tests/nuxt/mount-suspended.spec.ts  (25 tests | 1 skipped) 172ms
β”‚ stdout | tests/nuxt/mock-component-3.spec.ts-dom && pnpm test:jsdom && pnpm test:dev && pnpm test:types
β”‚ <Suspense> is an experimental feature and its API will likely change.ub.com/inouetakuya/test-utils/examples/app-vitest-full
β”‚ stdout | tests/nuxt/injected-value-component.spec.ts
β”‚ <Suspense> is an experimental feature and its API will likely change.m/inouetakuya/test-utils/examples/app-vitest-full
β”‚  βœ“ tests/nuxt/mock-component-3.spec.ts  (1 test) 18ms
β”‚  βœ“ tests/nuxt/injected-value-component.spec.ts  (1 test) 20ms
β”‚ stdout | tests/nuxt/mount-component.spec.tscom/inouetakuya/test-utils/examples/app-vitest-full
β”‚ <Suspense> is an experimental feature and its API will likely change.
β”‚ stdout | tests/nuxt/mock-nuxt-composable-1.spec.ts
β”‚ <Suspense> is an experimental feature and its API will likely change./examples/app-vitest
β”‚  βœ“ tests/nuxt/mount-component.spec.ts  (1 test) 11ms
β”‚ stdout | tests/nuxt/mock-component-2.spec.tss API will likely change.
β”‚ <Suspense> is an experimental feature and its API will likely change.
β”‚ stdout | tests/nuxt/mock-component-expose-setup-state.spec.ts
β”‚ <Suspense> is an experimental feature and its API will likely change.
β”‚ stdout | tests/nuxt/mock-nuxt-composable-3.spec.ts
β”‚ <Suspense> is an experimental feature and its API will likely change.
β”‚  βœ“ tests/nuxt/mock-nuxt-composable-1.spec.ts  (1 test) 8ms
β”‚  βœ“ tests/nuxt/mock-component-expose-setup-state.spec.ts  (1 test) 9ms
β”‚  βœ“ tests/nuxt/mock-component-2.spec.ts  (1 test) 38ms
β”‚  βœ“ tests/nuxt/mock-nuxt-composable-3.spec.ts  (2 tests) 9ms
β”‚ stdout | tests/nuxt/config.spec.ts
β”‚ <Suspense> is an experimental feature and its API will likely change.
β”‚  βœ“ tests/nuxt/config.spec.ts  (1 test) 3ms
β”‚  βœ“ tests/unit/index.spec.ts  (1 test) 1ms
β”‚  βœ“ pages/index.vue  (1 test) 1ms
β”‚  ❯ tests/nuxt/render-suspended.spec.ts  (12 tests | 1 failed) 5106ms
β”‚    Γ— renderSuspended > should render asyncData and other options api properties within nuxt suspense
β”‚      β†’ Test timed out in 5000ms.
β”‚ If this is a long-running test, pass a timeout value as the last argument or configure it globally with "testTimeout".
β”‚ stdout | tests/nuxt/plugins.spec.ts
β”‚ <Suspense> is an experimental feature and its API will likely change.
β”‚ stdout | tests/nuxt/mock-nuxt-composable-2.spec.ts
β”‚ <Suspense> is an experimental feature and its API will likely change.
β”‚  βœ“ tests/nuxt/plugins.spec.ts  (1 test) 12ms
β”‚  βœ“ tests/nuxt/mock-nuxt-composable-2.spec.ts  (1 test) 11ms
β”‚ ⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯
β”‚  FAIL  tests/nuxt/render-suspended.spec.ts > renderSuspended > should render asyncData and other options api properties within nuxt susp…
β”‚ Error: Test timed out in 5000ms.
β”‚ If this is a long-running test, pass a timeout value as the last argument or configure it globally with "testTimeout".
β”‚ ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯
β”‚ ⎯⎯⎯⎯ Unhandled Rejection ⎯⎯⎯⎯⎯
β”‚ TypeError: _ctx.greetingInMethods is not a function
β”‚  ❯ Proxy._sfc_render pages/other/options-api.vue:22:10
β”‚ ⎯⎯⎯⎯⎯⎯ Unhandled Errors ⎯⎯⎯⎯⎯⎯
β”‚ Vitest caught 1 unhandled error during the test run.
β”‚ This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.
β”‚      20|     </li>
β”‚      21|     <li data-testid="greetingInMethods">
β”‚      22|       {{ greetingInMethods() }}
β”‚        |          ^
β”‚      23|     </li>
β”‚      24|     <li data-testid="returnData1">
β”‚  ❯ Proxy.clonedComponent.render ../../dist/runtime-utils/index.mjs:264:39
β”‚  ❯ renderComponentRoot ../../node_modules/.pnpm/@vue+runtime-core@3.5.6/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:6365:16
β”‚  ❯ ReactiveEffect.componentUpdateFn [as fn] ../../node_modules/.pnpm/@vue+runtime-core@3.5.6/node_modules/@vue/runtime-core/dist/runtime…
β”‚  ❯ ReactiveEffect.run ../../node_modules/.pnpm/@vue+reactivity@3.5.6/node_modules/@vue/reactivity/dist/reactivity.cjs.js:226:19
β”‚  ❯ setupRenderEffect ../../node_modules/.pnpm/@vue+runtime-core@3.5.6/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5322:5
β”‚  ❯ ../../node_modules/.pnpm/@vue+runtime-core@3.5.6/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:7064:9
β”‚ This error originated in "tests/nuxt/render-suspended.spec.ts" test file. It doesn't mean the error was thrown inside the file itself, b…
β”‚ The latest test that might've caused the error is "should render asyncData and other options api properties within nuxt suspense". It mi…
β”‚ - The error was thrown, while Vitest was running this test.
β”‚ - If the error occurred after the test had been completed, this was the last documented test before it was thrown.
β”‚ ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
β”‚  Test Files  1 failed | 20 passed (21)
β”‚       Tests  1 failed | 63 passed | 1 todo (65)
β”‚      Errors  1 error
β”‚    Start at  10:02:06
β”‚    Duration  9.67s (transform 2.75s, setup 44.53s, collect 4.03s, tests 5.70s, environment 7.50s, prepare 8.42s)
β”‚  ELIFECYCLE  Command failed with exit code 1.
β”‚  ELIFECYCLE  Command failed with exit code 1.
└─ Failed in 13.3s at /Users/inouetakuya/src/github.com/inouetakuya/test-utils/examples/app-vitest-full
examples/app-vitest test$ vitest run
β”‚  RUN  v2.1.2 /Users/inouetakuya/src/github.com/inouetakuya/test-utils/examples/app-vitest
β”‚ stdout | test/app.nuxt.spec.ts
β”‚ <Suspense> is an experimental feature and its API will likely change.
β”‚  βœ“ test/app.nuxt.spec.ts  (1 test) 8ms
└─ Running...
/Users/inouetakuya/src/github.com/inouetakuya/test-utils/examples/app-vitest-full:
 ERR_PNPM_RECURSIVE_RUN_FIRST_FAIL  example-app-vitest-full@ test: `pnpm test:happy-dom && pnpm test:jsdom && pnpm test:dev && pnpm test:types`
Exit status 1
examples/content test$ vitest run
└─ Running...
 ELIFECYCLE  Command failed with exit code 1.
inouetakuya commented 1 month ago

Revert "debug: Comment out supporting options api in renderSuspended"

-> Test succeeds πŸ†—

$ pnpm prepack && pnpm test:examples
 WARN  Unsupported engine: wanted: {"node":">=18.20.4"} (current: {"node":"v18.19.0","pnpm":"9.11.0"})

> @nuxt/test-utils@3.14.2 prepack /Users/inouetakuya/src/github.com/inouetakuya/test-utils
> unbuild

β„Ή Building test-utils                                                                                                             10:04:02
β„Ή Cleaning dist directory: ./dist                                                                                                 10:04:02
βœ” Build succeeded for test-utils                                                                                                  10:04:07
[3 lines collapsed]
[10 lines collapsed]pendencies because vite config has changed
β”‚ [nitro] βœ” Nuxt Nitro server built in 1773 ms
β”‚ β„Ή Vite client warmed up in 1ms
β”‚ [nuxi] β„Ή Dev server event: type="nuxt:internal:dev:ready" port=51194
β”‚ β„Ή Vite server warmed up in 676ms
β”‚ PASS test/dev.e2e.spec.ts (23.224 s)
β”‚ Test Suites: 3 passed, 3 total
β”‚ Tests:       6 passed, 6 total
└─ Done in 25s 2 passed, 2 total
β”‚ Time:        23.555 s
β”‚ Ran all test suites.
examples/app-playwright test$ playwright test
[165 lines collapsed]
β”‚   └─ .nuxt/test/ns92by/output/server/package.json (600 B) (299 B gzip)
  [Desktop Chrome] β€Ί basic.test.ts:3:1 β€Ί test
  [Desktop Chrome] β€Ί basic.test.ts:17:1 β€Ί testing
    2 passed (14.8s)
β”‚ To open last HTML report run:
β”‚   pnpm exec playwright show-report
└─ Done in 15.2s
examples/app-vitest test$ vitest run
[5 lines collapsed]
β”‚ Listening on http://127.0.0.1:51205
β”‚  βœ“ test/server.e2e.spec.ts  (1 test) 13536ms
β”‚  βœ“ test/browser.e2e.spec.ts  (1 test) 14582ms
β”‚ Listening on http://127.0.0.1:51217
β”‚  βœ“ test/generate.e2e.spec.ts  (1 test) 15428ms
β”‚  βœ“ test/dev.e2e.spec.ts  (1 test) 18003ms
β”‚  Test Files  5 passed (5)form 2.03s, setup 41.96s, collect 4.27s, tests 932ms, environment 7.49s, prepare 8.74s)
β”‚       Tests  5 passed (5)@ test:jsdom /Users/inouetakuya/src/github.com/inouetakuya/test-utils/examples/app-vitest-full
β”‚    Start at  10:04:11
β”‚    Duration  19.37s (transform 778ms, setup 2.01s, collect 2.15s, tests 61.56s, environment 272ms, prepare 982ms)-full
└─ Done in 22.7s
examples/app-vitest-full test$ pnpm test:happy-dom && pnpm test:jsdom && pnpm test:dev && pnpm test:types
[65 lines collapsed]
β”‚  Test Files  21 passed (21)
[441 lines collapsed]sed | 1 todo (65)
β”‚ [nuxt] Your project has pages but the <NuxtPage /> component has not been used. You might be using the <RouterView /> component instead,…
β”‚ βœ“ tests/nuxt/plugins.spec.ts  (1 test) 5ms
β”‚ Test Files  20 passed (20)
β”‚ Tests  63 passed | 1 todo (64)
β”‚ Start at  10:04:40
β”‚ Duration  6.60s (transform 1.22s, setup 33.35s, collect 1.39s, tests 499ms, environment 4.06s, prepare 6.40s)
β”‚ > example-app-vitest-full@ test:types /Users/inouetakuya/src/github.com/inouetakuya/test-utils/examples/app-vitest-full
β”‚ > nuxi prepare && vue-tsc --noEmit
β”‚ From custom module!
β”‚ βœ” Types generated in .nuxt
└─ Done in 43.7sUsers/inouetakuya/src/github.com/inouetakuya/test-utils/examples/content
β”‚ stdout | tests/index.spec.ts
β”‚ <Suspense> is an experimental feature and its API will likely change./examples/app-vitest
β”‚  βœ“ tests/index.spec.ts  (1 test) 24ms
β”‚  Test Files  1 passed (1)ntal feature and its API will likely change.
β”‚       Tests  1 passed (1) (1 test) 8ms
β”‚    Start at  10:04:29
β”‚    Duration  2.98s (transform 1.21s, setup 1.54s, collect 343ms, tests 24ms, environment 140ms, prepare 420ms)
└─ Done in 8.7s
examples/i18n test$ vitest runnpm test:unit --run
└─ Running...
examples/module test$ vitest run
β”‚  RUN  v2.1.2 /Users/inouetakuya/src/github.com/inouetakuya/test-utils/examples/i18n
β”‚ stdout | test/index.spec.ts
β”‚ <Suspense> is an experimental feature and its API will likely change.
β”‚  βœ“ test/index.spec.ts  (1 test) 14ms
β”‚  Test Files  1 passed (1)
β”‚       Tests  1 passed (1)
β”‚    Start at  10:04:34
β”‚    Duration  1.17s (transform 442ms, setup 440ms, collect 84ms, tests 14ms, environment 129ms, prepare 317ms)
└─ Done in 4.7s
examples/module test$ vitest run
[2 lines collapsed]
β”‚ Plugin injected by my-module!
β”‚ Listening on http://127.0.0.1:51279
β”‚ Plugin injected by my-module!
β”‚ Listening on http://127.0.0.1:51285
β”‚ Plugin injected by my-module!
β”‚  βœ“ test/basic.test.ts  (2 tests) 6250ms
β”‚  Test Files  1 passed (1)
β”‚       Tests  2 passed (2)
β”‚    Start at  10:04:32
β”‚    Duration  6.73s (transform 70ms, setup 0ms, collect 266ms, tests 6.25s, environment 0ms, prepare 51ms)
└─ Done in 7.1s
.                                        |  WARN  Unsupported engine: wanted: {"node":">=18.20.4"} (current: {"node":"v18.19.0","pnpm":"9.11.0"})

> example-app-cucumber@ test /Users/inouetakuya/src/github.com/inouetakuya/test-utils/examples/app-cucumber
> NODE_OPTIONS='--loader ts-node/esm' NODE_ENV=test cucumber-js

(node:6830) ExperimentalWarning: `--experimental-loader` may be removed in the future; instead use `register()`:
--import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; register("ts-node/esm", pathToFileURL("./"));'
(Use `node --trace-warnings ...` to show where the warning was created)
(node:6861) ExperimentalWarning: `--experimental-loader` may be removed in the future; instead use `register()`:
--import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; register("ts-node/esm", pathToFileURL("./"));'
(Use `node --trace-warnings ...` to show where the warning was created)
Listening on http://127.0.0.1:51371
1 scenario (1 passed)
1 step (1 passed)
0m06.571s (executing steps: 0m00.676s)
inouetakuya commented 1 month ago

@danielroe

Thank you for your review!

https://github.com/nuxt/test-utils/pull/963#pullrequestreview-2346367843

do we need to apply the same fixes to renderSuspended?

Yes, we need.

I added test for renderSuspended.

inouetakuya commented 1 month ago

@danielroe

I actually use this library for work. So I'd like to ask, when do you think the version that merges this pull request will be released? πŸ™ πŸ™ πŸ™

danielroe commented 1 month ago

released! βœ…

inouetakuya commented 1 month ago

Thank you so much!! πŸ™Œ