Skip to content

Commit 08b119f

Browse files
committed
test: begin improving tests
1 parent bd47303 commit 08b119f

13 files changed

Lines changed: 1000 additions & 1582 deletions

File tree

.vscode/tasks.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"type": "npm",
6+
"script": "test:watch",
7+
"group": {
8+
"kind": "test",
9+
"isDefault": true
10+
},
11+
"problemMatcher": []
12+
}
13+
]
14+
}

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"start": "tsdx watch",
2020
"build": "tsup src/index.ts src/cli.ts --dts",
2121
"test": "tsdx test",
22+
"test:watch": "tsdx test --watch",
2223
"lint": "tsdx lint",
2324
"prepare": "yarn build",
2425
"clean": "rimraf dist",
@@ -35,6 +36,10 @@
3536
"singleQuote": true,
3637
"trailingComma": "es5"
3738
},
39+
"resolutions": {
40+
"**/tsdx/jest": "27.4.7",
41+
"**/tsdx/ts-jest": "27.1.3"
42+
},
3843
"devDependencies": {
3944
"@types/glob": "^7.2.0",
4045
"@types/node": "^17.0.13",

src/api.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import postcss, { Root } from 'postcss';
22

33
export interface TransformAPI {
4+
/**
5+
* Parse a raw CSS string into an abstract syntax tree and return a PostCSS `Root` node.
6+
*/
47
parse(source: string): Root;
58
}
69

src/files.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ export interface TransformFileInfo {
1313
source: string;
1414
}
1515

16+
/**
17+
* Construct file metadata given a file path.
18+
*/
1619
export const getFileInfo = (file: string): TransformFileInfo => {
1720
try {
1821
const source = fs.readFileSync(file, 'utf8').toString();
@@ -23,6 +26,9 @@ export const getFileInfo = (file: string): TransformFileInfo => {
2326
}
2427
};
2528

29+
/**
30+
* Write contents to the given file.
31+
*/
2632
export const writeFile = (file: string, contents: string): void => {
2733
try {
2834
fs.writeFileSync(file, contents);
@@ -31,4 +37,7 @@ export const writeFile = (file: string, contents: string): void => {
3137
}
3238
};
3339

40+
/**
41+
* Return an array of file paths that matches the given files string.
42+
*/
3443
export const getAllFilesToTransform = (files: string) => glob.sync(files);

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
export { traverse, Processors } from './traverse';
21
export { Transform } from './transform';

src/transform.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ export type Transform = (
2323
/**
2424
* Validate the general structure of the transform to catch simple errors.
2525
*/
26-
const validateTransform = (transform: unknown): Transform => {
27-
if (typeof transform === 'function') {
26+
export const validateTransform = (transform: unknown): Transform => {
27+
if (typeof transform === 'function' && transform.length <= 2) {
2828
return transform as Transform;
2929
} else {
3030
console.error(

src/traverse/index.ts

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

test/api.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Declaration, Rule } from 'postcss';
2+
import { api } from '../src/api';
3+
4+
describe('api', () => {
5+
describe('#parse', () => {
6+
it('should return the root node of an abstract syntax tree', () => {
7+
const root = api.parse(`.class { color: orange; }`);
8+
9+
expect(root.type).toEqual('root');
10+
expect(root.nodes.length).toEqual(1);
11+
12+
const rule = root.nodes[0] as Rule;
13+
expect(rule.type).toEqual('rule');
14+
expect(rule.nodes.length).toEqual(1);
15+
16+
const declaration = rule.nodes[0] as Declaration;
17+
expect(declaration.type).toEqual('decl');
18+
expect(declaration.prop).toEqual('color');
19+
expect(declaration.value).toEqual('orange');
20+
});
21+
});
22+
});

test/examples/color-to-red.ts

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

test/examples/uppercase-selectors.ts

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

0 commit comments

Comments
 (0)