Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
100 commits
Select commit Hold shift + click to select a range
2943ccf
feat(experimental): allow disabling the module runner
sheremet-va Dec 8, 2025
085be59
fix: throw an error in vmForks/vmThreads
sheremet-va Dec 8, 2025
c25402a
feat: implement nativeModuleRunner
sheremet-va Dec 8, 2025
318eb6b
chore: cleanup types
sheremet-va Dec 8, 2025
0611b57
fix: watcher respects non-client/ssr environments
sheremet-va Dec 8, 2025
3e4caa2
feat: support watch mode
sheremet-va Dec 8, 2025
4e5464b
fix: support module.register
sheremet-va Dec 8, 2025
3c40971
feat: support import.meta.vitest
sheremet-va Dec 8, 2025
c047490
chore: cli-config
sheremet-va Dec 8, 2025
b83f29c
chore: add sample project
sheremet-va Dec 9, 2025
3fd5618
chore: cleanup
sheremet-va Dec 9, 2025
e4b0d2e
chore: cleanup
sheremet-va Dec 9, 2025
fe86182
feat: support automock, autospy and redirect mock types
sheremet-va Dec 9, 2025
3f7e2b3
fix: show module as external in UI
sheremet-va Dec 9, 2025
2273518
fix: run setup file for every test, execute in-source tests as separa…
sheremet-va Dec 9, 2025
6c44609
feat: first implementation of factory mocking
sheremet-va Dec 10, 2025
4091d0d
chore: collect
sheremet-va Dec 12, 2025
0926f28
chore: refactor native module mocker
sheremet-va Dec 12, 2025
f76caa0
refactor: cleanup exports collection
sheremet-va Dec 12, 2025
d6f72bd
fix: support importActual and recursive factory
sheremet-va Dec 12, 2025
086c47c
fix: support vi.importMock
sheremet-va Dec 12, 2025
06a351e
fix: support mocking deps
sheremet-va Dec 12, 2025
8b4f29b
fix(mocker): support top level import if dependency is not circular
sheremet-va Dec 15, 2025
f50aa81
fix: allow `export *` when automocking
sheremet-va Dec 15, 2025
a4e603e
test: add more tests
sheremet-va Dec 15, 2025
d458c67
fix: update loading errors
sheremet-va Dec 15, 2025
34f1bb3
Merge branch 'main' of github.com:vitest-dev/vitest into 12-08-feat_e…
sheremet-va Dec 15, 2025
d3db1bf
chore: cleanup
sheremet-va Dec 15, 2025
b653ee3
chore: cleanup
sheremet-va Dec 15, 2025
0ab2dab
fix: override is false by default
sheremet-va Dec 16, 2025
69ca0d8
chore: cleanup
sheremet-va Dec 16, 2025
4786dd1
refactor: move the example to test/
sheremet-va Dec 16, 2025
5bb68aa
docs: cleanup
sheremet-va Dec 16, 2025
74a84c4
chore: add try/catch
sheremet-va Dec 17, 2025
c81d363
Merge branch 'main' of github.com:vitest-dev/vitest into 12-08-feat_e…
sheremet-va Dec 17, 2025
f7def15
fix: listen for unhandled errors in vm pool
sheremet-va Dec 17, 2025
3f96c63
fix(windows): support circular manual mock
sheremet-va Dec 17, 2025
f8ab6a5
chore: replace #test-loader with #nodejs-worker-loader
sheremet-va Dec 18, 2025
34350d6
chore: remove a warning hint
sheremet-va Dec 18, 2025
fda5d88
docs: mention it works only in forks/threads
sheremet-va Dec 18, 2025
fa80150
chore: nitpicks
sheremet-va Dec 18, 2025
f8c558d
test: add imports from basic.ts
sheremet-va Dec 18, 2025
3ded894
fix: move mock=actual resolution to worker loader
sheremet-va Dec 18, 2025
d99af1d
fix: load custom env with a regular import
sheremet-va Dec 18, 2025
f0c1ae2
chore: lint
sheremet-va Dec 18, 2025
861e0d1
fix: wrong default
sheremet-va Dec 18, 2025
a5dc591
fix: load automocked builtins properly
sheremet-va Dec 18, 2025
c48303e
chore: support import on windows
sheremet-va Dec 18, 2025
a097b32
Merge branch 'main' of github.com:vitest-dev/vitest into 12-08-feat_e…
sheremet-va Dec 19, 2025
d21b3a8
chore: use `process.setSourceMapsEnabled` if available
sheremet-va Dec 22, 2025
694f9e9
chore: document limitation of mocking builtins
sheremet-va Dec 22, 2025
3c5f534
chore: don't mock anything inside node_modules for now
sheremet-va Dec 22, 2025
bf901f3
Merge branch 'main' of github.com:vitest-dev/vitest into 12-08-feat_e…
sheremet-va Dec 27, 2025
9bb9ef7
chore: add a comment
sheremet-va Dec 27, 2025
f1f1d72
chore: review
sheremet-va Jan 12, 2026
d47477e
Merge branch 'main' of github.com:vitest-dev/vitest into 12-08-feat_e…
sheremet-va Jan 12, 2026
892e8e2
chore: cleanup
sheremet-va Jan 12, 2026
ea60b20
fix: check before ?
sheremet-va Jan 12, 2026
afe7ef9
chore: lockfile
sheremet-va Jan 12, 2026
485f627
chore: add `describe`
sheremet-va Jan 12, 2026
fa5674e
chore: cleanup
sheremet-va Jan 12, 2026
01d64d0
chore: cleanup
sheremet-va Jan 12, 2026
7206421
test: add a cts test
sheremet-va Jan 12, 2026
2df65e4
chore: cleanup
sheremet-va Jan 12, 2026
cca67da
refactor: use a util to run no-module-runner tests
sheremet-va Jan 12, 2026
b84b160
Merge branch 'main' of github.com:vitest-dev/vitest into 12-08-feat_e…
sheremet-va Jan 13, 2026
2dc04b9
chore: cleanup
sheremet-va Jan 13, 2026
8586581
Merge branch 'main' of github.com:vitest-dev/vitest into 12-08-feat_e…
sheremet-va Jan 14, 2026
75f7d04
test: move native tests
sheremet-va Jan 14, 2026
f4a8bb4
chore: fix v8 support
sheremet-va Jan 14, 2026
630c595
chore: cleanup
sheremet-va Jan 14, 2026
5dc2d19
chore: use local-pkg instead
sheremet-va Jan 14, 2026
2f6680d
test: add v8 coverage test
sheremet-va Jan 14, 2026
af5f13a
chore: add comment
sheremet-va Jan 14, 2026
4a65754
test: add watch and snapshot tests
sheremet-va Jan 14, 2026
1dcf484
fix: respect transform mode
sheremet-va Jan 14, 2026
229d3d7
chore: lint
sheremet-va Jan 14, 2026
89b324b
test: remove only
sheremet-va Jan 14, 2026
3dd5e62
chore: pass down url
sheremet-va Jan 14, 2026
d2fcd47
chore: convert to path
sheremet-va Jan 14, 2026
79064fc
chore: cleanup
sheremet-va Jan 15, 2026
be09438
chore: cleanup
sheremet-va Jan 15, 2026
e9512ae
chore: fix query params
sheremet-va Jan 15, 2026
cb4a7ed
test: make coverage test more stable
sheremet-va Jan 15, 2026
2dc9546
chore: oops
sheremet-va Jan 15, 2026
2cf4de4
docs: add notes
sheremet-va Jan 15, 2026
8155942
test(coverage): native runner with v8
AriPerkkio Jan 19, 2026
41d181e
fix: proper `startOffset` when `import.meta.vitest` without module ru…
AriPerkkio Jan 19, 2026
045179e
Merge branch 'main' of github.com:vitest-dev/vitest into 12-08-feat_e…
sheremet-va Jan 19, 2026
f27507b
Merge branch 'main' of github.com:vitest-dev/vitest into 12-08-feat_e…
sheremet-va Jan 21, 2026
04ae041
test(coverage): exclude node 20 tests if registerHooks is not available
sheremet-va Jan 21, 2026
8b10b8a
fix: use IMPORT_META_TEST() function instead of appending IMPORT_META…
sheremet-va Jan 21, 2026
85ba069
test: don't process node_modules
sheremet-va Jan 21, 2026
74d009a
chore: include vitest/cjs-lib in deps optimisation
sheremet-va Jan 21, 2026
5d9db1a
test: remove try/catch
sheremet-va Jan 21, 2026
33750a0
test: pass down skip
sheremet-va Jan 21, 2026
6c663fd
refactor(coverage): what is this madness
sheremet-va Jan 21, 2026
6c6d6ba
chore: cleanup
sheremet-va Jan 21, 2026
cf69ac8
test: be conservative with what is included
sheremet-va Jan 21, 2026
a6fa2ab
test: fix dynamic test import
sheremet-va Jan 21, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: add try/catch
  • Loading branch information
