Skip to content

Commit 8bdef13

Browse files
committed
test: add cli tests
1 parent 5769f5f commit 8bdef13

11 files changed

Lines changed: 144 additions & 14 deletions

File tree

.github/workflows/main.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ jobs:
2525
- name: Lint
2626
run: yarn lint
2727

28-
- name: Test
29-
run: yarn test --ci --coverage --maxWorkers=2
30-
3128
- name: Build
3229
run: yarn build
3330

31+
- name: Test
32+
run: yarn test --ci --coverage --maxWorkers=2
33+
3434
publish:
3535
needs: build
3636
runs-on: ubuntu-latest

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"**/tsdx/ts-jest": "27.1.3"
4242
},
4343
"devDependencies": {
44+
"@types/fs-extra": "^9.0.13",
4445
"@types/glob": "^7.2.0",
4546
"@types/node": "^17.0.13",
4647
"dripip": "^0.10.0",
@@ -55,6 +56,7 @@
5556
"bundle-require": "^3.0.2",
5657
"cac": "^6.7.12",
5758
"esbuild": "^0.14.14",
59+
"fs-extra": "^10.0.0",
5860
"glob": "^7.2.0",
5961
"postcss": "^8.4.5"
6062
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.a {
2+
color: green;
3+
}
4+
5+
.b {
6+
margin-top: 123px;
7+
}
8+
9+
.c {
10+
background-color: orange;
11+
color: #fff;
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.a {
2+
color: red;
3+
}
4+
5+
.b {
6+
margin-top: 123px;
7+
}
8+
9+
.c {
10+
background-color: orange;
11+
color: red;
12+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.a {
2+
color: green;
3+
}
4+
5+
.b {
6+
margin-top: 123px;
7+
}
8+
9+
.c {
10+
background-color: orange;
11+
color: #fff;
12+
}
13+
14+
.camelCase {
15+
border: 1px solid red;
16+
}
17+
18+
.kebab-case {
19+
border: 1px solid blue;
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.A {
2+
color: green;
3+
}
4+
5+
.B {
6+
margin-top: 123px;
7+
}
8+
9+
.C {
10+
background-color: orange;
11+
color: #fff;
12+
}
13+
14+
.CAMELCASE {
15+
border: 1px solid red;
16+
}
17+
18+
.KEBAB-CASE {
19+
border: 1px solid blue;
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Transform } from '../../src';
2+
3+
export const transform: Transform = (fileInfo, api) => {
4+
const root = api.parse(fileInfo.source);
5+
6+
root.walk(node => {
7+
if (node.type === 'rule') {
8+
node.selector = node.selector.toUpperCase();
9+
}
10+
});
11+
12+
return root.toString();
13+
};

test/cli.test.ts

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,49 @@
1+
import { execSync } from 'child_process';
2+
import glob from 'glob';
3+
import path from 'path';
4+
import fs from 'fs-extra';
5+
6+
const bin = './dist/cli.js';
7+
const cache = path.join(__dirname, '.cache');
8+
9+
const run = (recipe: string) => {
10+
// Setup the test files
11+
const originalInput = `./recipes/${recipe}/input`;
12+
const inputDest = path.resolve(cache, recipe);
13+
fs.copySync(originalInput, inputDest);
14+
15+
// Run the command
16+
const transform = `./recipes/${recipe}/transform.ts`;
17+
const inputGlob = `${inputDest}/**/*.css`;
18+
const command = `${bin} -t ${transform} '${inputGlob}'`;
19+
execSync(command);
20+
21+
// Compare results
22+
const expectedOutput = `./recipes/${recipe}/output`;
23+
const expectedOutputGlob = `${expectedOutput}/**/*.css`;
24+
const expectedFiles = glob.sync(expectedOutputGlob);
25+
26+
expectedFiles.forEach(expectedFile => {
27+
const uniquePath = path.relative(expectedOutput, expectedFile);
28+
const actualFilePath = path.join(inputDest, uniquePath);
29+
const expectedContent = fs.readFileSync(expectedFile).toString();
30+
const actualContent = fs.readFileSync(actualFilePath).toString();
31+
expect(actualContent).toEqual(expectedContent);
32+
});
33+
};
34+
35+
const recipes = fs.readdirSync(path.resolve(__dirname, '../recipes'));
36+
137
describe('cli', () => {
2-
it('should perform file transformations', () => {
3-
// cli();
38+
beforeAll(() => {
39+
fs.removeSync(cache);
40+
});
41+
42+
afterAll(() => {
43+
fs.removeSync(cache);
44+
});
45+
46+
it.each(recipes)('should perform %s transform correctly', recipe => {
47+
run(recipe);
448
});
549
});

test/perform.test.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)