Skip to content

Commit c0ae91c

Browse files
authored
♻️ Refactor tests (#27)
Moves fixtures directory to the root, uses file snapshot in unit test, and creates test helper script for pipeline tests. Updates CONTRIBUTING. Tests will now all run instead of failing fast.
1 parent 1875279 commit c0ae91c

18 files changed

+68
-66
lines changed

.github/workflows/pipeline.yaml

+11-40
Original file line numberDiff line numberDiff line change
@@ -77,58 +77,29 @@ jobs:
7777
- name: Build
7878
run: npm run build
7979

80-
# Run tests
81-
#
82-
# - Run from $RUNNER_TEMP for auto-cleanup.
83-
# - `./dist/main.js` is executing local `css-typed` as if installed (same as `bin`).
84-
# But it is `$GITHUB_WORKSPACE/dist/main.js` b/c we `cd $RUNNER_TEMP`.
85-
# - Use `diff` to compare the files.
86-
# Use `-I '//.*'` to ignore the first line (comment) which has generated path and timestamp.
87-
8880
- name: "Test 1: Default case"
8981
run: |
90-
cp src/fixtures/casing/casing.css $RUNNER_TEMP/casing.css
91-
cp src/fixtures/casing/dashesOnly.d.css.ts $RUNNER_TEMP/expected.d.css.ts
92-
93-
cd $RUNNER_TEMP
94-
$GITHUB_WORKSPACE/dist/main.js '*.css'
95-
diff --strip-trailing-cr -uI '//.*' expected.d.css.ts casing.d.css.ts
82+
scripts/test.sh foo
9683
97-
- name: "Test 2: localsConvention, second position"
84+
- name: "Test 2: localsConvention, first position"
85+
if: success() || failure()
9886
run: |
99-
cp src/fixtures/casing/casing.css $RUNNER_TEMP/casing.css
100-
cp src/fixtures/casing/camelCaseOnly.d.css.ts $RUNNER_TEMP/expected.d.css.ts
101-
102-
cd $RUNNER_TEMP
103-
$GITHUB_WORKSPACE/dist/main.js '*.css' --localsConvention camelCaseOnly
104-
diff --strip-trailing-cr -uI '//.*' expected.d.css.ts casing.d.css.ts
87+
scripts/test.sh casing/casing "--localsConvention camelCaseOnly" casing/camelCaseOnly
10588
106-
- name: "Test 3: localsConvention, first position"
89+
- name: "Test 3: localsConvention, second position"
90+
if: success() || failure()
10791
run: |
108-
cp src/fixtures/casing/casing.css $RUNNER_TEMP/casing.css
109-
cp src/fixtures/casing/camelCaseOnly.d.css.ts $RUNNER_TEMP/expected.d.css.ts
110-
111-
cd $RUNNER_TEMP
112-
$GITHUB_WORKSPACE/dist/main.js --localsConvention camelCaseOnly '*.css'
113-
diff --strip-trailing-cr -uI '//.*' expected.d.css.ts casing.d.css.ts
92+
scripts/test.sh casing/casing "" casing/camelCaseOnly "--localsConvention camelCaseOnly"
11493
11594
- name: "Test 4: relative outdir"
95+
if: success() || failure()
11696
run: |
117-
cp src/fixtures/casing/casing.css $RUNNER_TEMP/casing.css
118-
cp src/fixtures/casing/dashesOnly.d.css.ts $RUNNER_TEMP/expected.d.css.ts
119-
120-
cd $RUNNER_TEMP
121-
$GITHUB_WORKSPACE/dist/main.js '*.css' --outdir generated
122-
diff --strip-trailing-cr -uI '//.*' expected.d.css.ts generated/casing.d.css.ts
97+
scripts/test.sh foo "--outdir generated" "" "" generated/
12398
12499
- name: "Test 5: absolute outdir"
100+
if: success() || failure()
125101
run: |
126-
cp src/fixtures/casing/casing.css $RUNNER_TEMP/casing.css
127-
cp src/fixtures/casing/dashesOnly.d.css.ts $RUNNER_TEMP/expected.d.css.ts
128-
129-
cd $RUNNER_TEMP
130-
$GITHUB_WORKSPACE/dist/main.js '*.css' -o $GITHUB_WORKSPACE/generated
131-
diff --strip-trailing-cr -uI '//.*' expected.d.css.ts $GITHUB_WORKSPACE/generated/casing.d.css.ts
102+
scripts/test.sh foo "-o $GITHUB_WORKSPACE/generated" "" "" "$GITHUB_WORKSPACE"/generated/
132103
133104
Publish:
134105
if: ${{ github.ref == 'refs/heads/main' }}

CONTRIBUTING.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Contributing
22

3-
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in BCP 14 [[RFC2119]] [[RFC8174]] when, and only when, they appear in all capitals, as shown here.
3+
The keywords "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in BCP 14 [[RFC2119]] [[RFC8174]] when, and only when, they appear in all capitals, as shown here.
44

55
## Getting started
66

@@ -20,9 +20,12 @@ The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "S
2020

2121
The [src](./src) directory contains the main and test sources.
2222

23-
- [main.js](./src/main.js) represents the entry point (the CLI tool).
24-
- [generate-declaration.js](./src/generate-declaration.js) represents the unit-tested JS logic.
25-
- [fixtures](./src/fixtures) directory contains files for data-file-driven unit tests.
23+
- [main.ts](./src/main.ts) represents the entry point (the CLI tool).
24+
- [logic.ts](./src/logic.ts) represents the unit-tested logic.
25+
26+
The [fixtures](fixtures) directory contains files for data-file-driven unit tests.
27+
28+
The [scripts](./scripts) directory contains the esbuild build script and pipeline test helper script.
2629

2730
## Expectations
2831

@@ -31,7 +34,7 @@ All contributions MUST adhere to the following expectations.
3134
1. Every change MUST have unit tests.
3235
2. Every change MUST have a GitHub issue linked.
3336
3. Any configuration option change SHOULD be discussed in a GitHub issue first.
34-
4. The PR build (see [pipeline.yaml](./.github/workflows/pipeline.yaml)) MUST succeed.
37+
4. The PR build and test (see [pipeline.yaml](./.github/workflows/pipeline.yaml)) MUST succeed.
3538
5. I will squash-merge the changeset into `main` upon approval.
3639

3740
[RFC2119]: https://www.rfc-editor.org/rfc/rfc2119

eslint.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ export default [
44
...connorjsConfig,
55
{
66
// Ignore declaration files used for tests. These represent generated files.
7-
ignores: [`src/fixtures/**/*.d.css.ts`],
7+
ignores: [`fixtures/**/*.d.css.ts`],
88
},
99
];

src/fixtures/casing/camelCase.d.css.ts renamed to fixtures/casing/camelCase.d.css.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Generated from `src/fixtures/casing/casing.css` by css-typed at $TIME
1+
// Generated from `fixtures/casing/casing.css` by css-typed at $TIME
22

33
const lowercase: string;
44
const UPPERCASE: string;

src/fixtures/casing/camelCaseOnly.d.css.ts renamed to fixtures/casing/camelCaseOnly.d.css.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Generated from `src/fixtures/casing/casing.css` by css-typed at $TIME
1+
// Generated from `fixtures/casing/casing.css` by css-typed at $TIME
22

33
export const lowercase: string;
44
export const uppercase: string;
File renamed without changes.

src/fixtures/casing/dashes.d.css.ts renamed to fixtures/casing/dashes.d.css.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Generated from `src/fixtures/casing/casing.css` by css-typed at $TIME
1+
// Generated from `fixtures/casing/casing.css` by css-typed at $TIME
22

33
const lowercase: string;
44
const UPPERCASE: string;

src/fixtures/casing/dashesOnly.d.css.ts renamed to fixtures/casing/dashesOnly.d.css.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Generated from `src/fixtures/casing/casing.css` by css-typed at $TIME
1+
// Generated from `fixtures/casing/casing.css` by css-typed at $TIME
22

33
export const lowercase: string;
44
export const UPPERCASE: string;

src/fixtures/casing/none.d.css.ts renamed to fixtures/casing/none.d.css.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Generated from `src/fixtures/casing/casing.css` by css-typed at $TIME
1+
// Generated from `fixtures/casing/casing.css` by css-typed at $TIME
22

33
const lowercase: string;
44
const UPPERCASE: string;
File renamed without changes.

src/fixtures/foo.d.css.ts renamed to fixtures/foo.d.css.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Generated from `src/fixtures/foo.css` by css-typed at $TIME
1+
// Generated from `fixtures/foo.css` by css-typed at $TIME
22

33
export const foo: string;
44
export const bar: string;
File renamed without changes.

src/fixtures/foo.module.d.css.ts renamed to fixtures/foo.module.d.css.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Generated from `src/fixtures/foo.module.css` by css-typed at $TIME
1+
// Generated from `fixtures/foo.module.css` by css-typed at $TIME
22

33
export const foo: string;
44
export const bar: string;

package-lock.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626
"engines": {
2727
"node": ">=18"
2828
},
29+
"engineStrict": true,
2930
"bin": {
3031
"css-typed": "dist/main.js"
3132
},
3233
"files": [
3334
"dist"
3435
],
36+
"main": "dist/main.js",
3537
"scripts": {
3638
"build": "node scripts/build.js",
3739
"ci-build": "npm-run-all -l -p eslint prettier test tsc -s build",
@@ -43,7 +45,6 @@
4345
"test": "vitest run",
4446
"tsc": "tsc"
4547
},
46-
"engineStrict": true,
4748
"dependencies": {
4849
"@commander-js/extra-typings": "^12.1.0",
4950
"commander": "^12.1.0",
@@ -57,14 +58,13 @@
5758
"@types/lodash.camelcase": "^4.3.9",
5859
"@types/node": "^20.14.14",
5960
"esbuild": "~0.23.0",
60-
"eslint-config-connorjs": "^1.0.0",
61+
"eslint-config-connorjs": "^1.1.0",
6162
"husky": "^9.1.4",
6263
"is-ci": "^3.0.1",
6364
"lint-staged": "^15.2.8",
6465
"npm-run-all": "^4.1.5",
6566
"prettier": "^3.3.3",
6667
"typescript": "^5.5.4",
6768
"vitest": "^2.0.5"
68-
},
69-
"main": "dist/main.js"
69+
}
7070
}

