Skip to content

Commit

Permalink
feat(es/testing): Use __swc_snapshots__ for test! macro (#8191)
Browse files Browse the repository at this point in the history
**Description:**

This implements something similar to `__snapshots__` of `jest`. Instead of storing the expected result as a string literal, we now store it in `$crate/tests/__swc_snapshots__/$path_to_test__$test_name.js`.

**Related issue:**

 - Closes #4509
  • Loading branch information
kdy1 committed Oct 30, 2023
1 parent 769bc5b commit 0aafa75
Show file tree
Hide file tree
Showing 1,177 changed files with 12,268 additions and 17,684 deletions.
36 changes: 0 additions & 36 deletions crates/swc_ecma_compat_bugfixes/src/async_arrows_in_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,6 @@ mod tests {
bar() {
(async () => { })();
}
}"#,
r#"
class Foo {
constructor() {
this.x = async function () {
return await 1;
};
}
bar() {
(async function () {})();
}
}"#
);

Expand All @@ -152,14 +140,6 @@ mod tests {
foo() {
bar(async () => await 1);
}
}"#,
r#"
class Foo {
foo() {
bar(async function () {
return await 1;
});
}
}"#
);

Expand All @@ -172,15 +152,6 @@ mod tests {
constructor() {
this.x = () => async () => await this;
}
}"#,
r#"
class Foo {
constructor() {
var _this = this;
this.x = () => async function () {
return await _this;
};
}
}"#
);

