Skip to content

Commit f56978d

Browse files
authored
fix(tests): "TypeError: … undefined (reading 'isMapped')" #4250
Problem: Even after 6d66720, tests still sometimes fail because the global schemaService is not properly restored: createNewSamApp addInitialLaunchConfiguration produces and returns initial launch configurations: TypeError: Cannot read properties of undefined (reading 'registerMapping') at CloudFormationTemplateRegistry.process (src/shared/fs/templateRegistry.ts:38:35) at async CloudFormationTemplateRegistry.addItem (src/shared/fs/watchedFiles.ts:214:26) at async Context.<anonymous> (src/test/lambda/commands/createNewSamApp.test.ts:118:13) ... rejected promise not handled within 1 second: TypeError: Cannot read properties of undefined (reading 'isMapped') stack trace: TypeError: Cannot read properties of undefined (reading 'isMapped') at /Users/runner/work/aws-toolkit-vscode/aws-toolkit-vscode/src/shared/awsFiletypes.ts:69:59 Solution: Use a spy on the actual service, instead of replacing the service with a temporary stub.
1 parent a21f6fc commit f56978d

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

src/test/dynamicResources/awsResourceManager.test.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import { CloudControlClient, DefaultCloudControlClient } from '../../shared/clie
1616
import { CloudFormationClient, DefaultCloudFormationClient } from '../../shared/clients/cloudFormationClient'
1717
import { makeTemporaryToolkitFolder, readFileAsString } from '../../shared/filesystemUtilities'
1818
import { FakeExtensionContext } from '../fakeExtensionContext'
19-
import { SchemaService } from '../../shared/schemas'
2019
import { remove } from 'fs-extra'
2120
import { existsSync } from 'fs'
2221
import { ResourceTypeMetadata } from '../../dynamicResources/model/resources'
@@ -31,13 +30,12 @@ describe('ResourceManager', function () {
3130
let resourceNode: ResourceNode
3231
let resourceTypeNode: ResourceTypeNode
3332
let resourceManager: AwsResourceManager
34-
let schemaService: Stub<SchemaService>
3533
let tempFolder: string
34+
let registerMappingSpy: sinon.SinonSpy
3635

3736
const fakeTypeName = 'sometype'
3837
const fakeIdentifier = 'someidentifier'
3938
const fakeRegion = 'someregion'
40-
const oldSchemaService = globals.schemaService
4139

4240
const fakeResourceDescription = {
4341
Role: 'arn:aws:iam::1234:role/service-role/fooResource',
@@ -56,10 +54,6 @@ describe('ResourceManager', function () {
5654
cloudFormation = stub(DefaultCloudFormationClient, {
5755
regionCode: '',
5856
})
59-
schemaService = stub(SchemaService, {
60-
registerMapping: sinon.stub(),
61-
isMapped: sinon.stub(),
62-
})
6357
sandbox = sinon.createSandbox()
6458
mockClients()
6559
tempFolder = await makeTemporaryToolkitFolder()
@@ -70,19 +64,16 @@ describe('ResourceManager', function () {
7064
const fakeContext = await FakeExtensionContext.create()
7165
fakeContext.globalStorageUri = vscode.Uri.file(tempFolder)
7266
resourceManager = new AwsResourceManager(fakeContext)
73-
globals.schemaService = schemaService
67+
registerMappingSpy = sinon.spy(globals.schemaService, 'registerMapping')
7468
})
7569

7670
afterEach(async function () {
71+
registerMappingSpy.restore()
7772
sandbox.restore()
7873
await resourceManager.dispose()
7974
await remove(tempFolder)
8075
})
8176

82-
after(function () {
83-
globals.schemaService = oldSchemaService
84-
})
85-
8677
it('opens resources in preview mode', async function () {
8778
const mockTextDocument = {} as vscode.TextDocument
8879
const mockTextEditor = {} as vscode.TextEditor
@@ -193,9 +184,9 @@ describe('ResourceManager', function () {
193184

194185
it('registers schema mappings when opening in edit', async function () {
195186
const editor = await resourceManager.open(resourceNode, false)
196-
assert(schemaService.registerMapping.calledOnce)
187+
assert(registerMappingSpy.calledOnce)
197188

198-
const [mapping] = schemaService.registerMapping.args[schemaService.registerMapping.args.length - 1]
189+
const [mapping] = registerMappingSpy.args[registerMappingSpy.args.length - 1]
199190

200191
const expectedSchemaLocation = path.join(tempFolder, 'sometype.schema.json')
201192
assert.ok(existsSync(expectedSchemaLocation))
@@ -212,17 +203,17 @@ describe('ResourceManager', function () {
212203
sandbox.stub(vscode.window, 'showTextDocument').resolves(mockTextEditor)
213204

214205
await resourceManager.open(resourceNode, true)
215-
assert(schemaService.registerMapping.notCalled)
206+
assert(registerMappingSpy.notCalled)
216207
assert(cloudFormation.describeType.notCalled)
217208
})
218209

219210
it('deletes resource mapping on file close', async function () {
220211
const editor = await resourceManager.open(resourceNode, false)
221212
await resourceManager.close(editor.document.uri)
222-
assert(schemaService.registerMapping.firstCall.calledWith(sinon.match.any, true))
223-
assert(schemaService.registerMapping.secondCall.calledWith(sinon.match.any))
213+
assert(registerMappingSpy.firstCall.calledWith(sinon.match.any, true))
214+
assert(registerMappingSpy.secondCall.calledWith(sinon.match.any))
224215

225-
const [mapping] = schemaService.registerMapping.args[schemaService.registerMapping.args.length - 1]
216+
const [mapping] = registerMappingSpy.args[registerMappingSpy.args.length - 1]
226217

227218
assert.strictEqual(mapping.type, 'json')
228219
testutil.assertEqualPaths(mapping.uri.fsPath, editor.document.uri.fsPath)

0 commit comments

Comments
 (0)