Skip to content

Commit a5fdcc1

Browse files
committed
Add tests for new CSS modules CLI features
1 parent 96f419f commit a5fdcc1

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ struct CliArgs {
2929
custom_media: bool,
3030
/// Enable CSS modules in output.
3131
/// If no filename is provided, <output_file>.json will be used.
32+
/// If no --output-file is specified, code and exports will be printed to stdout as JSON.
3233
#[clap(long, group = "css_modules")]
3334
css_modules: Option<Option<String>>,
3435
#[clap(long, requires = "css_modules")]
@@ -174,7 +175,7 @@ pub fn main() -> Result<(), std::io::Error> {
174175
"{}",
175176
serde_json::json!({
176177
"code": res.code,
177-
"modules": exports
178+
"exports": exports
178179
})
179180
);
180181
} else {

tests/cli_integration_tests.rs

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@ fn css_module_test_vals() -> (String, String, String) {
9292
"#
9393
.into(),
9494
indoc! {r#"
95-
.foo_EgL3uq {
95+
.EgL3uq_foo {
9696
color: red;
9797
}
9898
99-
#id_EgL3uq {
100-
animation: test_EgL3uq 2s;
99+
#EgL3uq_id {
100+
animation: EgL3uq_test 2s;
101101
}
102102
103-
@keyframes test_EgL3uq {
103+
@keyframes EgL3uq_test {
104104
from {
105105
color: red;
106106
}
@@ -110,15 +110,15 @@ fn css_module_test_vals() -> (String, String, String) {
110110
}
111111
}
112112
113-
@counter-style circles_EgL3uq {
113+
@counter-style EgL3uq_circles {
114114
symbols: Ⓐ Ⓑ Ⓒ;
115115
}
116116
117117
ul {
118-
list-style: circles_EgL3uq;
118+
list-style: EgL3uq_circles;
119119
}
120120
121-
@keyframes fade_EgL3uq {
121+
@keyframes EgL3uq_fade {
122122
from {
123123
opacity: 0;
124124
}
@@ -278,6 +278,41 @@ fn css_modules_output_target_option() -> Result<(), Box<dyn std::error::Error>>
278278
Ok(())
279279
}
280280

281+
#[test]
282+
fn css_modules_stdout() -> Result<(), Box<dyn std::error::Error>> {
283+
let (input, out_code, exports) = css_module_test_vals();
284+
let infile = assert_fs::NamedTempFile::new("test.css")?;
285+
infile.write_str(&input)?;
286+
let mut cmd = Command::cargo_bin("parcel_css")?;
287+
cmd.current_dir(infile.path().parent().unwrap());
288+
cmd.arg(infile.path());
289+
cmd.arg("--css-modules");
290+
let assert = cmd.assert().success();
291+
let output = assert.get_output();
292+
293+
let expected: serde_json::Value = serde_json::from_str(&exports)?;
294+
let actual: serde_json::Value = serde_json::from_slice(&output.stdout)?;
295+
assert_eq!(out_code, actual.pointer("/code").unwrap().as_str().unwrap());
296+
assert_eq!(&expected, actual.pointer("/exports").unwrap());
297+
298+
Ok(())
299+
}
300+
301+
#[test]
302+
fn css_modules_pattern() -> Result<(), Box<dyn std::error::Error>> {
303+
let (input, _, _) = css_module_test_vals();
304+
let infile = assert_fs::NamedTempFile::new("test.css")?;
305+
infile.write_str(&input)?;
306+
let mut cmd = Command::cargo_bin("parcel_css")?;
307+
cmd.current_dir(infile.path().parent().unwrap());
308+
cmd.arg(infile.path());
309+
cmd.arg("--css-modules");
310+
cmd.arg("--css-modules-pattern").arg("[name]-[hash]-[local]");
311+
cmd.assert().success().stdout(predicate::str::contains("test-EgL3uq-foo"));
312+
313+
Ok(())
314+
}
315+
281316
#[test]
282317
fn sourcemap() -> Result<(), Box<dyn std::error::Error>> {
283318
let (input, _, _) = css_module_test_vals();

0 commit comments

Comments
 (0)