scripts/test.sh

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env bash
2+
3+
# $1 is the input name, relative to `fixtures`. Required.
4+
input=$1
5+
6+
# $2 is the standard (before) options. Defaults to "".
7+
IFS=" " read -r -a beforeOpts <<< "${2:-}"
8+
9+
# $3 is the output name, relative to `fixtures`. Defaults to $1.
10+
output=${3:-$1}
11+
12+
# $4 is the after options. Use an array. Defaults to "".
13+
IFS=" " read -r -a afterOpts <<< "${4:-}"
14+
15+
# $5 is the path prefix for output. Defaults to "".
16+
prefix=${5:-}
17+
18+
# Run from $RUNNER_TEMP for auto-cleanup.
19+
cp fixtures/${input}.css $RUNNER_TEMP/test.css
20+
cp fixtures/${output}.d.css.ts $RUNNER_TEMP/expected.d.css.ts
21+
pushd $RUNNER_TEMP > /dev/null || exit
22+
23+
# `./dist/main.js` is executing local `css-typed` as if installed (same as `bin`).
24+
# But it is `$GITHUB_WORKSPACE/dist/main.js` b/c we `cd $RUNNER_TEMP`.
25+
echo "css-typed ${beforeOpts[*]} \"*.css\" ${afterOpts[*]}"
26+
# shellcheck disable=SC2068
27+
$GITHUB_WORKSPACE/dist/main.js ${beforeOpts[@]} "*.css" ${afterOpts[@]}
28+
29+
# Use `diff` to compare the files.
30+
# Use `-I '//.*'` to ignore the first line (comment) which has generated path and timestamp.
31+
diff --color=auto --strip-trailing-cr -uI "//.*" expected.d.css.ts ${prefix}test.d.css.ts

src/logic.test.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { readFileSync } from "node:fs";
21
import path from "node:path";
32
import * as process from "node:process";
43

@@ -31,10 +30,8 @@ describe(`css-typed`, () => {
3130
const inputPath = fixtureFile(inputFilename);
3231
const outputPath = fixtureFile(outputFilename);
3332

34-
const expected = readFileSync(outputPath, { encoding: `utf8` });
35-
3633
const generated = await generateDeclaration(inputPath, `$TIME`, options);
37-
expect(generated).toStrictEqual(expected);
34+
await expect(generated).toMatchFileSnapshot(outputPath);
3835
});
3936
});
4037

@@ -57,5 +54,5 @@ describe(`css-typed`, () => {
5754
});
5855

5956
function fixtureFile(filename: string) {
60-
return path.join(import.meta.dirname, `fixtures`, filename);
57+
return path.join(import.meta.dirname, `..`, `fixtures`, filename);
6158
}

0 commit comments

Comments
 (0)