-
-
Notifications
You must be signed in to change notification settings - Fork 7
Upgrade to Jiti 2 #149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Upgrade to Jiti 2 #149
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f5ee080
feat!: Upgrade to Jiti 2
renovate[bot] ae87dd3
test: imrpove smoke tests
Codex- 1547200
chore: remove reaching into jiti for types
Codex- 6f9fb98
fix: improve typings
Codex- 364312b
test: improve tests
Codex- 0056629
fix: missed npx commands, moved to pnpm exec
Codex- File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,11 +1,11 @@ | ||
{ | ||
"git": { | ||
"changelog": "npx auto-changelog --stdout --commit-limit false --unreleased --template https://github.com/raw/release-it/release-it/master/templates/changelog-compact.hbs" | ||
"changelog": "pnpm exec auto-changelog --stdout --commit-limit false --unreleased --template https://github.com/raw/release-it/release-it/master/templates/changelog-compact.hbs" | ||
}, | ||
"github": { | ||
"release": true | ||
}, | ||
"hooks": { | ||
"after:bump": "npx auto-changelog -p" | ||
"after:bump": "pnpm exec auto-changelog -p" | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,2 @@ | ||
export { TypeScriptLoader } from "./loader.js"; | ||
export { TypeScriptLoader, TypeScriptLoaderSync } from "./loader.js"; | ||
export type { TypeScriptCompileError } from "./typescript-compile-error.js"; |
This file contains hidden or 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,94 +1,156 @@ | ||
import fs from "node:fs"; | ||
import path from "node:path"; | ||
|
||
import { Loader } from "cosmiconfig"; | ||
import type { LoaderResult, LoaderSync } from "cosmiconfig"; | ||
import * as jiti from "jiti"; | ||
|
||
import { TypeScriptLoader } from "./loader"; | ||
import { TypeScriptLoader, TypeScriptLoaderSync } from "./loader"; | ||
import { TypeScriptCompileError } from "./typescript-compile-error"; | ||
|
||
// Handle jiti using `export default` | ||
jest.mock("jiti", () => { | ||
const actual = jest.requireActual("jiti"); | ||
return { | ||
__esModule: true, | ||
default: jest.fn(actual), | ||
createJiti: actual.createJiti, | ||
}; | ||
}); | ||
|
||
describe("TypeScriptLoader", () => { | ||
const fixturesPath = path.resolve(__dirname, "__fixtures__"); | ||
const jitiSpy = jest.spyOn(jiti, "default"); | ||
|
||
let loader: Loader; | ||
let jitiCreateJitiSpy: jest.SpyInstance<typeof jiti.createJiti>; | ||
|
||
function readFixtureContent(file: string): string { | ||
return fs.readFileSync(file).toString(); | ||
} | ||
|
||
beforeAll(() => { | ||
loader = TypeScriptLoader(); | ||
beforeEach(() => { | ||
jitiCreateJitiSpy = jest.spyOn(jiti, "createJiti"); | ||
}); | ||
|
||
it("should parse a valid TS file", () => { | ||
const filePath = path.resolve(fixturesPath, "valid.fixture.ts"); | ||
loader(filePath, readFixtureContent(filePath)); | ||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it("should fail on parsing an invalid TS file", () => { | ||
const filePath = path.resolve(fixturesPath, "invalid.fixture.ts"); | ||
expect((): unknown => | ||
loader(filePath, readFixtureContent(filePath)), | ||
).toThrow(); | ||
}); | ||
describe("asynchronous", () => { | ||
let loader: (filepath: string, content: string) => Promise<LoaderResult>; | ||
|
||
it("should use the same instance of jiti across multiple calls", () => { | ||
const filePath = path.resolve(fixturesPath, "valid.fixture.ts"); | ||
loader(filePath, readFixtureContent(filePath)); | ||
loader(filePath, readFixtureContent(filePath)); | ||
expect(jitiSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
beforeEach(() => { | ||
loader = TypeScriptLoader(); | ||
}); | ||
|
||
it("should parse a valid TS file", async () => { | ||
const filePath = path.resolve(fixturesPath, "valid.fixture.ts"); | ||
await loader(filePath, readFixtureContent(filePath)); | ||
}); | ||
|
||
it("should throw a TypeScriptCompileError on error", () => { | ||
try { | ||
it("should fail on parsing an invalid TS file", async () => { | ||
const filePath = path.resolve(fixturesPath, "invalid.fixture.ts"); | ||
loader(filePath, readFixtureContent(filePath)); | ||
fail( | ||
"Error should be thrown upon failing to transpile an invalid TS file.", | ||
); | ||
} catch (error: unknown) { | ||
expect(error).toBeInstanceOf(TypeScriptCompileError); | ||
} | ||
}); | ||
await expect( | ||
loader(filePath, readFixtureContent(filePath)), | ||
).rejects.toThrow(); | ||
}); | ||
|
||
describe("jiti", () => { | ||
const unknownError = "Test Error"; | ||
it("should use the same instance of jiti across multiple calls", async () => { | ||
const filePath = path.resolve(fixturesPath, "valid.fixture.ts"); | ||
await loader(filePath, readFixtureContent(filePath)); | ||
await loader(filePath, readFixtureContent(filePath)); | ||
expect(jitiCreateJitiSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
let stub: jest.SpyInstance; | ||
it("should throw a TypeScriptCompileError on error", async () => { | ||
const filePath = path.resolve(fixturesPath, "invalid.fixture.ts"); | ||
await expect( | ||
loader(filePath, readFixtureContent(filePath)), | ||
).rejects.toThrow(TypeScriptCompileError); | ||
}); | ||
|
||
describe("jiti", () => { | ||
const unknownError = "Test Error"; | ||
|
||
beforeEach(() => { | ||
jitiCreateJitiSpy.mockImplementation((() => ({ | ||
import: () => { | ||
// eslint-disable-next-line @typescript-eslint/only-throw-error | ||
throw unknownError; | ||
}, | ||
})) as never); | ||
|
||
loader = TypeScriptLoader(); | ||
}); | ||
|
||
it("rethrows an error if it is not an instance of Error", async () => { | ||
try { | ||
await loader("filePath", "invalidInput"); | ||
fail( | ||
"Error should be thrown upon failing to transpile an invalid TS file.", | ||
); | ||
} catch (error: unknown) { | ||
expect(error).not.toBeInstanceOf(TypeScriptCompileError); | ||
expect(error).toStrictEqual(unknownError); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
describe("synchronous", () => { | ||
let loader: LoaderSync; | ||
|
||
beforeEach(() => { | ||
stub = jest.spyOn(jiti, "default").mockImplementation((() => () => { | ||
// eslint-disable-next-line @typescript-eslint/only-throw-error | ||
throw unknownError; | ||
}) as never); | ||
// eslint-disable-next-line @typescript-eslint/no-deprecated | ||
loader = TypeScriptLoaderSync(); | ||
}); | ||
|
||
loader = TypeScriptLoader(); | ||
it("should parse a valid TS file", () => { | ||
const filePath = path.resolve(fixturesPath, "valid.fixture.ts"); | ||
loader(filePath, readFixtureContent(filePath)); | ||
}); | ||
|
||
it("should fail on parsing an invalid TS file", () => { | ||
const filePath = path.resolve(fixturesPath, "invalid.fixture.ts"); | ||
expect((): unknown => | ||
loader(filePath, readFixtureContent(filePath)), | ||
).toThrow(); | ||
}); | ||
|
||
afterEach(() => { | ||
stub.mockRestore(); | ||
it("should use the same instance of jiti across multiple calls", () => { | ||
const filePath = path.resolve(fixturesPath, "valid.fixture.ts"); | ||
loader(filePath, readFixtureContent(filePath)); | ||
loader(filePath, readFixtureContent(filePath)); | ||
expect(jitiCreateJitiSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("should throw a TypeScriptCompileError on error", () => { | ||
const filePath = path.resolve(fixturesPath, "invalid.fixture.ts"); | ||
expect((): unknown => | ||
loader(filePath, readFixtureContent(filePath)), | ||
).toThrow(TypeScriptCompileError); | ||
Codex- marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
it("rethrows an error if it is not an instance of Error", () => { | ||
try { | ||
loader("filePath", "readFixtureContent(filePath)"); | ||
fail( | ||
"Error should be thrown upon failing to transpile an invalid TS file.", | ||
); | ||
} catch (error: unknown) { | ||
expect(error).not.toBeInstanceOf(TypeScriptCompileError); | ||
expect(error).toStrictEqual(unknownError); | ||
} | ||
describe("jiti", () => { | ||
const unknownError = "Test Error"; | ||
|
||
beforeEach(() => { | ||
jitiCreateJitiSpy.mockImplementation((() => () => { | ||
// eslint-disable-next-line @typescript-eslint/only-throw-error | ||
throw unknownError; | ||
}) as never); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-deprecated | ||
loader = TypeScriptLoaderSync(); | ||
}); | ||
|
||
it("rethrows an error if it is not an instance of Error", () => { | ||
try { | ||
loader("filePath", "invalidInput"); | ||
fail( | ||
"Error should be thrown upon failing to transpile an invalid TS file.", | ||
); | ||
} catch (error: unknown) { | ||
expect(error).not.toBeInstanceOf(TypeScriptCompileError); | ||
expect(error).toStrictEqual(unknownError); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.