Skip to content

Commit

Permalink
[ENG-1350] use vitest (konfig-dev#337)
Browse files Browse the repository at this point in the history
* use vitest for testing

* delete jest files

* re-enable httpsnippet test

* fix redundancies

* add code-generator-typescript.test.ts

* add needs:

* use vitest for integration tests

* update snapshots for vitest
  • Loading branch information
dphuang2 authored Nov 5, 2023
1 parent bba0b55 commit bc44a4e
Show file tree
Hide file tree
Showing 48 changed files with 3,008 additions and 4,109 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/konfig-api-portal-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ on:
push:
branches:
- main
- 'ENG-*' # https://github.com/jitterbit/get-changed-files/issues/10#issuecomment-1012312507
pull_request:

jobs:
Expand All @@ -29,6 +28,7 @@ jobs:
fi
done
unit-tests:
needs: check_for_relevant_changes
if: needs.check_for_relevant_changes.outputs.shouldRun == 'true'
strategy:
matrix:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ on:
push:
branches:
- main
- 'ENG-*' # https://github.com/jitterbit/get-changed-files/issues/10#issuecomment-1012312507
pull_request:

jobs:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/konfig-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ on:
push:
branches:
- main
- 'ENG-*' # https://github.com/jitterbit/get-changed-files/issues/10#issuecomment-1012312507
pull_request:

jobs:
Expand All @@ -29,6 +28,7 @@ jobs:
fi
done
unit-tests:
needs: check_for_relevant_changes
if: needs.check_for_relevant_changes.outputs.shouldRun == 'true'
strategy:
matrix:
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ jobs:
fi
done
release:
needs: check_for_relevant_changes
if: needs.check_for_relevant_changes.outputs.shouldRun == 'true'
name: Release
runs-on: ubuntu-latest
if: needs.check_for_relevant_changes.outputs.shouldRun == 'true'
env:
DIRECTORY: ./generator/konfig-dash
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion generator/konfig-integration-tests/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ integration-tests:
FROM +konfig-test-dependencies
COPY package.json yarn.lock .
RUN yarn
COPY tsconfig.json jest.config.ts util.ts .
COPY tsconfig.json vitest.config.ts util.ts .
COPY sdks sdks
COPY tests tests
# can be overridden at runtime (e.g. "docker run konfig-integration-tests:latest --testName=TestName")
Expand Down
116 changes: 68 additions & 48 deletions generator/konfig-integration-tests/bootstrap-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,31 @@
* Makes it easy to create new tests
*/

const fs = require('fs');
const readline = require('readline');
const path = require('path');
const fs = require("fs");
const readline = require("readline");
const path = require("path");

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
output: process.stdout,
});

const toCamelCase = (str) => {
return str.split(/[-_ ]+/)
.map((word, index) => index === 0 ? word[0].toUpperCase() + word.slice(1) : word[0].toUpperCase() + word.slice(1))
.join('');
};
return str
.split(/[-_ ]+/)
.map((word, index) =>
index === 0
? word[0].toUpperCase() + word.slice(1)
: word[0].toUpperCase() + word.slice(1)
)
.join("");
};

function generateKonfigYamlString(language, testName) {
const languageSpecificFields = generateKonfigYamlFieldsForLanguage(language, testName);
const languageSpecificFields = generateKonfigYamlFieldsForLanguage(
language,
testName
);
return `outputDirectory: /tmp/${testName}-sdks-out
specPath: api.yaml
generators:
Expand All @@ -35,58 +43,70 @@ generators:
}

function generateKonfigYamlFieldsForLanguage(language, testName) {
if (language == 'python') {
return `packageName: ${testName.replace(/-/g, '_')}
projectName: ${testName}`
} else if (language == 'typescript') {
return `npmName: ${testName}`
if (language == "python") {
return `packageName: ${testName.replace(/-/g, "_")}
projectName: ${testName}`;
} else if (language == "typescript") {
return `npmName: ${testName}`;
}
}

// Prompt for language
rl.question('Which language would you like to create a test for? (python/typescript) ', (languageInput) => {
const language = (languageInput.toLowerCase() === 'python') ? 'python' : 'typescript';
rl.question(
"Which language would you like to create a test for? (python/typescript) ",
(languageInput) => {
const language =
languageInput.toLowerCase() === "python" ? "python" : "typescript";

// Prompt for test name
rl.question('What would you like to name your test? ', (testName) => {
const testFiles = fs.readdirSync('tests').filter(file => file.endsWith('.test.ts'));
let ports = [];
// Prompt for test name
rl.question("What would you like to name your test? ", (testName) => {
const testFiles = fs
.readdirSync("tests")
.filter((file) => file.endsWith(".test.ts"));
let ports = [];

// Extract ports
testFiles.forEach(file => {
const content = fs.readFileSync(path.join('tests', file), 'utf-8');
const matches = content.match(/(?<=e2e\()\d+/g);
if (matches) {
ports.push(...matches.map(match => parseInt(match.replace(/\D/g, ''))));
}
});
// Extract ports
testFiles.forEach((file) => {
const content = fs.readFileSync(path.join("tests", file), "utf-8");
const matches = content.match(/(?<=e2e\()\d+/g);
if (matches) {
ports.push(
...matches.map((match) => parseInt(match.replace(/\D/g, "")))
);
}
});

// Find unused port
ports = ports.sort((a, b) => a - b);
let unusedPort = 4000;
while (ports.includes(unusedPort)) {
// Find unused port
ports = ports.sort((a, b) => a - b);
let unusedPort = 4000;
while (ports.includes(unusedPort)) {
unusedPort++;
}
}

// Create test file
const testContent = `import { e2e } from "../util";
// Create test file
const testContent = `import { e2e } from "../util";
import { test } from "vitest";
test("${testName}", async () => {
await e2e(${unusedPort});
});`;
fs.writeFileSync(`tests/${testName}.test.ts`, testContent);
fs.writeFileSync(`tests/${testName}.test.ts`, testContent);

// Create directory and konfig.yaml file
const sdkPath = `sdks/${testName}`;
fs.mkdirSync(sdkPath, { recursive: true });
fs.writeFileSync(`${sdkPath}/konfig.yaml`, generateKonfigYamlString(language, testName));
// Create directory and konfig.yaml file
const sdkPath = `sdks/${testName}`;
fs.mkdirSync(sdkPath, { recursive: true });
fs.writeFileSync(
`${sdkPath}/konfig.yaml`,
generateKonfigYamlString(language, testName)
);

// Copy api-template.yaml and replace {test_name} with actual test name
const apiTemplate = fs.readFileSync('api-template.yaml', 'utf-8');
const apiContent = apiTemplate.replace(/{test_name}/g, testName);
fs.writeFileSync(`${sdkPath}/api.yaml`, apiContent);
// Copy api-template.yaml and replace {test_name} with actual test name
const apiTemplate = fs.readFileSync("api-template.yaml", "utf-8");
const apiContent = apiTemplate.replace(/{test_name}/g, testName);
fs.writeFileSync(`${sdkPath}/api.yaml`, apiContent);

// Close interface
rl.close();
});
});
// Close interface
rl.close();
});
}
);
Loading

0 comments on commit bc44a4e

Please sign in to comment.