Skip to content

Commit

Permalink
Migrate from Jest to Vitest (#1340)
Browse files Browse the repository at this point in the history
* add vitest dev dependency

* Move tests to vitest

* Add missing dependency

* Remove all references to Jest

* Add VSCode extension recommendation and settings

* Check coverage under src dir only

* Consolidate some ESLint disable in the config

* Run VSCode tests with built-in parser

* Ignore some untestable code paths
  • Loading branch information
dangmai authored Apr 21, 2024
1 parent 8b7d70a commit b722031
Show file tree
Hide file tree
Showing 144 changed files with 3,318 additions and 8,365 deletions.
8 changes: 2 additions & 6 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"import/no-extraneous-dependencies": [
"error",
{
"devDependencies": ["tests*/**/*.ts"]
"devDependencies": ["tests*/**/*.ts", "vite.config.ts"]
}
],
"import/extensions": [2, "always"],
Expand All @@ -28,11 +28,7 @@
"globals": {
"runSpec": true
},
"env": {
"jest": true
},
"parserOptions": {
"project": "./tsconfig.json"
},
"ignorePatterns": ["jest.*.js"]
}
}
2 changes: 2 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"recommendations": [
"esbenp.prettier-vscode",
"alesforce.salesforcedx-vscode",
"vitest.explorer",
"dbaeumer.vscode-eslint",
"vscjava.vscode-java-pack",
"tintinweb.vscode-decompiler"
Expand Down
40 changes: 0 additions & 40 deletions .vscode/launch.json

This file was deleted.

7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@
"./packages/playground",
"./packages/prettier-plugin-apex"
],
"java.configuration.updateBuildConfiguration": "automatic"
"java.configuration.updateBuildConfiguration": "automatic",
"vitest.rootConfig": "packages/prettier-plugin-apex/vite.config.ts",
"vitest.nodeEnv": {
"AST_COMPARE": "1",
"APEX_PARSER": "built-in"
}
}
8 changes: 4 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ pnpm nx run apex-ast-serializer:build

Here's what you need to know about the Prettier Apex tests:

