-
-
Notifications
You must be signed in to change notification settings - Fork 48
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
use components discovery with tests #58
Comments
I think it would be possible to create something that auto imports components at test level. Otherwise |
Yes, please implement auto imports at test level. |
As a workaround, I'm registering all components as global Vue components in my jest setup file: [
'app/components/MyComponentA.vue',
'app/components/MyComponentB.vue',
'ui-kit/components/ComponentA.vue',
'ui-kit/components/ComponentB.vue',
'ui-kit/components/ComponentC.vue',
].forEach((path) => {
const name = path.match(/(\w*)\.vue$/)[1];
const prefix = path.startsWith('ui-kit/') ? 'Ui' : '';
Vue.component(`${prefix}${name}`, require(path).default);
}); I could automate this by pulling in the configuration/using globs, but I don't feel the need to make it more complicated than this, especially that I'm working on the project alone. I've spent the past couple of days messing with my project configuration. Not sure if it's been worth all the trouble, but I hope this helps somebody! |
@Merott Can you show your jest config file, please? |
@harrytran998 are you running into a particular issue? I don't have anything special for auto-scanning components in my jest config file: // jest.config.js
const path = require('path');
module.exports = {
moduleNameMapper: {
'^@@?/(.*)$': '<rootDir>/$1',
'^~~?/(.*)$': '<rootDir>/$1',
'^vue$': 'vue/dist/vue.common.js',
},
moduleFileExtensions: ['ts', 'js', 'vue', 'json'],
transform: {
'^.+\\.ts$': 'ts-jest',
'^.+\\.js$': 'babel-jest',
'.*\\.(vue)$': 'vue-jest',
},
testPathIgnorePatterns: ['<rootDir>/(.vscode|node_modules|cypress)/'],
setupFilesAfterEnv: [path.resolve(__dirname, 'jest.setup.ts')],
snapshotSerializers: ['jest-serializer-vue'],
}; |
Thanks @Merott, I ended up doing: // test/setupTest.js (jest setup)
const components = [
'../components/MyComponentA.vue',
'../components/MyComponentB.vue',
]
components.forEach((path) => {
const name = path.match(/(\w*)\.vue$/)[1];
Vue.component(`${name}`, require(path).default);
}); then could use |
Thanks, I have tried this solution which works. |
This solution is great, but it fails when some component is contained within auto-scanning components. |
The following code can automatically register components.
|
I hope someone will give it a clean solution.
I can use stubs: const wrapper = mount(MyComponent, { stubs: ['font-awesome-icon'] }) But it's not clean. |
I don't know if it's truly not affecting to any configuration that running on test system.. But i found this configuration working to me, and these setting helps me prevents warning @bob-lee mentions. By adding code in ./test/jest.setup.js import Vue from 'vue'
import { config } from '@vue/test-utils'
Vue.config.silent = true
// Vue.config.ignoredElements = ['nuxt-link']
// Mock Nuxt components
config.stubs.nuxt = { template: '<div />' }
config.stubs['nuxt-link'] = { template: '<a><slot /></a>' }
config.stubs['no-ssr'] = { template: '<span><slot /></span>' } and add these lines in config file ./jest.config.js |
@danielroe Is there something that could be implemented to include auto-scanning? This solution here works, but I can't get @ryoju-ohata 's solution working. |
There are actually two solutions to polyfill components in test environment:
For both, we need a |
@pi0 So what is the official way to setup our tests to take advantage of auto import feature? Could there be some documentation added to Nuxt to demonstrate this? Very difficult to take advantage of the auto import feature with our code if our tests don't work. |
That's a nice idea @nmackey. I'm currently a little bit overwhelmed by tasks but PR definitely welcome for adding examples or docs. /cc @danielroe @ricardogobbosouza if you are interested to help on this. |
Hello, I wouldn't mind writing the documentation, but I'm unsure what are the steps to actually make it work. 🤔 |
I got one definitely working solution, based on this comment:
The steps I actually took:
This worked, but it's a significant slowdown for running tests. If I know I have an up-to-date-build, I can delete the This specific implementation is to my mind too slow and hacky to enshrine in the official documentation, honestly, even though the programatically-generated |
Hi! So what would be the official way to solve this issue? module.exports = {
preset: '@nuxt/test-utils',
moduleFileExtensions: [
'js',
'json',
'vue'
],
transform: {
'.*\\.(vue)$': '@vue/vue2-jest',
'.*\\.(js)$': 'babel-jest'
},
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/$1',
'^~/(.*)$': '<rootDir>/$1',
'^vue$': 'vue/dist/vue.common.js',
'~/([a-zA-Z0-9/.\\-_]*)': '<rootDir>/$1',
'/^~/(.*)$/': './$1',
'^.+\\.(css)$': '<rootDir>/test/__mocks__/css.js'
},
testEnvironment: 'jsdom',
setupFilesAfterEnv: ['./jest.setup.js']
} And in
PS: And do you know if this will still be an issue with Nuxt 3 and Vitest? |
@ambirdsall-gogo Just having |
Describe the bug
Tried this library first time,
dynamic imports
are working great!About
auto scan
feature, I found it saves many lines of codes but some tests are getting error:this is the case where I have
MyComponentA
andMyComponentB
defined under/components
folder andMyComponentA
is usingMyComponentB
.If I resurrect lines of codes that imports
MyComponentB
inMyComponentA
, the test are working back ok but I wonder if I am missing something?Additional context
[email protected]
@nuxt/[email protected]
@vue/[email protected]
The text was updated successfully, but these errors were encountered: