-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #731 from pmcelhaney/unit-tests
unit tests
- Loading branch information
Showing
14 changed files
with
153 additions
and
78 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 @@ | ||
--- | ||
"counterfact": patch | ||
--- | ||
|
||
CodeGenerator#watch() now waits until Chokidar completes its initial scan before resolving |
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 |
---|---|---|
@@ -1,7 +1,6 @@ | ||
name: Debug Windows | ||
|
||
on: | ||
pull_request: | ||
workflow_dispatch: | ||
|
||
env: | ||
|
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 was deleted.
Oops, something went wrong.
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,28 @@ | ||
import type { EventEmitter } from "koa"; | ||
|
||
/** | ||
* Creates a promise that resolves when a specified event is fired on the given EventTarget. | ||
* @param {EventTarget | EventEmitter} target - The target to listen for the event on. | ||
* @param {string} eventName - The name of the event to listen for. | ||
* @returns {Promise<Event>} A promise that resolves with the event object when the event is fired. | ||
*/ | ||
export async function waitForEvent( | ||
target: EventEmitter | EventTarget, | ||
eventName: string, | ||
) { | ||
// eslint-disable-next-line promise/avoid-new | ||
return await new Promise((resolve) => { | ||
const handler = (event: unknown) => { | ||
if (target instanceof EventTarget) { | ||
target.removeEventListener(eventName, handler); | ||
} | ||
resolve(event); | ||
}; | ||
|
||
if (target instanceof EventTarget) { | ||
target.addEventListener(eventName, handler); | ||
} else { | ||
target.once(eventName, handler); | ||
} | ||
}); | ||
} |
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,75 @@ | ||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */ | ||
/* eslint-disable @typescript-eslint/no-unsafe-argument */ | ||
/* eslint-disable @typescript-eslint/no-unsafe-call */ | ||
import { usingTemporaryFiles } from "using-temporary-files"; | ||
|
||
import { CodeGenerator } from "../../src/server/code-generator.js"; | ||
import { waitForEvent } from "../../src/util/wait-for-event.js"; | ||
|
||
const OPENAPI = { | ||
components: { schemas: { Example: { type: "string" } } }, | ||
openapi: "3.0.3", | ||
|
||
paths: { | ||
"/example": { | ||
get: { | ||
responses: { | ||
default: { | ||
content: { | ||
"application/json": { | ||
schema: { | ||
$ref: "#/components/schemas/Example", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
describe("a CodeGenerator", () => { | ||
it("generates code for a component", async () => { | ||
await usingTemporaryFiles(async ({ add, path, read }) => { | ||
await add("openapi.json", JSON.stringify(OPENAPI)); | ||
const generator = new CodeGenerator( | ||
path("./openapi.json"), | ||
path("./out"), | ||
); | ||
|
||
await generator.watch(); | ||
|
||
const exampleComponent = await read("./out/components/Example.ts"); | ||
|
||
await generator.stopWatching(); | ||
|
||
expect(exampleComponent).toEqual("export type Example = string;\n"); | ||
}); | ||
}); | ||
|
||
it("updates the code when the spec changes", async () => { | ||
await usingTemporaryFiles(async ({ add, path, read }) => { | ||
await add("openapi.json", JSON.stringify(OPENAPI)); | ||
const generator = new CodeGenerator( | ||
path("./openapi.json"), | ||
path("./out"), | ||
); | ||
|
||
const changed = { ...OPENAPI }; | ||
changed.components.schemas.Example.type = "integer"; | ||
|
||
await generator.watch(); | ||
|
||
await add("openapi.json", JSON.stringify(changed)); | ||
|
||
await waitForEvent(generator, "generate"); | ||
|
||
const exampleComponent = await read("./out/components/Example.ts"); | ||
|
||
await generator.stopWatching(); | ||
|
||
expect(exampleComponent).toEqual("export type Example = number;\n"); | ||
}); | ||
}); | ||
}); |
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 @@ | ||
import { DUMMY_EXPORT_FOR_TEST_COVERAGE } from "../../src/server/config.js"; | ||
|
||
it("does nothing, just including because C8 coverage doesn't recognize type-only files", () => { | ||
expect(DUMMY_EXPORT_FOR_TEST_COVERAGE).toBe(DUMMY_EXPORT_FOR_TEST_COVERAGE); | ||
}); |
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