Skip to content

Commit

Permalink
test: switch to snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
naiyerasif committed Mar 24, 2024
1 parent 98e95c4 commit 10ac0c1
Show file tree
Hide file tree
Showing 63 changed files with 63 additions and 71 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ To sync the latest updates, run `vale sync`.
## Development

- [Rules](./Prim/) are written in `yml` files using the syntax described by [Vale docs](https://vale.sh/docs/).
- Every rule has a [fixture](./fixtures/) against which you can run tests.
- Tests are run against the [log output](./test/expectations/) of Vale.
- Every rule has a [fixture](./test/fixtures/) against which you can run tests.
- Tests are run against the [log output snapshots](./test/snapshots/) of Vale.

### Prerequisites

Expand All @@ -53,8 +53,8 @@ Run `pnpm i` to download the test dependencies; you would need them to run the t
A typical workflow looks like this.

1. Add a new rule by creating a new `Prim/<RuleName>.yml` file, or modify an existing rule in the existing `Prim/<RuleName>.yml` file.
2. Add or update the fixtures in the `fixtures/<RuleName>/test.md` directory.
3. Add the expected logs in the `test/expectations/<RuleName>.log` file.
2. Add or update the fixtures in the `test/fixtures/<RuleName>/test.md` directory.
3. Generate or update a snapshot with `pnpm snapshot:update`
4. Run `pnpm test` to run the tests.

## License
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.0.3",
"description": "A Vale-compatible prose style for non-native English speaker",
"scripts": {
"snapshot:update": "vitest rules --update",
"test": "vitest rules",
"dist": "zip -r Prim.zip Prim -x \"*.DS_Store\""
},
Expand Down
1 change: 0 additions & 1 deletion test/expectations/Semicolon.log

This file was deleted.

1 change: 0 additions & 1 deletion test/expectations/WordRepitition.log

This file was deleted.

2 changes: 1 addition & 1 deletion fixtures/AMPM/.vale.ini → test/fixtures/AMPM/.vale.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
StylesPath = ../../
StylesPath = ../../../

MinAlertLevel = suggestion

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
StylesPath = ../../
StylesPath = ../../../

MinAlertLevel = suggestion

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
StylesPath = ../../
StylesPath = ../../../

MinAlertLevel = suggestion

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
StylesPath = ../../
StylesPath = ../../../

MinAlertLevel = suggestion

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
StylesPath = ../../
StylesPath = ../../../

MinAlertLevel = suggestion

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
StylesPath = ../../
StylesPath = ../../../

MinAlertLevel = suggestion

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
StylesPath = ../../
StylesPath = ../../../

MinAlertLevel = suggestion

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion fixtures/Latin/.vale.ini → test/fixtures/Latin/.vale.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
StylesPath = ../../
StylesPath = ../../../

MinAlertLevel = suggestion

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
StylesPath = ../../
StylesPath = ../../../

MinAlertLevel = suggestion

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
StylesPath = ../../
StylesPath = ../../../

MinAlertLevel = suggestion

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
StylesPath = ../../
StylesPath = ../../../

MinAlertLevel = suggestion

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
StylesPath = ../../
StylesPath = ../../../

MinAlertLevel = suggestion

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
StylesPath = ../../
StylesPath = ../../../

MinAlertLevel = suggestion

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
StylesPath = ../../
StylesPath = ../../../

MinAlertLevel = suggestion

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
StylesPath = ../../
StylesPath = ../../../

MinAlertLevel = suggestion

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
StylesPath = ../../
StylesPath = ../../../

MinAlertLevel = suggestion

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
StylesPath = ../../
StylesPath = ../../../

MinAlertLevel = suggestion

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion fixtures/Space/.vale.ini → test/fixtures/Space/.vale.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
StylesPath = ../../
StylesPath = ../../../

MinAlertLevel = suggestion

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
StylesPath = ../../
StylesPath = ../../../

MinAlertLevel = suggestion

Expand Down
File renamed without changes.
34 changes: 12 additions & 22 deletions test/rules.parameters.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
import fs from "fs";
import path from "path";

function* walkSync(dir) {
const files = fs.readdirSync(dir, { withFileTypes: true });
for (const file of files) {
if (file.isDirectory()) {
yield* walkSync(path.join(dir, file.name));
} else {
yield path.join(dir, file.name);
}
}
}
const testDirectory = "test";
const fixturesDirectory = "fixtures";

const dir = path.resolve(process.cwd(), "test", "expectations");
const extension = ".log";
const parameters = [];
const fixturesRoot = path.resolve(process.cwd(), testDirectory, fixturesDirectory);
const parameters = fs.readdirSync(fixturesRoot, {
withFileTypes: false,
recursive: false
}).map(fpath => path.basename(fpath));

for (const filePath of walkSync(dir)) {
if (path.extname(filePath).toLowerCase() === extension) {
const fileName = path.basename(filePath).replace(extension, "");
const fileContent = fs.readFileSync(filePath).toString().trim();
parameters.push([fileName, fileContent]);
}
}

export default parameters;
export {
parameters as default,
testDirectory,
fixturesDirectory
};
15 changes: 8 additions & 7 deletions test/rules.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from "path";
import { execa } from "execa";
import { expect, test } from "vitest";
import scenario from "./rules.parameters.js";
import { expect, it } from "vitest";
import scenario, { testDirectory, fixturesDirectory } from "./rules.parameters.js";

const command = "vale";
const args = [
Expand All @@ -11,17 +11,18 @@ const args = [
"--relative",
"test.md"
]
const fixturesDirectory = "fixtures";
const snapshotsDirectory = "snapshots";

test.each(scenario)(`Test: %s`, async (rule, expected) => {
it.each(scenario)(`Test: %s`, async (rule) => {
let result;
try {
const cwd = path.resolve(process.cwd(), fixturesDirectory, rule);
const output = await execa(command, args, { cwd: cwd });
const fixtureLocation = path.resolve(process.cwd(), testDirectory, fixturesDirectory, rule);
const output = await execa(command, args, { cwd: fixtureLocation });
result = output.stdout;
} catch (error) {
result = error.stdout;
}

expect.soft(result.trim()).toBe(expected);
const snapshotFile = path.resolve(process.cwd(), testDirectory, snapshotsDirectory, `${rule}.log`);
await expect(result).toMatchFileSnapshot(snapshotFile);
});
2 changes: 1 addition & 1 deletion test/expectations/AMPM.log → test/snapshots/AMPM.log
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ test.md:3:24:Prim.AMPM:Use 'am' or 'pm' else 'a.m.' or 'p.m.' (preceded by a spa
test.md:4:33:Prim.AMPM:Use 'am' or 'pm' else 'a.m.' or 'p.m.' (preceded by a space).
test.md:4:41:Prim.AMPM:Use 'am' or 'pm' else 'a.m.' or 'p.m.' (preceded by a space).
test.md:5:33:Prim.AMPM:Use 'am' or 'pm' else 'a.m.' or 'p.m.' (preceded by a space).
test.md:6:30:Prim.AMPM:Use 'am' or 'pm' else 'a.m.' or 'p.m.' (preceded by a space).
test.md:6:30:Prim.AMPM:Use 'am' or 'pm' else 'a.m.' or 'p.m.' (preceded by a space).
Original file line number Diff line number Diff line change
@@ -1 +1 @@
test.md:3:45:Prim.AbbreviationPeriods:Don't use periods with abbreviations such as 'H.T.T.P.S.'.
test.md:3:45:Prim.AbbreviationPeriods:Don't use periods with abbreviations such as 'H.T.T.P.S.'.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
test.md:3:49:Prim.Ambiguity:Avoid ambiguity. Don't use 'and/or'.
test.md:3:49:Prim.Ambiguity:Avoid ambiguity. Don't use 'and/or'.
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,4 @@ test.md:267:14:Prim.Contractions:Avoid contractions. Use 'you are' instead of 'y
test.md:270:1:Prim.Contractions:Avoid contractions. Use 'You had' or 'You would' instead of 'You'd'.
test.md:271:4:Prim.Contractions:Avoid contractions. Use 'you had' or 'you would' instead of 'you'd'.
test.md:274:7:Prim.Contractions:Avoid contractions. Use 'you have' instead of 'you've'.
test.md:277:1:Prim.Contractions:Avoid contractions. Use 'You will' instead of 'You'll'.
test.md:277:1:Prim.Contractions:Avoid contractions. Use 'You will' instead of 'You'll'.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
test.md:3:23:Prim.Ellipsis:In general, don't use an ellipsis.
test.md:4:22:Prim.Ellipsis:In general, don't use an ellipsis.
test.md:4:22:Prim.Ellipsis:In general, don't use an ellipsis.
2 changes: 1 addition & 1 deletion test/expectations/EmDash.log → test/snapshots/EmDash.log
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
test.md:3:16:Prim.EmDash:Add spaces around '—'.
test.md:3:45:Prim.EmDash:Add spaces around '—'.
test.md:3:45:Prim.EmDash:Add spaces around '—'.
2 changes: 1 addition & 1 deletion test/expectations/EnDash.log → test/snapshots/EnDash.log
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
test.md:3:47:Prim.EnDash:Remove the spaces around '–'.
test.md:4:31:Prim.EnDash:Remove the spaces around '–'.
test.md:5:42:Prim.EnDash:Remove the spaces around '–'.
test.md:5:42:Prim.EnDash:Remove the spaces around '–'.
2 changes: 1 addition & 1 deletion test/expectations/Latin.log → test/snapshots/Latin.log
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ test.md:21:33:Prim.Latin:Avoid latin. Use 'that is' instead of 'ie,'.
test.md:24:22:Prim.Latin:Avoid latin. Use 'against' instead of 'vs.'.
test.md:27:26:Prim.Latin:Avoid latin. Use 'against' instead of 'versus'.
test.md:30:33:Prim.Latin:Avoid latin. Use 'over' or 'through' instead of 'via'.
test.md:33:31:Prim.Latin:Avoid latin. Use 'namely' instead of 'viz.'.
test.md:33:31:Prim.Latin:Avoid latin. Use 'namely' instead of 'viz.'.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
test.md:3:23:Prim.OptionalPlurals:Don't use plurals in parentheses such as in 'extension(s)'.
test.md:4:5:Prim.OptionalPlurals:Don't use plurals in parentheses such as in 'checkbox(es)'.
test.md:4:5:Prim.OptionalPlurals:Don't use plurals in parentheses such as in 'checkbox(es)'.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
test.md:3:1:Prim.Ordinal:Don't add -ly to an ordinal number.
test.md:3:45:Prim.Ordinal:Don't add -ly to an ordinal number.
test.md:3:84:Prim.Ordinal:Don't add -ly to an ordinal number.
test.md:3:84:Prim.Ordinal:Don't add -ly to an ordinal number.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
test.md:3:41:Prim.OxfordComma:Use the Oxford comma in 'flour, sugar, eggs and butter.'.
test.md:5:20:Prim.OxfordComma:Use the Oxford comma in 'clothes, toiletries or books?'.
test.md:5:20:Prim.OxfordComma:Use the Oxford comma in 'clothes, toiletries or books?'.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
test.md:3:28:Prim.Percentages:Use a numeral to represent a percentage.
test.md:3:28:Prim.Percentages:Use a numeral to represent a percentage.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
test.md:4:36:Prim.PreferredAlternatives:Prefer using 'Zero-day' over '0-day'.
test.md:7:39:Prim.PreferredAlternatives:Prefer using 'Midnight' over '12 am'.
test.md:10:30:Prim.PreferredAlternatives:Prefer using 'Noon' over '12 pm'.
test.md:10:30:Prim.PreferredAlternatives:Prefer using 'Noon' over '12 pm'.
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ test.md:7:27:Prim.RequiredAlternatives:Use '3D' instead of '3-D'.
test.md:10:19:Prim.RequiredAlternatives:Use 'sync' instead of 'synch'.
test.md:13:17:Prim.RequiredAlternatives:Use 'URL' instead of 'url'.
test.md:14:5:Prim.RequiredAlternatives:Use 'URL of' instead of 'URL for'.
test.md:15:10:Prim.RequiredAlternatives:Use 'a URL' instead of 'an URL'.
test.md:15:10:Prim.RequiredAlternatives:Use 'a URL' instead of 'an URL'.
1 change: 1 addition & 0 deletions test/snapshots/Semicolon.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test.md:3:33:Prim.Semicolon:Try to simplify this sentence.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
test.md:3:1:Prim.SentenceLength:Try to keep sentences short (< 30 words).
test.md:3:1:Prim.SentenceLength:Try to keep sentences short (< 30 words).
Original file line number Diff line number Diff line change
@@ -1 +1 @@
test.md:4:31:Prim.SimplerAlternatives:Use 'about' instead of 'approximately'.
test.md:4:31:Prim.SimplerAlternatives:Use 'about' instead of 'approximately'.
2 changes: 1 addition & 1 deletion test/expectations/Space.log → test/snapshots/Space.log
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ test.md:4:35:Prim.Space:'door. It' should have one space between them.
test.md:5:18:Prim.Space:'melody.It' should have one space between them.
test.md:6:29:Prim.Space:'ruins? They' should have one space between them.
test.md:7:32:Prim.Space:', marveling' should have one space between them.
test.md:8:1:Prim.Space:'Whoa! This' should have one space between them.
test.md:8:1:Prim.Space:'Whoa! This' should have one space between them.
1 change: 1 addition & 0 deletions test/snapshots/WordRepitition.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test.md:3:14:Prim.WordRepitition:'zoom' is repeated.

0 comments on commit 10ac0c1

Please sign in to comment.