This repository has been archived by the owner on Jan 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
write a snapshot test on new and migrate
- Loading branch information
Jamie Lynch
committed
Jan 22, 2021
1 parent
5c89afa
commit 4ad7c58
Showing
8 changed files
with
213 additions
and
7 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
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,47 @@ | ||
import { MockCodeMaker } from "../../test/utils/codemaker"; | ||
import { Imports } from "./imports"; | ||
import { TestBuilder } from "./snapshot"; | ||
|
||
describe("The TestBuilder class", () => { | ||
describe("addTest function", () => { | ||
const builder = new TestBuilder({ | ||
outputDir: "", | ||
outputFile: "", | ||
stackName: "StackName", | ||
appName: "AppName", | ||
imports: new Imports(), | ||
}); | ||
const codemaker = new MockCodeMaker(); | ||
builder.code = codemaker.codemaker; | ||
|
||
beforeEach(() => { | ||
codemaker.clear(); | ||
}); | ||
|
||
test("adds a snapshot test", () => { | ||
builder.addTest(); | ||
expect(codemaker._codemaker.openBlock).toHaveBeenNthCalledWith( | ||
1, | ||
`describe("The StackName stack", () =>` | ||
); | ||
expect(codemaker._codemaker.openBlock).toHaveBeenNthCalledWith( | ||
2, | ||
`it("matches the snapshot", () =>` | ||
); | ||
expect(codemaker._codemaker.line).toHaveBeenNthCalledWith( | ||
1, | ||
`const app = new App();` | ||
); | ||
expect(codemaker._codemaker.line).toHaveBeenNthCalledWith( | ||
2, | ||
`const stack = new StackName(app, "stack-name", { app: "app-name" });` | ||
); | ||
expect(codemaker._codemaker.line).toHaveBeenNthCalledWith( | ||
3, | ||
`expect(SynthUtils.toCloudFormation(stack)).toMatchSnapshot();` | ||
); | ||
expect(codemaker._codemaker.closeBlock).toHaveBeenNthCalledWith(1, `});`); | ||
expect(codemaker._codemaker.closeBlock).toHaveBeenNthCalledWith(2, `});`); | ||
}); | ||
}); | ||
}); |
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,65 @@ | ||
import { CodeMaker } from "codemaker"; | ||
import kebabCase from "lodash.kebabcase"; | ||
import type { Imports } from "./imports"; | ||
|
||
export interface TestBuilderProps { | ||
imports: Imports; | ||
appName: string; | ||
stackName: string; | ||
outputFile: string; | ||
outputDir: string; | ||
comment?: string; | ||
} | ||
|
||
export class TestBuilder { | ||
config: TestBuilderProps; | ||
imports: Imports; | ||
|
||
code: CodeMaker; | ||
|
||
constructor(props: TestBuilderProps) { | ||
this.config = props; | ||
this.imports = props.imports; | ||
|
||
this.code = new CodeMaker({ indentationLevel: 2 }); | ||
this.code.closeBlockFormatter = (s?: string): string => s ?? "}"; | ||
} | ||
|
||
async constructCdkFile(): Promise<void> { | ||
this.code.openFile(this.config.outputFile); | ||
if (this.config.comment) { | ||
this.code.line(this.config.comment); | ||
this.code.line(); | ||
} | ||
|
||
this.config.imports.render(this.code); | ||
|
||
this.addTest(); | ||
|
||
this.code.closeFile(this.config.outputFile); | ||
await this.code.save(this.config.outputDir); | ||
} | ||
|
||
addTest(): void { | ||
this.code.openBlock(`describe("The ${this.config.stackName} stack", () =>`); | ||
this.code.openBlock(`it("matches the snapshot", () =>`); | ||
|
||
this.code.line("const app = new App();"); | ||
this.code.line( | ||
`const stack = new ${this.config.stackName}(app, "${kebabCase( | ||
this.config.stackName | ||
)}", { app: "${kebabCase(this.config.appName)}" });` | ||
); | ||
this.code.line( | ||
"expect(SynthUtils.toCloudFormation(stack)).toMatchSnapshot();" | ||
); | ||
|
||
this.code.closeBlock("});"); | ||
this.code.closeBlock("});"); | ||
} | ||
} | ||
|
||
export const constructTest = async (props: TestBuilderProps): Promise<void> => { | ||
const builder = new TestBuilder(props); | ||
await builder.constructCdkFile(); | ||
}; |