generated from florian-lefebvre/astro-integration-template
-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds unit tests and use pathe whenever possible (#30)
Co-authored-by: Florian Lefebvre <[email protected]>
- Loading branch information
1 parent
214dc1d
commit 08e73d9
Showing
21 changed files
with
1,230 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"astro-integration-kit": patch | ||
--- | ||
|
||
Updates imports from `node:path` to `pathe` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: Tests | ||
|
||
on: [pull_request] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Setup PNPM | ||
run: corepack enable && pnpm -v | ||
|
||
- name: Setup Node | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: 18.19.0 | ||
cache: pnpm | ||
|
||
- name: Install dependencies | ||
run: pnpm install --frozen-lockfile | ||
|
||
- name: Test | ||
run: pnpm test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
dist | ||
dist | ||
.TMP_* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import { mkdirSync, readFileSync, rmSync, writeFileSync } from "fs"; | ||
import { fileURLToPath } from "url"; | ||
import type { AstroIntegrationLogger } from "astro"; | ||
import { dirname } from "pathe"; | ||
import { afterAll, beforeAll, describe, expect, test, vi } from "vitest"; | ||
import { addDts } from "../../src/utils/add-dts.js"; | ||
import { createResolver } from "../../src/utils/create-resolver.js"; | ||
|
||
const tempFolderName = ".TMP_ADDDTS/"; | ||
const { resolve } = createResolver(import.meta.url); | ||
const { resolve: tempFolderResolver } = createResolver(resolve(tempFolderName)); | ||
const envDtsPath = resolve(`${tempFolderName}/env.d.ts`); | ||
|
||
const createTempFiles = () => { | ||
mkdirSync(resolve(tempFolderName)); | ||
|
||
writeFileSync(envDtsPath, `/// <reference types="astro/client" />`, { | ||
encoding: "utf-8", | ||
}); | ||
}; | ||
|
||
const deleteTempFiles = () => { | ||
rmSync(resolve(tempFolderName), { | ||
recursive: true, | ||
force: true, | ||
}); | ||
}; | ||
|
||
describe("addDts", () => { | ||
beforeAll(() => { | ||
createTempFiles(); | ||
}); | ||
|
||
afterAll(() => { | ||
deleteTempFiles(); | ||
}); | ||
|
||
test("Should run", () => { | ||
const dtsFileName = "TEST"; | ||
const dtsFileContent = 'declare module "my-integration" {}'; | ||
const root = new URL(tempFolderName, import.meta.url); | ||
const srcDir = new URL(tempFolderName, import.meta.url); | ||
const logger = { | ||
info: vi.fn(), | ||
} as unknown as AstroIntegrationLogger; | ||
|
||
expect(() => | ||
addDts({ | ||
name: dtsFileName, | ||
content: dtsFileContent, | ||
logger, | ||
root, | ||
srcDir, | ||
}), | ||
).not.toThrow(); | ||
}); | ||
|
||
test("Should update the env.d.ts (double quotes)", () => { | ||
const dtsFileName = "TEST"; | ||
const dtsFileContent = 'declare module "my-integration" {}'; | ||
const root = new URL(tempFolderName, import.meta.url); | ||
const srcDir = new URL(tempFolderName, import.meta.url); | ||
const logger = { | ||
info: vi.fn(), | ||
} as unknown as AstroIntegrationLogger; | ||
|
||
const expectedEnvDtsContent = `/// <reference types="astro/client" />\n/// <reference types=".astro/${dtsFileName}.d.ts" />`; | ||
|
||
addDts({ | ||
name: dtsFileName, | ||
content: dtsFileContent, | ||
logger, | ||
root, | ||
srcDir, | ||
}); | ||
|
||
const fileContents = readFileSync(envDtsPath, { | ||
encoding: "utf-8", | ||
}); | ||
|
||
expect(fileContents).toEqual(expectedEnvDtsContent); | ||
}); | ||
|
||
test("Should update the env.d.ts (single quotes)", () => { | ||
const dtsFileName = "TEST"; | ||
const dtsFileContent = 'declare module "my-integration" {}'; | ||
const root = new URL(tempFolderName, import.meta.url); | ||
const srcDir = new URL(tempFolderName, import.meta.url); | ||
const logger = { | ||
info: vi.fn(), | ||
} as unknown as AstroIntegrationLogger; | ||
|
||
const expectedEnvDtsContent = `/// <reference types='astro/client' />\n/// <reference types='.astro/${dtsFileName}.d.ts' />`; | ||
|
||
writeFileSync(envDtsPath, `/// <reference types='astro/client' />`, { | ||
encoding: "utf-8", | ||
}); | ||
|
||
addDts({ | ||
name: dtsFileName, | ||
content: dtsFileContent, | ||
logger, | ||
root, | ||
srcDir, | ||
}); | ||
|
||
const fileContents = readFileSync(envDtsPath, { | ||
encoding: "utf-8", | ||
}); | ||
|
||
expect(fileContents).toEqual(expectedEnvDtsContent); | ||
}); | ||
|
||
test("Should create the virtual file", () => { | ||
const dtsFileName = "TEST"; | ||
const dtsFileContent = 'declare module "my-integration" {}'; | ||
const root = new URL(tempFolderName, import.meta.url); | ||
const srcDir = new URL(tempFolderName, import.meta.url); | ||
const logger = { | ||
info: vi.fn(), | ||
} as unknown as AstroIntegrationLogger; | ||
|
||
addDts({ | ||
name: dtsFileName, | ||
content: dtsFileContent, | ||
logger, | ||
root, | ||
srcDir, | ||
}); | ||
|
||
const fileContents = readFileSync( | ||
tempFolderResolver(`.astro/${dtsFileName}.d.ts`), | ||
{ | ||
encoding: "utf-8", | ||
}, | ||
); | ||
|
||
expect(fileContents).toEqual(dtsFileContent); | ||
}); | ||
}); |
Oops, something went wrong.