- The tests use [Jest snapshots](https://facebook.github.io/jest/docs/en/snapshot-testing.html).
- You can make changes and run `jest -u` to update the snapshots. Then run `git diff` to take a look at what changed. Always update the snapshots when opening a PR.
- You can run `AST_COMPARE=1 jest` for a more robust test run. That formats each file, re-parses it, and compares the new AST with the original one and makes sure they are semantically equivalent.
- Each test folder has a `jsfmt.spec.js` that runs the tests. Generally you can just put `runSpec(fileURLToPath(new URL(".", import.meta.url)), ["apex"]);` there. This will verify that the output using the Apex parser stays consistent. You can also pass options as the third argument, like this: `runSpec(fileURLToPath(new URL(".", import.meta.url)), ["apex"], { apexInsertFinalNewLine: false });`
- The tests use [Vitest snapshots](https://vitest.dev/guide/snapshot).
- You can make changes and run `pnpm nx run prettier-plugin-apex:test:parser:built-in -u` to update the snapshots. Then run `git diff` to take a look at what changed. Always update the snapshots when opening a PR.
- You can run `AST_COMPARE=1 pnpm nx run prettier-plugin-apex:test:parser:built-in` for a more robust test run. That formats each file, re-parses it, and compares the new AST with the original one and makes sure they are semantically equivalent.
- Each test folder has a `jsfmt.spec.ts` that runs the tests. Generally you can just put `runSpec(fileURLToPath(new URL(".", import.meta.url)), ["apex"]);` there. This will verify that the output using the Apex parser stays consistent. You can also pass options as the third argument, like this: `runSpec(fileURLToPath(new URL(".", import.meta.url)), ["apex"], { apexInsertFinalNewLine: false });`
- If you would like to debug prettier locally, you can either debug it in node or the browser. The easiest way to debug it in the browser is to run the interactive `docs` REPL locally. The easiest way to debug it in node, is to create a local test file and run it in an editor like VS Code.

Run `pnpm run -r prettier` to automatically format files.
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@
"prettier": "3.2.5",
"prettier-plugin-java": "2.6.0",
"prettier-plugin-organize-imports": "3.2.4",
"rimraf": "5.0.5",
"vite": "5.2.10",
"vite-plugin-radar": "0.9.4"
"rimraf": "5.0.5"
},
"keywords": [],
"author": "Dang Mai <[email protected]>",
Expand Down
4 changes: 3 additions & 1 deletion packages/playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
"devDependencies": {
"@types/react": "18.2.79",
"@types/react-dom": "18.2.25",
"@vitejs/plugin-react": "4.2.1"
"@vitejs/plugin-react": "4.2.1",
"vite": "5.2.10",
"vite-plugin-radar": "0.9.4"
},
"author": "Dang Mai <[email protected]>",
"license": "MIT"
Expand Down
3 changes: 0 additions & 3 deletions packages/playground/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { resolve } from "path";

// eslint-disable-next-line import/no-extraneous-dependencies -- this file is only used in dev mode
import react from "@vitejs/plugin-react";
// eslint-disable-next-line import/no-extraneous-dependencies -- we use the shared vite version from the root installation
import { defineConfig } from "vite";
// eslint-disable-next-line import/no-extraneous-dependencies -- this file is only used in dev mode
import { VitePluginRadar } from "vite-plugin-radar";

const analyticsId = process.env["GOOGLE_ANALYTICS_ID"] || "";
Expand Down
20 changes: 0 additions & 20 deletions packages/prettier-plugin-apex/jest.config.js

This file was deleted.

11 changes: 5 additions & 6 deletions packages/prettier-plugin-apex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"build:standalone": "vite build",
"clean": "rimraf node_modules dist",
"pretest": "pnpm run lint",
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest",
"test": "vitest run",
"test:parser:built-in": "cross-env APEX_PARSER=built-in pnpm test",
"test:parser:native": "cross-env APEX_PARSER=native pnpm test",
"start-server": "tsx bin/start-apex-server.ts -h 0.0.0.0 -c '*'",
Expand All @@ -45,16 +45,15 @@
"license": "MIT",
"devDependencies": {
"@tsconfig/node18": "18.2.4",
"@types/jest": "29.5.12",
"@types/node": "18.16.9",
"@types/wait-on": "5.3.4",
"jest": "29.7.0",
"jest-snapshot-serializer-raw": "1.2.0",
"@vitest/coverage-v8": "1.5.0",
"rimraf": "5.0.5",
"ts-jest": "29.1.2",
"tsx": "4.7.2",
"typescript": "5.4.5",
"undici": "6.13.0"
"undici": "6.13.0",
"vite": "5.2.10",
"vitest": "1.5.0"
},
"peerDependencies": {
"prettier": "^3.0.0"
Expand Down
4 changes: 3 additions & 1 deletion packages/prettier-plugin-apex/src/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,16 @@ export function printDanglingComment(
let fromPos = skipWhitespace(sourceCode, loc.startIndex - 1, {
backwards: true,
});
/* v8 ignore next 3 */
if (fromPos === false) {
return "";
}
fromPos += 1;
const leadingSpace = sourceCode.slice(fromPos, loc.startIndex);
const numberOfNewLines = isFirstComment
? 0
: (leadingSpace.match(/\n/g) || []).length;
: /* v8 ignore next 1 */
(leadingSpace.match(/\n/g) || []).length;

if (numberOfNewLines > 0) {
// If the leading space contains newlines, then add at most 2 new lines
Expand Down
1 change: 1 addition & 0 deletions packages/prettier-plugin-apex/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ function getLineIndexes(sourceCode: string) {
break;
}
const lastLineIndex = lineIndexes[lineIndex - 1];
/* v8 ignore next 3 */
if (lastLineIndex === undefined) {
return lineIndexes;
}
Expand Down
1 change: 1 addition & 0 deletions packages/prettier-plugin-apex/src/pragma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function insertPragma(text: string): string {
comments: parsedDocblock.comments.replace(/^(\s+?\r?\n)+/, ""), // remove leading newlines
}).replace(/(\r\n|\r)/g, "\n"); // normalise newlines (mitigate use of os.EOL by jest-docblock)
const strippedText = strip(text);
/* v8 ignore next 1 */
const separatingNewlines = strippedText.startsWith("\n") ? "\n" : "\n\n";
return newDocblock + separatingNewlines + strippedText;
}
12 changes: 10 additions & 2 deletions packages/prettier-plugin-apex/src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ function handleLiteralExpression(
node.loc.endIndex,
);
let lastCharacter = literal[literal.length - 1];
/* v8 ignore next 3 */
if (lastCharacter === undefined) {
lastCharacter = "";
}
Expand Down Expand Up @@ -917,10 +918,12 @@ function handleStatement(
case "apex.jorje.data.ast.Stmnt$DmlUndeleteStmnt":
doc = "undelete";
break;
/* v8 ignore start */
default:
throw new Error(
`Statement ${childClass} is not supported. Please file a bug report.`,
);
/* v8 ignore stop */
}
const node = path.getNode();
const parts: Doc[] = [];
Expand Down Expand Up @@ -2390,6 +2393,7 @@ function handleWhereQueryLiteral(
if (doc) {
return doc;
}
/* v8 ignore next 1 */
return "";
}

Expand Down Expand Up @@ -2658,12 +2662,14 @@ function handleUsingType(path: AstPath, print: printFn): Doc {
}

function handleModifier(childClass: string): Doc {
const modifierValue = MODIFIER[childClass as jorje.Modifier["@class"]] || "";
if (!modifierValue) {
/* v8 ignore start */
if (!(childClass in MODIFIER)) {
throw new Error(
`Modifier ${childClass} is not supported. Please file a bug report.`,
);
}
/* v8 ignore stop */
const modifierValue = MODIFIER[childClass as jorje.Modifier["@class"]];
return [modifierValue, " "];
}

Expand Down Expand Up @@ -3202,9 +3208,11 @@ function genericPrint(
);
}
}
/* v8 ignore start */
throw new Error(
`No handler found for ${apexClass}. Please file a bug report.`,
);
/* v8 ignore stop */
}