sheremet-va committed Dec 17, 2025
commit 74a84c4a0686590a0ff1524e5281504744c34a1b
9 changes: 5 additions & 4 deletions packages/mocker/src/node/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ export function resolveModuleFormat(url: string, code: string): 'module' | 'comm
return 'commonjs'
}
else {
// Ambiguous input! Check if it has ESM syntax. Node.js is much smarter here
// Ambiguous input! Check if it has ESM syntax. Node.js is much smarter here,
// but we don't need to run the code, so we can be more relaxed
if (hasESM(filterOutComments(code))) {
return 'module'
}
Expand All @@ -156,10 +157,10 @@ export function resolveModuleFormat(url: string, code: string): 'module' | 'comm
return undefined
}

let __require: NodeJS.Require | undefined
let __globalRequire: NodeJS.Require | undefined
function getBuiltinModule(moduleId: string) {
__require ??= module.createRequire(import.meta.url)
return __require(moduleId)
__globalRequire ??= module.createRequire(import.meta.url)
return __globalRequire(moduleId)
}

const ESM_RE
Expand Down
64 changes: 37 additions & 27 deletions packages/vitest/src/runtime/moduleRunner/nativeModuleMocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,28 @@ export { __${index} as "${key}" }`.trim()
return
}

const ms = automockModule(
transformedCode,
mockType,
code => parse(code, {
sourceType: 'module',
ecmaVersion: 'latest',
}),
{ id: moduleId },
)
const transformed = ms.toString()
const map = ms.generateMap({ hires: 'boundary', source: moduleId })
const code = `${transformed}\n//# sourceMappingURL=${genSourceMapUrl(map)}`
try {
const ms = automockModule(
transformedCode,
mockType,
code => parse(code, {
sourceType: 'module',
ecmaVersion: 'latest',
}),
{ id: moduleId },
)
const transformed = ms.toString()
const map = ms.generateMap({ hires: 'boundary', source: moduleId })
const code = `${transformed}\n//# sourceMappingURL=${genSourceMapUrl(map)}`

return {
format: 'module',
source: code,
shortCircuit: true,
return {
format: 'module',
source: code,
shortCircuit: true,
}
}
catch (cause) {
throw new Error(`Cannot automock '${url}' because it failed to parse.`, { cause })
}
}

Expand Down Expand Up @@ -128,30 +133,35 @@ export { __${index} as "${key}" }`.trim()
return
}

// since the factory returned an async result, we have to figure out keys synchronosly somehow
// so we parse the module with es/cjs-module-lexer to find the original exports -- we assume the same ones are returned
// injecting new keys is not supported (and should not be advised anyway)

const source = result.source.toString()
const transformedCode = transformCode(source, result.format || 'module', moduleId)
if (transformedCode == null) {
return
}

const format = result.format?.startsWith('module') ? 'module' : 'commonjs'
const exports = collectModuleExports(moduleId, transformedCode, format)
const manualMockedModule = createManualModuleSource(moduleId, exports)
try {
// we parse the module with es/cjs-module-lexer to find the original exports -- we assume the same ones are returned from the factory
// injecting new keys is not supported (and should not be advised anyway)
const exports = collectModuleExports(moduleId, transformedCode, format)
const manualMockedModule = createManualModuleSource(moduleId, exports)

return {
format: 'module',
source: manualMockedModule,
shortCircuit: true,
return {
format: 'module',
source: manualMockedModule,
shortCircuit: true,
}
}
catch (cause) {
throw new Error(`Failed to mock '${url}'. See the cause for more information.`, { cause })
}
}

public checkCircularManualMock(url: string): void {
const filename = url.startsWith('file://') ? fileURLToPath(url) : url
const id = cleanUrl(normalizeModuleId(filename))
// the module is mocked and requested a second time, let's resolve
// the factory function that will redefine the exports later
if (this.originalModulePromises.has(id)) {
const factoryPromise = this.factoryPromises.get(id)
this.originalModulePromises.get(id)?.resolve({ __factoryPromise: factoryPromise })
Expand All @@ -172,7 +182,7 @@ export { __${index} as "${key}" }`.trim()

const mockResult = mock.resolve()
if (mockResult instanceof Promise) {
// to avoid circular dependency, we resolve this function as {__factoryPromise}
// to avoid circular dependency, we resolve this function as {__factoryPromise} in `checkCircularManualMock`
// when it's requested the second time. then the exports are exposed as `undefined`,
// but later redefined when the promise is actually resolved
const promise = createDefer()
Expand Down