Skip to content

Commit 84e403a

Browse files
authored
fix(rstest): bugs (#10844)
1 parent 7566798 commit 84e403a

File tree

5 files changed

+54
-29
lines changed

5 files changed

+54
-29
lines changed

crates/rspack_plugin_rstest/src/import_dependency.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub fn module_id_rstest(
8484
}
8585
}
8686

87-
const WEBPACK_REQUIRE_IMPORT_ACTUAL: &str = "__webpack_require__.import_actual";
87+
const WEBPACK_REQUIRE_IMPORT_ACTUAL: &str = "__webpack_require__.rstest_import_actual";
8888

8989
// To support use `__webpack_require__.import_actual` for `importActual`.
9090
fn module_namespace_promise_rstest(

crates/rspack_plugin_rstest/src/mock_method_dependency.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub struct MockMethodDependency {
2323
pub enum MockMethod {
2424
Mock,
2525
Unmock,
26+
Hoisted,
2627
}
2728

2829
impl MockMethodDependency {
@@ -71,7 +72,6 @@ impl DependencyTemplate for MockMethodDependencyTemplate {
7172
code_generatable_context: &mut TemplateContext,
7273
) {
7374
let TemplateContext { init_fragments, .. } = code_generatable_context;
74-
7575
let dep = dep
7676
.as_any()
7777
.downcast_ref::<MockMethodDependency>()
@@ -81,18 +81,19 @@ impl DependencyTemplate for MockMethodDependencyTemplate {
8181
let hoist_flag = match dep.method {
8282
MockMethod::Mock => "MOCK",
8383
MockMethod::Unmock => "UNMOCK",
84+
MockMethod::Hoisted => "HOISTED",
8485
};
8586

8687
let mock_method = match dep.method {
87-
MockMethod::Mock => "set_mock",
88-
MockMethod::Unmock => "unmock",
88+
MockMethod::Mock => "rstest_set_mock",
89+
MockMethod::Unmock => "rstest_unmock",
90+
MockMethod::Hoisted => "rstest_hoisted",
8991
};
9092

9193
if dep.hoist {
92-
// Placeholder of hoist target.
9394
let init = NormalInitFragment::new(
9495
format!("/* RSTEST:{hoist_flag}_PLACEHOLDER:{request} */;"),
95-
InitFragmentStage::StageConstants,
96+
InitFragmentStage::StageESMImports,
9697
0,
9798
InitFragmentKey::Const(format!("retest mock_hoist {request}")),
9899
None,

crates/rspack_plugin_rstest/src/parser_plugin.rs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl RstestParserPlugin {
9191
.presentational_dependencies
9292
.push(Box::new(ConstDependency::new(
9393
range,
94-
".require_actual".into(),
94+
".rstest_require_actual".into(),
9595
None,
9696
)));
9797

@@ -158,7 +158,7 @@ impl RstestParserPlugin {
158158
.unwrap_or(value.as_ref())
159159
.to_string(),
160160
);
161-
let is_relative_request = path_buf.starts_with("."); // TODO: consider alias?
161+
let is_relative_request = path_buf.to_string().starts_with("."); // TODO: consider alias?
162162
let mocked_target = if is_relative_request {
163163
// Mock relative request to alongside `__mocks__` directory.
164164
path_buf
@@ -287,14 +287,33 @@ impl RstestParserPlugin {
287287
}
288288
}
289289

290+
fn hoisted(&self, parser: &mut JavascriptParser, call_expr: &CallExpr) {
291+
match call_expr.args.len() {
292+
1 => {
293+
parser
294+
.presentational_dependencies
295+
.push(Box::new(MockMethodDependency::new(
296+
call_expr.span(),
297+
call_expr.callee.span(),
298+
call_expr.span().real_lo().to_string(),
299+
true,
300+
MockMethod::Hoisted,
301+
)));
302+
}
303+
_ => {
304+
panic!("`rs.hoisted` function expects 1 argument, got more than 1");
305+
}
306+
}
307+
}
308+
290309
fn reset_modules(&self, parser: &mut JavascriptParser, call_expr: &CallExpr) -> Option<bool> {
291310
match call_expr.args.len() {
292311
0 => {
293312
parser
294313
.presentational_dependencies
295314
.push(Box::new(ConstDependency::new(
296315
call_expr.callee.span().into(),
297-
"__webpack_require__.reset_modules".into(),
316+
"__webpack_require__.rstest_reset_modules".into(),
298317
None,
299318
)));
300319
Some(true)
@@ -417,56 +436,61 @@ impl JavascriptParserPlugin for RstestParserPlugin {
417436
if let Some(prop) = q.prop.as_ident() {
418437
match (ident.sym.as_str(), prop.sym.as_str()) {
419438
// rs.mock
420-
("rs", "mock") => {
439+
("rs", "mock") | ("rstest", "mock") => {
421440
self.process_mock(parser, call_expr, true, true, MockMethod::Mock, true);
422441
return Some(false);
423442
}
424443
// rs.mockRequire
425-
("rs", "mockRequire") => {
444+
("rs", "mockRequire") | ("rstest", "mockRequire") => {
426445
self.process_mock(parser, call_expr, true, false, MockMethod::Mock, true);
427446
return Some(false);
428447
}
429448
// rs.doMock
430-
("rs", "doMock") => {
449+
("rs", "doMock") | ("rstest", "doMock") => {
431450
self.process_mock(parser, call_expr, false, true, MockMethod::Mock, true);
432451
return Some(false);
433452
}
434453
// rs.doMockRequire
435-
("rs", "doMockRequire") => {
454+
("rs", "doMockRequire") | ("rstest", "doMockRequire") => {
436455
self.process_mock(parser, call_expr, false, false, MockMethod::Mock, true);
437456
return Some(false);
438457
}
439458
// rs.importActual
440-
("rs", "importActual") => {
459+
("rs", "importActual") | ("rstest", "importActual") => {
441460
return self.process_import_actual(parser, call_expr);
442461
}
443462
// rs.requireActual
444-
("rs", "requireActual") => {
463+
("rs", "requireActual") | ("rstest", "requireActual") => {
445464
return self.process_require_actual(parser, call_expr);
446465
}
447466
// rs.importMock
448-
("rs", "importMock") => {
467+
("rs", "importMock") | ("rstest", "importMock") => {
449468
return self.load_mock(parser, call_expr, true);
450469
}
451470
// rs.requireMock
452-
("rs", "requireMock") => {
471+
("rs", "requireMock") | ("rstest", "requireMock") => {
453472
return self.load_mock(parser, call_expr, false);
454473
}
455474
// rs.unmock
456-
("rs", "unmock") => {
475+
("rs", "unmock") | ("rstest", "unmock") => {
457476
self.process_mock(parser, call_expr, true, true, MockMethod::Unmock, false);
458477
return Some(true);
459478
}
460479
// rs.doUnmock
461-
("rs", "doUnmock") => {
480+
("rs", "doUnmock") | ("rstest", "doUnmock") => {
462481
// return self.unmock_method(parser, call_expr, true);
463482
self.process_mock(parser, call_expr, false, true, MockMethod::Unmock, false);
464483
return Some(true);
465484
}
466485
// rs.resetModules
467-
("rs", "resetModules") => {
486+
("rs", "resetModules") | ("rstest", "resetModules") => {
468487
return self.reset_modules(parser, call_expr);
469488
}
489+
// rs.hoisted
490+
("rs", "hoisted") | ("rstest", "hoisted") => {
491+
self.hoisted(parser, call_expr);
492+
return Some(true);
493+
}
470494
_ => {
471495
// Not a mock module, continue.
472496
return None;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { foo } from './src/barrel'
22

3-
rs.mock('./src/foo')
3+
rstest.mock('./src/foo')
44

55
it('importActual should works', async () => {
66
expect(foo).toBe('mocked_foo')
7-
const originalFoo = await rs.importActual('./src/foo')
7+
const originalFoo = await rstest.importActual('./src/foo')
88
expect(originalFoo.value).toBe('foo')
99
})

packages/rspack-test-tools/tests/configCases/rstest/mock/rspack.config.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,24 @@ if (typeof __webpack_require__ === 'undefined') {
1919
return;
2020
}
2121
22-
__webpack_require__.before_mocked_modules = {};
22+
__webpack_require__.rstest_original_modules = {};
2323
24-
__webpack_require__.reset_modules = () => {
24+
__webpack_require__.rstest_reset_modules = () => {
2525
__webpack_module_cache__ = {};
2626
}
2727
28-
__webpack_require__.unmock = (id) => {
28+
__webpack_require__.rstest_unmock = (id) => {
2929
delete __webpack_module_cache__[id]
3030
}
3131
32-
__webpack_require__.import_actual = __webpack_require__.require_actual = (id) => {
33-
const beforeMock = __webpack_require__.before_mocked_modules[id];
32+
__webpack_require__.rstest_import_actual = __webpack_require__.rstest_require_actual = (id) => {
33+
const beforeMock = __webpack_require__.rstest_original_modules[id];
3434
return beforeMock;
3535
}
3636
37-
__webpack_require__.set_mock = (id, modFactory) => {
37+
__webpack_require__.rstest_set_mock = (id, modFactory) => {
3838
if (typeof modFactory === 'string' || typeof modFactory === 'number') {
39-
__webpack_require__.before_mocked_modules[id] = __webpack_require__(id);
39+
__webpack_require__.rstest_original_modules[id] = __webpack_require__(id);
4040
__webpack_module_cache__[id] = { exports: __webpack_require__(modFactory) };
4141
} else if (typeof modFactory === 'function') {
4242
__webpack_module_cache__[id] = { exports: modFactory() };

0 commit comments

Comments
 (0)