Comments (7)
But
createMockFromModule
is not available in Vitest, andimportMock
is not the same, as when used inside a manual mock, it just ends up importing the manual mock itself cyclicly.
Is this maybe a bug? Or if it's causing some infinite loop, that needs to be fixed at least?
from vitest.
Not an infinite loop, you are just getting your own module namespace object as far as I can tell. And I think importMock
is supposed to import manual mocks so not a bug as far as I can tell.
from vitest.
And I think
importMock
is supposed to import manual mocks so not a bug as far as I can tell.
It's called importMock
, which sounds like an opposite of importActual
https://vitest.dev/api/vi.html#vi-importactual but I thought the intent is simply "import actual + auto mocking" and could behave like Jest's createMockFromModule
https://jestjs.io/docs/jest-object#jestcreatemockfrommodulemodulename.
vitest/packages/vitest/src/runtime/mocker.ts
Lines 437 to 455 in e4e939b
Btw, can you setup a small reproduction to illustrate what you want to do with a concrete code? That would be helpful anyway as a test case if it's implemented. Also it would also help for us to suggest a workaround with what's currently possible.
One idea just came to my mind is to still use vi.mock
and overwrite some exports like this:
// use `setupFiles` to setup mock for all test files
import { vi, beforeEach } from "vitest"
import * as fooLib from "./foo";
// setup auto mocking for a whole module
vi.mock("./foo");
beforeEach(() => {
// then customize some named export
vi.mocked(fooLib).bar.mockImplementation(() => 'qux');
});
from vitest.
@hi-ogawa Here is an attempt to show what I'm trying to do, though a bit of a contrived example, I hope it shows what I'm trying to do: https://github.com/segevfiner/vitest-partial-manual-mock
from vitest.
I don't think we can support any dynamic imports for this use case because ESM needs to know every named import during parsing, but maybe we can support something like this:
// __mocks__/foo.js
export * from '../foo.js' with { mock: 'auto' }
export const bar = vi.fn(() => 'qux')
from vitest.
I don't think we can support any dynamic imports for this use case because ESM needs to know every named import during parsing, but maybe we can support something like this:
// __mocks__/foo.js export * from '../foo.js' with { mock: 'auto' } export const bar = vi.fn(() => 'qux')
AFAIK you can do dynamic imports in ESM, aka import()
, though it is async so you will need top level await, what's not supported is dynamic export, e.g. replace the entire exports of this module with the given object, which is why I had to use export =
which kinda fakes it by transpiling to CJS (module.exports = ...
), but a valid way of doing this without relying on some special handling of that syntax by Vitest might be needed as you suggested.
from vitest.
AFAIK you can do dynamic imports in ESM, aka
import()
, though it is async so you will need top level await, what's not supported is dynamic export
Yes, this is what I meant. The JS engine cannot parse the file to see all exported variables if you use a dynamic import because there is no mechanism to dynamically export variables.
e.g. replace the entire exports of this module with the given object, which is why I had to use export = which kinda fakes it by transpiling to CJS (module.exports = ...), but a valid way of doing this without relying on some special handling of that syntax by Vitest might be needed as you suggested.
This only works in Node.js runner because Vitest supports loading source code as CJS, this will not work in the browser (and module mocking is supported in the browser mode in the latest beta). This will also not work in future versions of Vitest because we cannot process files that were imported using require
(you can currently import this file, but you cannot import other things in this file without leaving Vitest module system because all imports are transformed into require
if it uses the CJS transform).
on some special handling of that syntax by Vitest might be needed as you suggested.
Also, export =
is not an ESM syntax and it requires special handling to process already. My proposal already works with the ESM syntax, and it only requires the processing of the foo
file, not the __mocks__/foo
file.
from vitest.
Related Issues (20)
- [ benchmark ] unable to use the json reporter HOT 2
- Take a screenshot if the test fails
- vi.fn() methods within vi.fn() methods HOT 1
- Allow multi-browser configuration
- `coverage.exclude` should be fully configurable
- Browser mode: No tests are running when using define in vitest config HOT 2
- Reporter live output for chess Perft scores HOT 3
- Store the last state of DOM before the test has finished
- Add option to exclude all files and folders mentioned in `.gitignore` HOT 5
- Relative Coverage Path HOT 2
- TypeScript accessor keyword throws rollup error: Unexpected token `...`. Expected * for generator, private key, identifier or async HOT 1
- iTerm2 clear screen does not clear the screen properly HOT 3
- Random tests failing due to "Fail to parse source" error HOT 1
- CLI flag to test given file or file/line instead of filtering by pattern HOT 1
- [bug] `ReferenceError: self is not defined` when running tests in `app/worker.ts` HOT 5
- Easily access failure screenshot after tests run HOT 1
- there is no file coverage if there are no tests for it HOT 2
- can't inline commonjs module when it's a sub-dependency HOT 1
- Error in Nuxt 3: Decorators are not valid here
- Exception stack trace line numbers are wrong in browser mode
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vitest.