Skip to content
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

Refactor Integration Tests to Jest #567

Merged
merged 10 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ jobs:

steps:
- uses: actions/checkout@v2
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- uses: actions/cache@v2
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: installing dependencies
run: |
yarn install
Expand Down Expand Up @@ -56,11 +66,23 @@ jobs:

steps:
- uses: actions/checkout@v2
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- uses: actions/cache@v2
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Download dist
uses: actions/download-artifact@v2
with:
name: dist
path: dist
- name: installing dependencies
run: yarn install
- name: integration tests
run: yarn integration:${TEST_TARGET}
env:
Expand All @@ -81,6 +103,16 @@ jobs:

steps:
- uses: actions/checkout@v2
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- uses: actions/cache@v2
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: HashiCorp - Setup Terraform
uses: hashicorp/setup-terraform@v1
with:
Expand All @@ -93,6 +125,8 @@ jobs:
with:
name: dist
path: dist
- name: installing dependencies
run: yarn install
- name: integration tests
run: yarn integration:windows:${env:TEST_TARGET}
env:
Expand Down
3 changes: 2 additions & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"examples/python/*",
"examples/typescript/backends/*",
"examples/java/*",
"examples/csharp/*"
"examples/csharp/*",
"test"
],
"npmClient": "yarn",
"useWorkspaces": true,
Expand Down
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
"test": "lerna run --scope cdktf* test",
"watch": "lerna run --parallel --stream --scope cdktf* watch-preserve-output",
"link-packages": "lerna exec --scope cdktf* yarn link",
"integration": "test/run-against-dist test/test-all.sh",
"integration:typescript": "test/run-against-dist test/test-all.sh typescript",
"integration:python": "test/run-against-dist test/test-all.sh python",
"integration:csharp": "test/run-against-dist test/test-all.sh csharp",
"integration:java": "test/run-against-dist test/test-all.sh java",
"integration:windows": "test\\run-against-dist.bat test\\test-all.bat",
"integration:windows:typescript": "test\\run-against-dist.bat \"test\\test-all.bat typescript\"",
"integration:windows:python": "test\\run-against-dist.bat \"test\\test-all.bat python\"",
"integration:windows:csharp": "test\\run-against-dist.bat \"test\\test-all.bat csharp\"",
"integration:windows:java": "test\\run-against-dist.bat \"test\\test-all.bat java\"",
"integration": "cd test && ./run-against-dist npx jest --runInBand",
"integration:typescript": "cd test && ./run-against-dist npx jest --runInBand --group=typescript",
"integration:python": "cd test && ./run-against-dist npx jest --runInBand --group=python",
"integration:csharp": "cd test && ./run-against-dist npx jest --runInBand --group=csharp",
"integration:java": "cd test && ./run-against-dist npx jest --runInBand --group=java",
"integration:windows": "cd test && run-against-dist.bat \"npx jest --runInBand\"",
"integration:windows:typescript": "cd test && run-against-dist.bat \"npx jest --runInBand --group=typescript\"",
"integration:windows:python": "cd test && run-against-dist.bat \"npx jest --runInBand --group=python\"",
"integration:windows:csharp": "cd test && run-against-dist.bat \"npx jest --runInBand --group=csharp\"",
"integration:windows:java": "cd test && run-against-dist.bat \"npx jest --runInBand --group=java\"",
"release-github": "tools/release-github.sh",
"build-docker-jsii": "docker build -t hashicorp/jsii-terraform .",
"push-docker-jsii": "docker push hashicorp/jsii-terraform",
Expand All @@ -43,6 +43,7 @@
"examples/typescript/backends/*",
"examples/java/*",
"examples/csharp/*",
"test",
".yalc/*",
".yalc/@*/*"
],
Expand Down
4 changes: 2 additions & 2 deletions packages/cdktf-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
},
"license": "MPL-2.0",
"dependencies": {
"@skorfmann/terraform-cloud": "^1.7.1",
"@skorfmann/ink-confirm-input": "^3.0.0",
"@skorfmann/terraform-cloud": "^1.9.1",
"@types/node": "^14.0.26",
"archiver": "^5.1.0",
"cdktf": "0.0.0",
Expand All @@ -36,7 +37,6 @@
"constructs": "^3.0.0",
"fs-extra": "^8.1.0",
"ink": "^3.0.8",
"@skorfmann/ink-confirm-input": "^3.0.0",
"ink-spinner": "^4.0.1",
"jsii-srcmak": "^0.1.195",
"log4js": "^6.3.0",
Expand Down
17 changes: 7 additions & 10 deletions test/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

This directory contains integration tests for the cdktf project.

Each subdirectory represents a single test, with an entrypoint of `test.sh`.
Tests are written as simple shell scripts and can simulate user activity.
Each subdirectory represents a target language, where each test is another nested folder, with an entrypoint of `test.ts`.
Tests are written in Typescript and Jest and can simulate user activity.

## Running Tests

You can either run individual tests by executing their entrypoint directly (e.g.
`test-python-app/test.sh`) or run all tests by executing the script
`./test-all.sh`.
`npx jest --runInBand typescript/synth-app/test.ts`) or run all tests by executing `npx jest --runInBand`.

