Skip to content

Commit 221fc16

Browse files
committed
fix(rstest): bugs
1 parent 28af80d commit 221fc16

File tree

5 files changed

+27
-28
lines changed

5 files changed

+27
-28
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: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,14 @@ impl DependencyTemplate for MockMethodDependencyTemplate {
8484
};
8585

8686
let mock_method = match dep.method {
87-
MockMethod::Mock => "set_mock",
88-
MockMethod::Unmock => "unmock",
87+
MockMethod::Mock => "rstest_set_mock",
88+
MockMethod::Unmock => "rstest_unmock",
8989
};
9090

9191
if dep.hoist {
92-
// Placeholder of hoist target.
9392
let init = NormalInitFragment::new(
9493
format!("/* RSTEST:{hoist_flag}_PLACEHOLDER:{request} */;"),
95-
InitFragmentStage::StageConstants,
94+
InitFragmentStage::StageESMImports,
9695
0,
9796
InitFragmentKey::Const(format!("retest mock_hoist {request}")),
9897
None,

crates/rspack_plugin_rstest/src/parser_plugin.rs

Lines changed: 14 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
@@ -294,7 +294,7 @@ impl RstestParserPlugin {
294294
.presentational_dependencies
295295
.push(Box::new(ConstDependency::new(
296296
call_expr.callee.span().into(),
297-
"__webpack_require__.reset_modules".into(),
297+
"__webpack_require__.rstest_reset_modules".into(),
298298
None,
299299
)));
300300
Some(true)
@@ -417,54 +417,54 @@ impl JavascriptParserPlugin for RstestParserPlugin {
417417
if let Some(prop) = q.prop.as_ident() {
418418
match (ident.sym.as_str(), prop.sym.as_str()) {
419419
// rs.mock
420-
("rs", "mock") => {
420+
("rs", "mock") | ("rstest", "mock") => {
421421
self.process_mock(parser, call_expr, true, true, MockMethod::Mock, true);
422422
return Some(false);
423423
}
424424
// rs.mockRequire
425-
("rs", "mockRequire") => {
425+
("rs", "mockRequire") | ("rstest", "mockRequire") => {
426426
self.process_mock(parser, call_expr, true, false, MockMethod::Mock, true);
427427
return Some(false);
428428
}
429429
// rs.doMock
430-
("rs", "doMock") => {
430+
("rs", "doMock") | ("rstest", "doMock") => {
431431
self.process_mock(parser, call_expr, false, true, MockMethod::Mock, true);
432432
return Some(false);
433433
}
434434
// rs.doMockRequire
435-
("rs", "doMockRequire") => {
435+
("rs", "doMockRequire") | ("rstest", "doMockRequire") => {
436436
self.process_mock(parser, call_expr, false, false, MockMethod::Mock, true);
437437
return Some(false);
438438
}
439439
// rs.importActual
440-
("rs", "importActual") => {
440+
("rs", "importActual") | ("rstest", "importActual") => {
441441
return self.process_import_actual(parser, call_expr);
442442
}
443443
// rs.requireActual
444-
("rs", "requireActual") => {
444+
("rs", "requireActual") | ("rstest", "requireActual") => {
445445
return self.process_require_actual(parser, call_expr);
446446
}
447447
// rs.importMock
448-
("rs", "importMock") => {
448+
("rs", "importMock") | ("rstest", "importMock") => {
449449
return self.load_mock(parser, call_expr, true);
450450
}
451451
// rs.requireMock
452-
("rs", "requireMock") => {
452+
("rs", "requireMock") | ("rstest", "requireMock") => {
453453
return self.load_mock(parser, call_expr, false);
454454
}
455455
// rs.unmock
456-
("rs", "unmock") => {
456+
("rs", "unmock") | ("rstest", "unmock") => {
457457
self.process_mock(parser, call_expr, true, true, MockMethod::Unmock, false);
458458
return Some(true);
459459
}
460460
// rs.doUnmock
461-
("rs", "doUnmock") => {
461+
("rs", "doUnmock") | ("rstest", "doUnmock") => {
462462
// return self.unmock_method(parser, call_expr, true);
463463
self.process_mock(parser, call_expr, false, true, MockMethod::Unmock, false);
464464
return Some(true);
465465
}
466466
// rs.resetModules
467-
("rs", "resetModules") => {
467+
("rs", "resetModules") | ("rstest", "resetModules") => {
468468
return self.reset_modules(parser, call_expr);
469469
}
470470
_ => {
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)