Skip to content
This repository was archived by the owner on Jan 6, 2024. It is now read-only.

Commit f48e620

Browse files
committed
test: add jest snapshot test for icss-compatible output
1 parent 8f82614 commit f48e620

File tree

6 files changed

+2699
-4
lines changed

6 files changed

+2699
-4
lines changed

__tests__/__data__/test.pcss

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
.parent {
2+
color: red;
3+
4+
& .child {
5+
color: green;
6+
}
7+
}
8+
9+
.list {
10+
color: red;
11+
12+
&_item {
13+
color: green;
14+
}
15+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Generates proper output for icss-utils: test-icss-compatible-output 1`] = `
4+
".test_parent_HASH {
5+
color: red
6+
}
7+
8+
.test_parent_HASH .test_child_HASH {
9+
color: green;
10+
}
11+
12+
.test_list_HASH {
13+
color: red
14+
}
15+
16+
.test_list_item_HASH {
17+
color: green;
18+
}
19+
20+
:export {
21+
parent: test_parent_HASH;
22+
child: test_child_HASH;
23+
list: test_list_HASH;
24+
list_item: test_list_item_HASH
25+
}
26+
"
27+
`;

__tests__/index.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
const { test, expect } = require("@jest/globals");
4+
const postcss = require("postcss");
5+
const nestedOnce = require("../index");
6+
const localByDefault = require("postcss-modules-local-by-default");
7+
const modulesScope = require("postcss-modules-scope");
8+
9+
test("Generates proper output for icss-utils", async () => {
10+
const from = path.join(__dirname, "__data__", "test.pcss");
11+
const contents = fs.readFileSync(from);
12+
13+
const postcssInstance = postcss([
14+
nestedOnce({ preserveEmpty: true }),
15+
localByDefault({ mode: "local" }),
16+
modulesScope({
17+
generateScopedName: (local, file) => {
18+
const name = path.basename(file, ".pcss");
19+
return `${name}_${local}_HASH`;
20+
},
21+
}),
22+
]);
23+
24+
const result = await postcssInstance.process(contents, { from: from });
25+
26+
expect(result.css).toMatchSnapshot("test-icss-compatible-output");
27+
});

__tests__/test-postcss.cjs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const potcss = require('postcss');
4+
const nestedOnce = require('../index');
5+
const modulesValues = require('postcss-modules-values');
6+
const localByDefault = require('postcss-modules-local-by-default');
7+
const extractImports = require('postcss-modules-extract-imports');
8+
const modulesScope = require('postcss-modules-scope');
9+
10+
11+
const FROM = path.join(__dirname, 'test.pcss');
12+
const TO = path.join(__dirname, 'test-postcss.css');
13+
const contents = fs.readFileSync(FROM);
14+
15+
16+
potcss([
17+
nestedOnce({ preserveEmpty: true }),
18+
modulesValues({}),
19+
localByDefault({ mode: 'local' }),
20+
extractImports(),
21+
modulesScope({
22+
generateScopedName: (local, file, css) => {
23+
const { dir, name, base } = path.parse(file);
24+
return `${name}_${local}_HASH`;
25+
},
26+
}),
27+
])
28+
.process(contents, { from: FROM, to: TO })
29+
.then((result) => {
30+
fs.writeFileSync(TO, result.css);
31+
if (result.map) {
32+
fs.writeFileSync(`${TO}.map`, result.map.toString());
33+
}
34+
});

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"postcss",
77
"css",
88
"postcss-plugin",
9+
"postcss-nested",
910
"sass",
1011
"nested"
1112
],
@@ -25,7 +26,11 @@
2526
"postcss": "^8.3.5"
2627
},
2728
"devDependencies": {
29+
"@types/jest": "^26.0.23",
30+
"jest": "^27.0.6",
2831
"postcss": "^8.3.5",
32+
"postcss-modules-local-by-default": "^4.0.0",
33+
"postcss-modules-scope": "^3.0.0",
2934
"prettier": "^2.3.2"
3035
}
3136
}

0 commit comments

Comments
 (0)