Replies: 2 comments
-
Can you find a minimal repro? // src/basic.ts
import svg from './demo.svg';
export { svg };
// test/basic.test.ts
import { svg } from '../src/basic.js';
test('svg', () => {
expect(svg).toMatchInlineSnapshot(`"/src/demo.svg"`);
}); |
Beta Was this translation helpful? Give feedback.
-
Okay, I fixed it but it took a little playing around. So I'd like to learn why it works now. I swithced // src/basic.ts to // src/basic.ts however, when I did this, it did nothing (I added the ./ before src) BUT when I switched src into /../ it worked, so I went from // src/basic.ts to (switching src with .. // src/basic.ts then the error is resolved and we fixed it, however not out of the woods yet |
Beta Was this translation helpful? Give feedback.
-
Summary
I'm trying to run Vitest on a test.js file that has .svg files imported into it and the src file it is testing. Due to the following error, I was aiming to get Vitest to just default to a mock file but that's throwing the attached error either way, if I use it or not. Note, jest passes this test Vitest does not.
Initial problem (attached)
On npx vitest src/someFodler/fileToBeTested.test.js, Failed to resolve import, is thrown
Facts
I am adding svgr as a plugin in both the vite and vitest config.js files (see attached)
What I have tried
export const ReactComponent = () => 'MockedSVGComponent';
export default 'mocked-svg';
* I have added the alias to the vitest.config.js file as below
alias: {
'\.svg$': path.resolve(__dirname, 'fileMock.js'), // Use absolute path to fileMock.js
},
Beta Was this translation helpful? Give feedback.
All reactions