Tests assume the `cdktf` CLI is installed and in the PATH, and will use the same
version of the `cdktf` module (this is the behavior of `cdktf init`).
Expand All @@ -21,18 +20,16 @@ $ yarn install
$ yarn build
$ yarn run package # creates "dist/"
$ cd test
$ ./run-against-dist ./test-all.sh
# or
$ ./run-against-dist ./test-typescript-app/test.sh
$ ./run-against-dist npx jest --runInBand
```

## Writing Tests

1. Create a new subdirectory with a `test-` prefix.
2. Create a file named `test.sh`, make it executable.
1. Create a new subdirectory in the directory of your target language.
2. Create a file named `test.ts`

Test Environment:
- The script `test.sh` is executed within a temporary working directory under
- The test script is executed within a temporary working directory under
/tmp/xxxx/test (where xxxx is some random tmp file).
- See existing tests as examples on how to bring in auxiliary files to the test.
- Test MUST NOT install any dependencies or the `cdktf` CLI. They can expect it
Expand Down
File renamed without changes.
40 changes: 40 additions & 0 deletions test/csharp/synth-app/__snapshots__/test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`csharp full integration test synth synth generates JSON 1`] = `
"{
\\"//\\": {
\\"metadata\\": {
\\"version\\": \\"stubbed\\",
\\"stackName\\": \\"csharp-simple\\"
}
},
\\"terraform\\": {
\\"required_providers\\": {
\\"aws\\": {
\\"version\\": \\"~> 2.0\\",
\\"source\\": \\"aws\\"
}
}
},
\\"provider\\": {
\\"aws\\": [
{
\\"region\\": \\"eu-central-1\\"
}
]
},
\\"resource\\": {
\\"aws_sns_topic\\": {
\\"Topic\\": {
\\"display_name\\": \\"my-first-sns-topic\\",
\\"//\\": {
\\"metadata\\": {
\\"path\\": \\"csharp-simple/Topic\\",
\\"uniqueId\\": \\"Topic\\"
}
}
}
}
}
}"
`;
File renamed without changes.
20 changes: 20 additions & 0 deletions test/csharp/synth-app/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
*
* @group csharp
*/

import { TestDriver } from "../../test-helper";

describe("csharp full integration test synth", () => {
let driver: TestDriver;

beforeAll(() => {
driver = new TestDriver(__dirname)
driver.setupCsharpProject()
});

test("synth generates JSON", async () => {
driver.synth()
expect(driver.synthesizedStack()).toMatchSnapshot()
})
})
File renamed without changes.
40 changes: 40 additions & 0 deletions test/java/synth-app/__snapshots__/test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`java full integration synth generates JSON 1`] = `
"{
\\"//\\": {
\\"metadata\\": {
\\"version\\": \\"stubbed\\",
\\"stackName\\": \\"java-simple\\"
}
},
\\"terraform\\": {
\\"required_providers\\": {
\\"aws\\": {
\\"version\\": \\"~> 2.0\\",
\\"source\\": \\"aws\\"
}
}
},
\\"provider\\": {
\\"aws\\": [
{
\\"region\\": \\"eu-central-1\\"
}
]
},
\\"resource\\": {
\\"aws_sns_topic\\": {
\\"Topic\\": {
\\"display_name\\": \\"my-first-sns-topic\\",
\\"//\\": {
\\"metadata\\": {
\\"path\\": \\"java-simple/Topic\\",
\\"uniqueId\\": \\"Topic\\"
}
}
}
}
}
}"
`;
File renamed without changes.
20 changes: 20 additions & 0 deletions test/java/synth-app/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
*
* @group java
*/

import { TestDriver } from "../../test-helper";

describe("java full integration", () => {
let driver: TestDriver;

beforeAll(() => {
driver = new TestDriver(__dirname)
driver.setupJavaProject()
});

test("synth generates JSON", async () => {
driver.synth()
expect(driver.synthesizedStack()).toMatchSnapshot()
})
})
13 changes: 13 additions & 0 deletions test/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
roots: [
"<rootDir>"
],
testMatch: ['**/*test.ts'],
moduleFileExtensions: [
"js",
"ts"
],
runner: "groups"
}
15 changes: 15 additions & 0 deletions test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "end2end-tests",
"version": "0.0.0",
"description": "This directory contains integration tests for the cdktf project.",
"scripts": {},
"license": "MPL-2.0",
"devDependencies": {
"@skorfmann/terraform-cloud": "^1.9.1",
"@types/jest": "^26.0.20",
"jest": "^26.6.3",
"jest-runner-groups": "^2.0.1",
"ts-jest": "^26.5.1",
"typescript": "^4.1.5"
}
}
61 changes: 61 additions & 0 deletions test/python/synth-app/__snapshots__/test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`python full integration test synth synth generates JSON 1`] = `
"{
\\"//\\": {
\\"metadata\\": {
\\"version\\": \\"stubbed\\",
\\"stackName\\": \\"python-simple\\"
}
},
\\"terraform\\": {
\\"required_providers\\": {
\\"aws\\": {
\\"version\\": \\"~> 2.0\\",
\\"source\\": \\"aws\\"
}
},
\\"backend\\": {
\\"remote\\": {
\\"organization\\": \\"test\\",
\\"workspaces\\": {
\\"name\\": \\"test\\"
}
}
}
},
\\"provider\\": {
\\"aws\\": [
{
\\"region\\": \\"eu-central-1\\"
}
]
},
\\"module\\": {
\\"CustomVpc\\": {
\\"name\\": \\"custom-vpc\\",
\\"source\\": \\"terraform-aws-modules/vpc/aws\\",
\\"version\\": \\"2.39.0\\",
\\"//\\": {
\\"metadata\\": {
\\"path\\": \\"python-simple/CustomVpc\\",
\\"uniqueId\\": \\"CustomVpc\\"
}
}
}
},
\\"resource\\": {
\\"aws_sns_topic\\": {
\\"Topic\\": {
\\"display_name\\": \\"my-first-sns-topic\\",
\\"//\\": {
\\"metadata\\": {
\\"path\\": \\"python-simple/Topic\\",
\\"uniqueId\\": \\"Topic\\"
}
}
}
}
}
}"
`;
File renamed without changes.
File renamed without changes.
Loading