Skip to content

Commit

Permalink
Add tests for the ESLint caching. (vercel#2859)
Browse files Browse the repository at this point in the history
I pulled the trigger too fast on vercel#2850, these are the tests that should have been written for it.
  • Loading branch information
nathanhammond authored Nov 30, 2022
1 parent 6006545 commit fb5229f
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 6 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dist/

/examples/

packages/eslint-plugin-turbo/__tests__/fixtures
packages/create-turbo/templates
packages/turbo-tracing-next-plugin/test/with-mongodb-mongoose
crates/*/tests/**
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"format:prettier": "prettier -w .",
"format:rs": "cargo fmt --all",
"format:toml": "taplo format",
"lint": "eslint . --ext js,jsx,ts,tsx -c ./.eslintrc.js",
"lint": "eslint . --ext js,jsx,ts,tsx",
"turbo": "cd cli && make turbo && cd .. && node turbow.js",
"docs": "pnpm -- turbo run dev --filter=docs --no-cache",
"run-example": "./scripts/run-example.sh"
Expand All @@ -33,10 +33,11 @@
"typescript": "^4.8.4"
},
"lint-staged": {
"*.@(js|ts)": [
"eslint --quiet --fix"
"*.{js,jsx,ts,tsx}": [
"pnpm run lint --quiet --fix --",
"prettier --write"
],
"*.{ts,tsx,md,mdx,js,jsx,mjs,yml,yaml,css}": [
"*.{md,mdx,mjs,yml,yaml,css}": [
"prettier --write"
],
"*.go": [
Expand Down
111 changes: 111 additions & 0 deletions packages/eslint-plugin-turbo/__tests__/cwd.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import fs from "fs";
import path from "path";
import JSON5 from "json5";
import { execSync } from "child_process";

describe("eslint settings check", () => {
beforeAll(() => {
const cwd = path.join(__dirname, "fixtures", "workspace");
execSync(`npm install`, { cwd });
});

afterAll(() => {
const nodeModulesDir = path.join(
__dirname,
"fixtures",
"workspace",
"node_modules"
);
fs.rmSync(nodeModulesDir, { force: true, recursive: true });
});

it("does the right thing for peers", () => {
const cwd = path.join(__dirname, "fixtures", "workspace");
const configString = execSync(`eslint --print-config peer.js`, {
cwd,
encoding: "utf8",
});
const configJson = JSON.parse(configString);

expect(configJson.settings).toEqual({
turbo: { envVars: ["CI", "UNORDERED"] },
});
});

it("does the right thing for child dirs", () => {
const cwd = path.join(__dirname, "fixtures", "workspace", "child");
const configString = execSync(`eslint --print-config child.js`, {
cwd,
encoding: "utf8",
});
const configJson = JSON.parse(configString);

expect(configJson.settings).toEqual({
turbo: { envVars: ["CI", "UNORDERED"] },
});
});
});

describe("eslint cache is busted", () => {
let turboJsonPath: string;
let originalString: string;

beforeAll(() => {
const cwd = path.join(__dirname, "fixtures", "workspace");
execSync(`npm install`, { cwd });

turboJsonPath = path.join(__dirname, "fixtures", "workspace", "turbo.json");
originalString = fs.readFileSync(turboJsonPath, { encoding: "utf8" });
});

afterEach(() => {
fs.writeFileSync(turboJsonPath, originalString);
});

afterAll(() => {
fs.writeFileSync(turboJsonPath, originalString);

const nodeModulesDir = path.join(
__dirname,
"fixtures",
"workspace",
"node_modules"
);
fs.rmSync(nodeModulesDir, { force: true, recursive: true });
});

it("catches a lint error after changing config", () => {
expect.assertions(2);

// ensure that we populate the cache with a failure.
const cwd = path.join(__dirname, "fixtures", "workspace", "child");
try {
execSync(`eslint --format=json child.js`, { cwd, encoding: "utf8" });
} catch (error: any) {
const outputJson = JSON.parse(error.stdout);
expect(outputJson).toMatchObject([
{
messages: [
{
message:
"$NONEXISTENT is not listed as a dependency in turbo.json",
},
],
},
]);
}

// change the configuration
const turboJson = JSON5.parse(originalString);
turboJson.globalEnv = ["CI", "NONEXISTENT"];
fs.writeFileSync(turboJsonPath, JSON.stringify(turboJson, null, 2));

// test that we invalidated the eslint cache
const output = execSync(`eslint --format=json child.js`, {
cwd,
encoding: "utf8",
});
const outputJson = JSON.parse(output);
expect(outputJson).toMatchObject([{ errorCount: 0 }]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
root: true,
extends: ["plugin:turbo/recommended"],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
process.env.NONEXISTENT
process.env.CI
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"eslint-plugin-turbo": "../../.."
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
process.env.CI
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"$schema": "https://turbo.build/schema.json",
"globalEnv": ["UNORDERED", "CI"],
"pipeline": {
"build": {
// A workspace's `build` task depends on that workspace's
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import findTurboConfig from "../../lib/utils/findTurboConfig";

describe("findTurboConfig", () => {
it("it parses valid turbo.json file", () => {
const cwd = path.resolve(__dirname, "./fixtures/workspace");
const cwd = path.join(__dirname, "..", "fixtures", "workspace");
expect(findTurboConfig({ cwd })).toEqual({
$schema: "https://turbo.build/schema.json",
globalEnv: ["UNORDERED", "CI"],
pipeline: {
build: {
dependsOn: ["^build"],
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin-turbo/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = {
transform: {
"^.+\\.tsx?$": "ts-jest",
},
testPathIgnorePatterns: [".+/fixtures/.+"],
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
modulePathIgnorePatterns: ["<rootDir>/node_modules", "<rootDir>/dist"],
preset: "ts-jest",
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-turbo/lib/configs/recommended.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const envVars = getEnvVarDependencies({
});
const settings = {
turbo: {
envVars: envVars ? [...envVars] : [],
envVars: envVars ? [...envVars].sort() : [],
},
};

Expand Down
3 changes: 3 additions & 0 deletions turbo.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"$schema": "./docs/public/schema.json",
"pipeline": {
"eslint-plugin-turbo#test": {
"dependsOn": ["build"]
},
"test": {
"outputs": ["coverage/**/*"],
"dependsOn": ["^build"]
Expand Down

0 comments on commit fb5229f

Please sign in to comment.