Skip to content

Commit b4935ab

Browse files
committed
cleans up unit tests for better test isolation
1 parent 4308d91 commit b4935ab

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

packages/amazonq/test/e2e/amazonq/testGen.test.ts

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import { qTestingFramework } from './framework/framework'
99
import sinon from 'sinon'
1010
import { Messenger } from './framework/messenger'
1111
import { FollowUpTypes } from 'aws-core-vscode/amazonq'
12-
import { registerAuthHook, using, TestFolder, closeAllEditors } from 'aws-core-vscode/test'
12+
import { registerAuthHook, using, TestFolder, closeAllEditors, getTestWorkspaceFolder } from 'aws-core-vscode/test'
1313
import { loginToIdC } from './utils/setup'
1414
import { waitUntil, workspaceUtils } from 'aws-core-vscode/shared'
15+
import * as path from 'path'
1516

1617
describe('Amazon Q Test Generation', function () {
1718
let framework: qTestingFramework
@@ -20,11 +21,13 @@ describe('Amazon Q Test Generation', function () {
2021
const testFiles = [
2122
{
2223
language: 'python',
23-
filePath: 'python3.7-image-sam-app/hello_world/app.py',
24+
filePath: 'testGenFolder/src/main/math.py',
25+
testFilePath: 'testGenFolder/src/test/test_math.py',
2426
},
2527
{
2628
language: 'java',
27-
filePath: 'java17-gradle/HelloWorldFunction/src/main/java/helloworld/App.java',
29+
filePath: 'testGenFolder/src/main/Math.java',
30+
testFilePath: 'testGenFolder/src/test/MathTest.java',
2831
},
2932
]
3033

@@ -33,14 +36,15 @@ describe('Amazon Q Test Generation', function () {
3336
// must be atleast one unsupported language here for testing
3437
{
3538
language: 'typescript',
36-
filePath: 'ts-plain-sam-app/src/app.ts',
39+
filePath: 'testGenFolder/src/main/math.ts',
3740
},
3841
{
3942
language: 'javascript',
40-
filePath: 'js-plain-sam-app/src/app.js',
43+
filePath: 'testGenFolder/src/main/math.js',
4144
},
4245
]
4346

47+
// handles opening the file since /test must be called on an active file
4448
async function setupTestDocument(filePath: string, language: string) {
4549
const document = await waitUntil(async () => {
4650
const doc = await workspaceUtils.openTextDocument(filePath)
@@ -68,6 +72,15 @@ describe('Amazon Q Test Generation', function () {
6872
})
6973
}
7074

75+
// clears test file to a blank file
76+
// not cleaning up test file may possibly cause bloat in CI since testFixtures does not get reset
77+
async function cleanupTestFile(testFilePath: string) {
78+
const workspaceFolder = getTestWorkspaceFolder()
79+
const absoluteTestFilePath = path.join(workspaceFolder, testFilePath)
80+
const testFileUri = vscode.Uri.file(absoluteTestFilePath)
81+
await vscode.workspace.fs.writeFile(testFileUri, Buffer.from('', 'utf-8'))
82+
}
83+
7184
before(async function () {
7285
await using(registerAuthHook('amazonq-test-account'), async () => {
7386
await loginToIdC()
@@ -112,7 +125,7 @@ describe('Amazon Q Test Generation', function () {
112125
})
113126

114127
describe('/test entry', () => {
115-
describe('Unsupported language', () => {
128+
describe('Unsupported language file', () => {
116129
const { language, filePath } = unsupportedLanguages[0]
117130

118131
beforeEach(async () => {
@@ -134,13 +147,13 @@ describe('Amazon Q Test Generation', function () {
134147
})
135148
})
136149

137-
describe('External file', async () => {
150+
describe('External file out of project', async () => {
138151
let testFolder: TestFolder
139152
let fileName: string
140153

141154
beforeEach(async () => {
142155
testFolder = await TestFolder.create()
143-
fileName = 'test.py'
156+
fileName = 'math.py'
144157
const filePath = await testFolder.write(fileName, 'def add(a, b): return a + b')
145158

146159
const document = await vscode.workspace.openTextDocument(filePath)
@@ -162,8 +175,8 @@ describe('Amazon Q Test Generation', function () {
162175
})
163176
})
164177

165-
for (const { language, filePath } of testFiles) {
166-
describe(`${language} file`, () => {
178+
for (const { language, filePath, testFilePath } of testFiles) {
179+
describe(`/test on ${language} file`, () => {
167180
beforeEach(async () => {
168181
await waitUntil(async () => await setupTestDocument(filePath, language), {})
169182

@@ -175,7 +188,7 @@ describe('Amazon Q Test Generation', function () {
175188
await tab.waitForChatFinishesLoading()
176189
})
177190

178-
describe('View diff', async () => {
191+
describe('View diff of test file', async () => {
179192
it('Clicks on view diff', async () => {
180193
const chatItems = tab.getChatItems()
181194
const viewDiffMessage = chatItems[5]
@@ -188,7 +201,14 @@ describe('Amazon Q Test Generation', function () {
188201
})
189202
})
190203

191-
describe('Accept code', async () => {
204+
describe('Accept unit tests', async () => {
205+
afterEach(async () => {
206+
// this e2e test generates unit tests, so we want to clean them up after this test is done
207+
await waitUntil(async () => {
208+
await cleanupTestFile(testFilePath)
209+
}, {})
210+
})
211+
192212
it('Clicks on accept', async () => {
193213
await tab.waitForButtons([FollowUpTypes.AcceptCode, FollowUpTypes.RejectCode])
194214
tab.clickButton(FollowUpTypes.AcceptCode)
@@ -202,7 +222,7 @@ describe('Amazon Q Test Generation', function () {
202222
})
203223
})
204224

205-
describe('Reject code', async () => {
225+
describe('Reject unit tests', async () => {
206226
it('Clicks on reject', async () => {
207227
await tab.waitForButtons([FollowUpTypes.AcceptCode, FollowUpTypes.RejectCode])
208228
tab.clickButton(FollowUpTypes.RejectCode)

0 commit comments

Comments
 (0)