Expand All @@ -192,12 +163,6 @@ mod tests {
|_| tr(),
non_async_arrow,
r#"
class Foo {
constructor() {
this.x = () => {};
}
}"#,
r#"
class Foo {
constructor() {
this.x = () => {};
Expand All @@ -209,7 +174,6 @@ mod tests {
::swc_ecma_parser::Syntax::default(),
|_| tr(),
non_class_async_arrow,
"let x = async () => await 1;",
"let x = async () => await 1;"
);
}
8 changes: 2 additions & 6 deletions crates/swc_ecma_compat_bugfixes/src/edge_default_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,31 +76,27 @@ mod tests {
::swc_ecma_parser::Syntax::default(),
|_| tr(),
destructured_default_value,
"const f = ({ a = 1 }) => a;",
"const f = ({ a: a = 1 }) => a;"
"const f = ({ a = 1 }) => a;"
);

test!(
::swc_ecma_parser::Syntax::default(),
|_| tr(),
destructured_no_default_value,
"const f = ({ a }) => a;",
"const f = ({ a }) => a;"
);

test!(
::swc_ecma_parser::Syntax::default(),
|_| tr(),
nested_default_value,
"const f = ({ a: { b = 1 } }) => [a, b];",
"const f = ({ a: { b: b = 1 } }) => [a, b];"
"const f = ({ a: { b = 1 } }) => [a, b];"
);

test!(
::swc_ecma_parser::Syntax::default(),
|_| tr(),
non_arguments,
"const f = () => { const { a = 1 } = {}; };",
"const f = () => { const { a = 1 } = {}; };"
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,36 +118,28 @@ mod tests {
basic,
"(function a ([a]) { a });
(function a({ ...a }) { a });
(function a({ a }) { a });",
"(function a([_a]) { _a; });
(function a({ ..._a }) { _a; });
(function a({ a: _a }) { _a; });"
(function a({ a }) { a });"
);

test!(
Syntax::default(),
|_| tr(),
avoid_collision_1,
"(function a([a, _a]) { a + _a })",
"(function a([_a1, _a]) {
_a1 + _a;
});"
"(function a([a, _a]) { a + _a })"
);

test!(
Syntax::default(),
|_| tr(),
use_duplicated_id,
"(function a([a]) { console.log(_a); })",
"(function a([_a1]) { console.log(_a); });"
"(function a([a]) { console.log(_a); })"
);

test!(
Syntax::default(),
|_| tr(),
avoid_collision_2,
"(function _a([_a]) { console.log(_a); })",
"(function _a([__a]) { console.log(__a); });"
"(function _a([_a]) { console.log(_a); })"
);

test!(
Expand All @@ -157,19 +149,14 @@ mod tests {
"let _a;
(function a([a]) {
_a = 3;
})",
"let _a;
(function a([_a1]) {
_a = 3;
})"
);

test!(
Syntax::default(),
|_| tr(),
assignment_expr_in_default_value,
"(function a([a = a = 3]) {})",
"(function a([_a = _a = 3]) {})"
"(function a([a = a = 3]) {})"
);

test!(
Expand All @@ -190,20 +177,6 @@ mod tests {
return _type_of();
}
",
"
export default function _type_of__2() {
if (Date__1.now() > 0) {
_type_of__2 = function _type_of__5() {
console__1.log(0);
};
} else {
_type_of__2 = function _type_of__8() {
console__1.log(2);
};
}
return _type_of__2();
}
"
);
}
65 changes: 8 additions & 57 deletions crates/swc_ecma_compat_bugfixes/src/template_literal_caching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,22 +169,14 @@ mod tests {
::swc_ecma_parser::Syntax::default(),
|_| tr(),
single_tag,
"t`a`;",
r#"
let _ = t1 => t1, t1;
t(t1 || (t1 = _`a`));
"#
"t`a`;"
);

test!(
::swc_ecma_parser::Syntax::default(),
|_| tr(),
single_tag_empty,
"x``;",
r#"
let _ = t => t, t;
x(t || (t = _``));
"#
"x``;"
);

test!(
Expand All @@ -194,67 +186,42 @@ mod tests {
r#"
t`a`;
x``;
"#,
r#"
let _ = t1 => t1, t1, t2;
t(t1 || (t1 = _`a`));
x(t2 || (t2 = _``));
"#
);

test!(
::swc_ecma_parser::Syntax::default(),
|_| tr(),
function_scoped_tag,
"const f = t => t`a`;",
r#"
let _ = t => t, t;
const f = t1 => t1(t || (t = _`a`));
"#
"const f = t => t`a`;"
);

test!(
::swc_ecma_parser::Syntax::default(),
|_| tr(),
dynamic_tag,
"fn()``;",
r#"
let _ = t => t, t;
fn()(t || (t = _``));
"#
"fn()``;"
);

test!(
::swc_ecma_parser::Syntax::default(),
|_| tr(),
dynamic_expressions,
"const f = t => t`a${1}b${t}${[\"hello\"]}`;",
r#"
let _ = t => t, t;
const f = t1 => t1(t || (t = _`a${0}b${0}${0}`), 1, t1, ["hello"]);
"#
"const f = t => t`a${1}b${t}${[\"hello\"]}`;"
);

test!(
::swc_ecma_parser::Syntax::default(),
|_| tr(),
same_tag_safari_11,
"x`a` === x`a`;",
r#"
let _ = t => t, t, t1;
x(t || (t = _`a`)) === x(t1 || (t1 = _`a`));
"#
"x`a` === x`a`;"
);

test!(
::swc_ecma_parser::Syntax::default(),
|_| tr(),
shared_strings_safari_11,
"x`a` === y`a`;",
r#"
let _ = t => t, t, t1;
x(t || (t = _`a`)) === y(t1 || (t1 = _`a`));
"#
"x`a` === y`a`;"
);

test!(
Expand All @@ -266,12 +233,6 @@ mod tests {
t(`a`);
t;
`a`;
"#,
r#"
`a`;
t(`a`);
t;
`a`;
"#
);

Expand All @@ -282,23 +243,13 @@ mod tests {
r#"
const _ = 1;
t``;
"#,
r#"
let _ = t1 => t1, t1;
const _1 = 1;
t(t1 || (t1 = _``));
"#
);

test!(
::swc_ecma_parser::Syntax::default(),
|_| tr(),
block_scoped_tag,
"for (let t of []) t`a`;",
r#"
let _ = t => t, t;
for (let t1 of []) t1(t || (t = _`a`));
"#
"for (let t of []) t`a`;"
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Foo {
constructor(){
this.x = async function() {
return await 1;
};
}
bar() {
(async function() {})();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Foo {
foo() {
bar(async function() {
return await 1;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Foo {
constructor(){
this.x = ()=>{};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let x = async ()=>await 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Foo {
constructor(){
var _this = this;
this.x = ()=>async function() {
return await _this;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const f = ({ a: a = 1 })=>a;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const f = ({ a })=>a;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const f = ({ a: { b: b = 1 } })=>[
a,
b
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const f = ()=>{
const { a = 1 } = {};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let _a;
(function a([_a1]) {
_a = 3;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(function a([_a = _a = 3]) {});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(function a([_a1, _a]) {
_a1 + _a;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(function _a([__a]) {
console.log(__a);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(function a([_a]) {
_a;
});
(function a({ ..._a }) {
_a;
});
(function a({ a: _a }) {
_a;
});

0 comments on commit 0aafa75

Please sign in to comment.