Skip to content

Instantly share code, notes, and snippets.

@revmischa
Created May 16, 2021 11:53
Show Gist options
  • Save revmischa/b1814a1ead320f421b68d1b30988ab32 to your computer and use it in GitHub Desktop.
Save revmischa/b1814a1ead320f421b68d1b30988ab32 to your computer and use it in GitHub Desktop.
Unit test that tries to compile each code block in the README.md
import { createProjectSync, ts } from "@ts-morph/bootstrap"
import * as fs from "fs"
import * as path from "path"
import { default as SimpleMarkdown } from "simple-markdown"
describe("README examples", () => {
const readmePath = path.join(__dirname, "..", "..", "README.md")
const readmeContents = fs.readFileSync(readmePath)
// extract examples
const parser = SimpleMarkdown.parserFor(SimpleMarkdown.defaultRules)
const parsed = parser(readmeContents.toString())
let exampleNum = 1
parsed.forEach((block) => {
if (block.type !== "codeBlock" || block.lang != "typescript") return
describe(`compiles example ${exampleNum++}`, () => {
compile(block.content)
})
})
})
function compile(input: string): void {
const project = createProjectSync({ tsConfigFilePath: "tsconfig.json" })
// hack to make it possible to import the project source files instead of "@jetkit/cdk"
input = input.replace('from "@jetkit/cdk"', 'from "./src/index"')
// build program
project.createSourceFile("example.ts", input)
const program = project.createProgram()
// try to compile
const emitResult = program.emit()
// get errors
const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics)
allDiagnostics.forEach((diagnostic) => {
if (diagnostic.file) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const { line, character } = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start!)
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")
console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`)
} else {
console.log(ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"))
}
})
it("compiles without errors or warnings", () => {
expect(allDiagnostics).toHaveLength(0)
expect(emitResult.emitSkipped).toBeFalsy()
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment