From 183027ee066ad9244c0df563e37d08a0b6839b0e Mon Sep 17 00:00:00 2001 From: Mark Dalgleish Date: Fri, 3 May 2024 21:08:38 +1000 Subject: [PATCH] test: refactor --- src/validate-file.test.ts | 364 +++++++++++++++++------------------- src/validate-import.test.ts | 284 +++++++++++++--------------- 2 files changed, 310 insertions(+), 338 deletions(-) diff --git a/src/validate-file.test.ts b/src/validate-file.test.ts index 1e07177..e435abf 100644 --- a/src/validate-file.test.ts +++ b/src/validate-file.test.ts @@ -6,206 +6,192 @@ function fromCwd(relativePath: string) { return path.join(process.cwd(), relativePath) } -describe("validateFile", () => { - describe("server", () => { - describe("env: client", () => { - test("failed string validation", () => { - expect(() => - validateFile({ - absolutePath: fromCwd("/lib/server-only.ts"), - denyFiles: { - client: ["lib/server-only.ts"], - server: [], - }, - root: fromCwd("/"), - importer: fromCwd("/path/to/importer.ts"), - env: "client", - }) - ).toThrowErrorMatchingInlineSnapshot( - ` - [Error: File denied in client environment - - File: lib/server-only.ts - - Importer: path/to/importer.ts - - Matcher: "lib/server-only.ts"] - ` - ) - }) +test("validateFile / denyFiles.client / env:client / failed string validation", () => { + expect(() => + validateFile({ + absolutePath: fromCwd("/lib/server-only.ts"), + denyFiles: { + client: ["lib/server-only.ts"], + server: [], + }, + root: fromCwd("/"), + importer: fromCwd("/path/to/importer.ts"), + env: "client", + }) + ).toThrowErrorMatchingInlineSnapshot( + ` + [Error: File denied in client environment + - File: lib/server-only.ts + - Importer: path/to/importer.ts + - Matcher: "lib/server-only.ts"] + ` + ) +}) - test("failed regex validation", () => { - expect(() => - validateFile({ - absolutePath: fromCwd("/lib/server-only.ts"), - denyFiles: { - client: [/^lib\/server-only\.ts$/], - server: [], - }, - root: fromCwd("/"), - importer: fromCwd("/path/to/importer.ts"), - env: "client", - }) - ).toThrowErrorMatchingInlineSnapshot( - ` - [Error: File denied in client environment - - File: lib/server-only.ts - - Importer: path/to/importer.ts - - Matcher: /^lib\\/server-only\\.ts$/] - ` - ) - }) +test("validateFile / denyFiles.client / env:client / failed regex validation", () => { + expect(() => + validateFile({ + absolutePath: fromCwd("/lib/server-only.ts"), + denyFiles: { + client: [/^lib\/server-only\.ts$/], + server: [], + }, + root: fromCwd("/"), + importer: fromCwd("/path/to/importer.ts"), + env: "client", + }) + ).toThrowErrorMatchingInlineSnapshot( + ` + [Error: File denied in client environment + - File: lib/server-only.ts + - Importer: path/to/importer.ts + - Matcher: /^lib\\/server-only\\.ts$/] + ` + ) +}) - test("failed validation without importer", () => { - expect(() => - validateFile({ - absolutePath: fromCwd("/lib/server-only.ts"), - denyFiles: { - client: ["lib/server-only.ts"], - server: [], - }, - root: fromCwd("/"), - importer: undefined, - env: "client", - }) - ).toThrowErrorMatchingInlineSnapshot( - ` - [Error: File denied in client environment - - File: lib/server-only.ts - - Matcher: "lib/server-only.ts"] - ` - ) - }) +test("validateFile / denyFiles.client / env:client / failed validation without importer", () => { + expect(() => + validateFile({ + absolutePath: fromCwd("/lib/server-only.ts"), + denyFiles: { + client: ["lib/server-only.ts"], + server: [], + }, + root: fromCwd("/"), + importer: undefined, + env: "client", + }) + ).toThrowErrorMatchingInlineSnapshot( + ` + [Error: File denied in client environment + - File: lib/server-only.ts + - Matcher: "lib/server-only.ts"] + ` + ) +}) - test("passed validation", () => { - expect(() => - validateFile({ - absolutePath: fromCwd("/lib/some-other-file.ts"), - denyFiles: { - client: [/server-only/], - server: [], - }, - root: fromCwd("/"), - importer: fromCwd("/path/to/importer.ts"), - env: "client", - }) - ).not.toThrow() - }) +test("validateFile / denyFiles.client / env:client / passed validation", () => { + expect(() => + validateFile({ + absolutePath: fromCwd("/lib/some-other-file.ts"), + denyFiles: { + client: [/server-only/], + server: [], + }, + root: fromCwd("/"), + importer: fromCwd("/path/to/importer.ts"), + env: "client", }) + ).not.toThrow() +}) - describe("env: server", () => { - test("ignores server only modules", () => { - expect(() => - validateFile({ - absolutePath: fromCwd("/lib/server-only.ts"), - denyFiles: { - client: ["lib/server-only.ts"], - server: [], - }, - root: fromCwd("/"), - importer: fromCwd("/path/to/importer.ts"), - env: "server", - }) - ).not.toThrow() - }) +test("validateFile / denyFiles.client / env:server / ignores denyFiles.client", () => { + expect(() => + validateFile({ + absolutePath: fromCwd("/lib/server-only.ts"), + denyFiles: { + client: ["lib/server-only.ts"], + server: [], + }, + root: fromCwd("/"), + importer: fromCwd("/path/to/importer.ts"), + env: "server", }) - }) + ).not.toThrow() +}) - describe("client", () => { - describe("env: server", () => { - test("failed string validation", () => { - expect(() => - validateFile({ - absolutePath: fromCwd("/lib/client-only.js"), - denyFiles: { - client: [], - server: ["lib/client-only.js"], - }, - root: fromCwd("/"), - importer: fromCwd("/path/to/importer.ts"), - env: "server", - }) - ).toThrowErrorMatchingInlineSnapshot( - ` - [Error: File denied in server environment - - File: lib/client-only.js - - Importer: path/to/importer.ts - - Matcher: "lib/client-only.js"] - ` - ) - }) +test("validateFile / denyFiles.server / env:server / failed string validation", () => { + expect(() => + validateFile({ + absolutePath: fromCwd("/lib/client-only.js"), + denyFiles: { + client: [], + server: ["lib/client-only.js"], + }, + root: fromCwd("/"), + importer: fromCwd("/path/to/importer.ts"), + env: "server", + }) + ).toThrowErrorMatchingInlineSnapshot( + ` + [Error: File denied in server environment + - File: lib/client-only.js + - Importer: path/to/importer.ts + - Matcher: "lib/client-only.js"] + ` + ) +}) - test("failed regex validation", () => { - expect(() => - validateFile({ - absolutePath: fromCwd("/lib/client-only.ts"), - denyFiles: { - client: [], - server: [/^lib\/client-only\.ts$/], - }, - root: fromCwd("/"), - importer: fromCwd("/path/to/importer.ts"), - env: "server", - }) - ).toThrowErrorMatchingInlineSnapshot( - ` - [Error: File denied in server environment - - File: lib/client-only.ts - - Importer: path/to/importer.ts - - Matcher: /^lib\\/client-only\\.ts$/] - ` - ) - }) +test("validateFile / denyFiles.server / env:server / failed regex validation", () => { + expect(() => + validateFile({ + absolutePath: fromCwd("/lib/client-only.ts"), + denyFiles: { + client: [], + server: [/^lib\/client-only\.ts$/], + }, + root: fromCwd("/"), + importer: fromCwd("/path/to/importer.ts"), + env: "server", + }) + ).toThrowErrorMatchingInlineSnapshot( + ` + [Error: File denied in server environment + - File: lib/client-only.ts + - Importer: path/to/importer.ts + - Matcher: /^lib\\/client-only\\.ts$/] + ` + ) +}) - test("failed validation without importer", () => { - expect(() => - validateFile({ - absolutePath: fromCwd("/lib/client-only.ts"), - denyFiles: { - client: [], - server: ["lib/client-only.ts"], - }, - root: fromCwd("/"), - importer: undefined, - env: "server", - }) - ).toThrowErrorMatchingInlineSnapshot( - ` - [Error: File denied in server environment - - File: lib/client-only.ts - - Matcher: "lib/client-only.ts"] - ` - ) - }) +test("validateFile / denyFiles.server / env:server / failed validation without importer", () => { + expect(() => + validateFile({ + absolutePath: fromCwd("/lib/client-only.ts"), + denyFiles: { + client: [], + server: ["lib/client-only.ts"], + }, + root: fromCwd("/"), + importer: undefined, + env: "server", + }) + ).toThrowErrorMatchingInlineSnapshot( + ` + [Error: File denied in server environment + - File: lib/client-only.ts + - Matcher: "lib/client-only.ts"] + ` + ) +}) - test("passed validation", () => { - expect(() => - validateFile({ - absolutePath: fromCwd("/lib/some-other-file.ts"), - denyFiles: { - client: [], - server: ["lib/client-only.ts"], - }, - root: fromCwd("/"), - importer: fromCwd("/path/to/importer.ts"), - env: "server", - }) - ).not.toThrow() - }) +test("validateFile / denyFiles.server / env:server / passed validation", () => { + expect(() => + validateFile({ + absolutePath: fromCwd("/lib/some-other-file.ts"), + denyFiles: { + client: [], + server: ["lib/client-only.ts"], + }, + root: fromCwd("/"), + importer: fromCwd("/path/to/importer.ts"), + env: "server", }) + ).not.toThrow() +}) - describe("env: client", () => { - test("ignores client only modules", () => { - expect(() => - validateFile({ - absolutePath: fromCwd("/lib/client-only.ts"), - denyFiles: { - client: [], - server: ["/lib/client-only.ts"], - }, - root: fromCwd("/"), - importer: fromCwd("/path/to/importer.ts"), - env: "client", - }) - ).not.toThrow() - }) +test("validateFile / denyFiles.server / env:client / ignores denyFiles.server", () => { + expect(() => + validateFile({ + absolutePath: fromCwd("/lib/client-only.ts"), + denyFiles: { + client: [], + server: ["/lib/client-only.ts"], + }, + root: fromCwd("/"), + importer: fromCwd("/path/to/importer.ts"), + env: "client", }) - }) + ).not.toThrow() }) diff --git a/src/validate-import.test.ts b/src/validate-import.test.ts index fc041d7..24de028 100644 --- a/src/validate-import.test.ts +++ b/src/validate-import.test.ts @@ -6,164 +6,150 @@ function fromCwd(relativePath: string) { return path.join(process.cwd(), relativePath) } -describe("validateImport", () => { - describe("server", () => { - describe("env: client", () => { - test("failed string validation", () => { - expect(() => - validateImport({ - id: "server-only", - denyImports: { - client: ["server-only"], - server: [], - }, - root: fromCwd("/"), - importer: fromCwd("/path/to/importer.ts"), - env: "client", - }) - ).toThrowErrorMatchingInlineSnapshot( - ` - [Error: Import denied in client environment - - Import: "server-only" - - Importer: path/to/importer.ts - - Matcher: "server-only"] - ` - ) - }) +test("validateImport / denyImports.client / env:client / failed string validation", () => { + expect(() => + validateImport({ + id: "server-only", + denyImports: { + client: ["server-only"], + server: [], + }, + root: fromCwd("/"), + importer: fromCwd("/path/to/importer.ts"), + env: "client", + }) + ).toThrowErrorMatchingInlineSnapshot( + ` + [Error: Import denied in client environment + - Import: "server-only" + - Importer: path/to/importer.ts + - Matcher: "server-only"] + ` + ) +}) - test("failed regex validation", () => { - expect(() => - validateImport({ - id: "../foo.server.ts", - denyImports: { - client: [/\.server/], - server: [], - }, - root: fromCwd("/"), - importer: fromCwd("/path/to/importer.ts"), - env: "client", - }) - ).toThrowErrorMatchingInlineSnapshot( - ` - [Error: Import denied in client environment - - Import: "../foo.server.ts" - - Importer: path/to/importer.ts - - Matcher: /\\.server/] - ` - ) - }) +test("validateImport / denyImports.client / env:client / failed regex validation", () => { + expect(() => + validateImport({ + id: "../foo.server.ts", + denyImports: { + client: [/\.server/], + server: [], + }, + root: fromCwd("/"), + importer: fromCwd("/path/to/importer.ts"), + env: "client", + }) + ).toThrowErrorMatchingInlineSnapshot( + ` + [Error: Import denied in client environment + - Import: "../foo.server.ts" + - Importer: path/to/importer.ts + - Matcher: /\\.server/] + ` + ) +}) - test("passed validation", () => { - expect(() => - validateImport({ - id: "some-other-module", - denyImports: { - client: ["server-only"], - server: [], - }, - root: fromCwd("/"), - importer: fromCwd("/path/to/importer.ts"), - env: "client", - }) - ).not.toThrow() - }) +test("validateImport / denyImports.client / env:client / passed validation", () => { + expect(() => + validateImport({ + id: "some-other-module", + denyImports: { + client: ["server-only"], + server: [], + }, + root: fromCwd("/"), + importer: fromCwd("/path/to/importer.ts"), + env: "client", }) + ).not.toThrow() +}) - describe("env: server", () => { - test("ignores server only modules", () => { - expect(() => - validateImport({ - id: "server-only", - denyImports: { - client: ["server-only"], - server: [], - }, - root: fromCwd("/"), - importer: fromCwd("/path/to/importer.ts"), - env: "server", - }) - ).not.toThrow() - }) +test("validateImport / denyImports.client / env:server / ignores denyImports.client", () => { + expect(() => + validateImport({ + id: "server-only", + denyImports: { + client: ["server-only"], + server: [], + }, + root: fromCwd("/"), + importer: fromCwd("/path/to/importer.ts"), + env: "server", }) - }) + ).not.toThrow() +}) - describe("client", () => { - describe("env: server", () => { - test("failed string validation", () => { - expect(() => - validateImport({ - id: "client-only", - denyImports: { - client: [], - server: ["client-only"], - }, - root: fromCwd("/"), - importer: fromCwd("/path/to/importer.ts"), - env: "server", - }) - ).toThrowErrorMatchingInlineSnapshot( - ` - [Error: Import denied in server environment - - Import: "client-only" - - Importer: path/to/importer.ts - - Matcher: "client-only"] - ` - ) - }) +test("validateImport / denyImports.server / env:server / failed string validation", () => { + expect(() => + validateImport({ + id: "client-only", + denyImports: { + client: [], + server: ["client-only"], + }, + root: fromCwd("/"), + importer: fromCwd("/path/to/importer.ts"), + env: "server", + }) + ).toThrowErrorMatchingInlineSnapshot( + ` + [Error: Import denied in server environment + - Import: "client-only" + - Importer: path/to/importer.ts + - Matcher: "client-only"] + ` + ) +}) - test("failed regex validation", () => { - expect(() => - validateImport({ - id: "../foo.client.ts", - denyImports: { - client: [], - server: [/\.client/], - }, - root: fromCwd("/"), - importer: fromCwd("/path/to/importer.ts"), - env: "server", - }) - ).toThrowErrorMatchingInlineSnapshot( - ` - [Error: Import denied in server environment - - Import: "../foo.client.ts" - - Importer: path/to/importer.ts - - Matcher: /\\.client/] - ` - ) - }) +test("validateImport / denyImports.server / env:server / failed regex validation", () => { + expect(() => + validateImport({ + id: "../foo.client.ts", + denyImports: { + client: [], + server: [/\.client/], + }, + root: fromCwd("/"), + importer: fromCwd("/path/to/importer.ts"), + env: "server", + }) + ).toThrowErrorMatchingInlineSnapshot( + ` + [Error: Import denied in server environment + - Import: "../foo.client.ts" + - Importer: path/to/importer.ts + - Matcher: /\\.client/] + ` + ) +}) - test("passed validation", () => { - expect(() => - validateImport({ - id: "some-other-module", - denyImports: { - client: [], - server: ["client-only"], - }, - root: fromCwd("/"), - importer: fromCwd("/path/to/importer.ts"), - env: "server", - }) - ).not.toThrow() - }) +test("validateImport / denyImports.server / env:server / passed validation", () => { + expect(() => + validateImport({ + id: "some-other-module", + denyImports: { + client: [], + server: ["client-only"], + }, + root: fromCwd("/"), + importer: fromCwd("/path/to/importer.ts"), + env: "server", }) + ).not.toThrow() +}) - describe("env: client", () => { - test("ignores client only modules", () => { - expect(() => - validateImport({ - id: "client-only", - denyImports: { - client: [], - server: ["client-only"], - }, - root: fromCwd("/"), - importer: fromCwd("/path/to/importer.ts"), - env: "client", - }) - ).not.toThrow() - }) +test("validateImport / denyImports.server / env:client / ignores denyImports.server", () => { + expect(() => + validateImport({ + id: "client-only", + denyImports: { + client: [], + server: ["client-only"], + }, + root: fromCwd("/"), + importer: fromCwd("/path/to/importer.ts"), + env: "client", }) - }) + ).not.toThrow() })