Open codeofsumit opened 1 month ago
It can be worked around by explicitly importing.
import { useNuxtApp } from ‘#imports'
@wattanx thanks!
import { useNuxtApp } from ‘#imports'
That seems to fix it so the web app starts. However, tests have a problem with this:
Anything special I need to do in tests that test the components that use this import workaround?
#imports
is alias.
You can check what alias it is by looking at .nuxt/tsconfig.json
.
If you are using vitest, you can set this in resolve.alias
.
https://vitest.dev/config/#alias
If alias is not used, the following writing style will also work.
import { useNuxtApp } from '@nuxt/bridge/dist/runtime'
@wattanx we are using jest and @vue/test-utils.
There is no tsconfig.json
in .nuxt
unfortunately.
I replaced all imports of import { useNuxtApp } from '#imports';
with import { useNuxtApp } from '@nuxt/bridge/dist/runtime'
but unfortunately it did not help.
Cannot find module '@nuxt/bridge/dist/runtime' from 'composables/usePortfolioReloadHandler.js
This is what thenuxt bridge folder looks like:
@wattanx I also tried it with
import { useNuxtApp } from '@nuxt/bridge/dist/runtime/index.mjs';
Now the error is this:
FAIL helper/index.test.js ● Test suite failed to run
Jest encountered an unexpected token Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax. Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration. By default "node_modules" folder is ignored by transformers.
Can this be fixed by doing some magic in jest.config.js
?
I tried transformIgnorePatterns: ['/node_modules/(?!@nuxt/bridge)'],
but it didn't help.
I replaced all imports of import { useNuxtApp } from '#imports'; with import { useNuxtApp } from '@nuxt/bridge/dist/runtime' but unfortunately it did not help.
I can't reproduce it. If you can provide me with a reproduction, I can investigate more. https://stackblitz.com/edit/github-hqqmha-zf5tch
we are using jest and @vue/test-utils.
I also think jest can resolve alias by using moduleNameMapper
.
https://jestjs.io/docs/configuration#modulenamemapper-objectstring-string--arraystring
@wattanx adding the alias via moduleNameMapper helped!
Unfortunately jest can't deal with ESM - I'm trying to figure this out using babel but no luck yet.
This is the jest config
module.exports = {
setupFiles: ['./jest.setup.js'],
testEnvironment: 'jsdom',
moduleNameMapper: {
'/assets/json/generated/(.*)': '<rootDir>/tests/__mocks__/$1',
'^@/(.*)$': '<rootDir>/$1',
'^~/(.*)$': '<rootDir>/$1',
'^vue$': 'vue/dist/vue.common.js',
'^#imports$': '@nuxt/bridge/dist/runtime/index.mjs',
},
moduleFileExtensions: ['js', 'mjs', 'cjs', 'jsx', 'ts', 'tsx', 'json', 'node', 'vue'],
transform: {
'\\.m?jsx?$': 'jest-esm-transformer',
'^.+\\.(js)$': 'babel-jest',
'.*\\.(vue)$': '@vue/vue2-jest',
'^.+\\.(css)$': '<rootDir>/cssTransform.js',
},
transformIgnorePatterns: ['/node_modules/(?!(swiper|@nuxt/bridge)/)'],
};
And this is the babel config
module.exports = {
presets: ['@babel/preset-env'],
plugins: ['@babel/plugin-transform-export-namespace-from'],
};
Environment
Darwin
v20.15.1
2.17.4
3.11.1
2.9.7
npm@10.8.2
webpack
bridge
,ssr
,modern
,telemetry
,components
,head
,loading
,build
,publicRuntimeConfig
,serverHandlers
,devServerHandlers
,devServer
,typescript
,nitro
,buildModules
-
()
,@nuxt/bridge@3.2.0
Reproduction
https://github.com/tresorone/tresor-repro
Describe the bug
As part of our nuxt 2 to nuxt 3 migration we ran across an error where
useNuxtApp
is not defined in our build, while it works fine in dev.To reproduce:
npm i
npm run build
npm start
localhost:3000
in your browser and open the javascript consoleYou should see the error
useNuxtApp is not defined
.Additional context
No response
Logs
No response