let options: prettier.ParserOptions;
Expand Down
4 changes: 4 additions & 0 deletions packages/prettier-plugin-apex/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,11 @@ const PRECEDENCE: { [key: string]: number } = {};

export function getPrecedence(op: string): number {
const precedence = PRECEDENCE[op];
/* v8 ignore start */
if (precedence === undefined) {
throw new Error(`Failed to get precedence for operator ${op}`);
}
/* v8 ignore start */
return precedence;
}

Expand All @@ -255,12 +257,14 @@ export async function getSerializerBinDirectory(): Promise<string> {
url.fileURLToPath(new URL(".", import.meta.url)),
"../vendor/apex-ast-serializer/bin",
);
/* v8 ignore start */
if (!(await doesFileExist(serializerBin))) {
serializerBin = nodePath.join(
url.fileURLToPath(new URL(".", import.meta.url)),
"../../vendor/apex-ast-serializer/bin",
);
}
/* v8 ignore stop */
return serializerBin;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@IsTest
class TestClass {
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@IsTest(SeeAllData=false)
class AnnotatedFalseParamClass {
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@IsTest(SeeAllData=true)
class AnnotedTrueParamClass {
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class topLevelClass {
@AuraEnabled
public String someProperty { get; set; }

@AuraEnabled(cacheable=true continuation=true)
public static Account getAccount(Id accountId) {
}

@SuppressWarnings('PMD.ApexCRUDViolation')
void someMethod() {
}

@SuppressWarnings('PMD.ApexCRUDViolation, PMD.AnotherViolation')
void anotherMethod() {
}

@InvocableMethod(label='Label' description='Description' category='Category')
List<String> getAccountNames(List<ID> ids) {
}

@InvocableMethod(callout=true)
List<String> getAccountNamesWithCallout(List<ID> ids) {
}

@InvocableMethod
public static List<Results> execute(List<Requests> requestList) {
}

@InvocableVariable(
label='Records for Input'
description='yourDescription'
required=true
)
public List<SObject> inputCollection;
}
Loading

0 comments on commit b722031

Please sign in to comment.