-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathindex.test.ts
More file actions
147 lines (126 loc) · 5.75 KB
/
index.test.ts
File metadata and controls
147 lines (126 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright (c) 2024, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { expect } from 'chai';
import { LWCServer, Workspace } from '@lwc/lwc-dev-server';
import esmock from 'esmock';
import sinon from 'sinon';
import { TestContext } from '@salesforce/core/testSetup';
import { AuthInfo, Logger, SfProject } from '@salesforce/core';
import * as devServer from '../../src/lwc-dev-server/index.js';
import { ConfigUtils } from '../../src/shared/configUtils.js';
describe('lwc-dev-server', () => {
const $$ = new TestContext();
const server = {
stopServer: () => {},
} as LWCServer;
let lwcDevServer: typeof devServer;
let mockLogger: Logger;
let mockProject: Partial<SfProject>;
let getLocalDevServerPortsStub: sinon.SinonStub;
let getLocalDevServerWorkspaceStub: sinon.SinonStub;
before(async () => {
lwcDevServer = await esmock<typeof devServer>('../../src/lwc-dev-server/index.js', {
'@lwc/lwc-dev-server': {
startLwcDevServer: async () => server,
},
});
});
beforeEach(async () => {
getLocalDevServerPortsStub = $$.SANDBOX.stub(ConfigUtils, 'getLocalDevServerPorts').resolves({
httpPort: 1234,
httpsPort: 5678,
});
getLocalDevServerWorkspaceStub = $$.SANDBOX.stub(ConfigUtils, 'getLocalDevServerWorkspace').resolves(
Workspace.SfCli
);
mockLogger = await Logger.child('test');
mockProject = {
getDefaultPackage: $$.SANDBOX.stub().returns({ fullPath: '/fake/path' }),
getPackageDirectories: $$.SANDBOX.stub().returns([{ fullPath: '/fake/path' }]),
resolveProjectConfig: $$.SANDBOX.stub().resolves({ namespace: '' }),
};
$$.SANDBOX.stub(SfProject, 'resolve').resolves(mockProject as unknown as SfProject);
});
afterEach(() => {
$$.restore();
});
it('exports a startLWCServer function', () => {
expect(lwcDevServer.startLWCServer).to.be.a('function');
});
describe('JWT Authentication Error Handling', () => {
it('should throw helpful error when no authorization information is found', async () => {
const authError = new Error('No authorization information found for user test-user@example.com');
$$.SANDBOX.stub(AuthInfo, 'create').rejects(authError);
try {
await lwcDevServer.startLWCServer(mockLogger, '/fake/path', 'fake-token', 'test-user@example.com');
expect.fail('Expected function to throw an error');
} catch (error) {
expect(error).to.be.an('error');
expect((error as Error).message).to.include('JWT authentication not found for user test-user@example.com');
expect((error as Error).message).to.include("Please run 'sf org login jwt' or 'sf org login web' first");
}
});
it('should throw helpful error when JWT token is expired', async () => {
const authError = new Error('JWT token expired for user test-user@example.com');
$$.SANDBOX.stub(AuthInfo, 'create').rejects(authError);
try {
await lwcDevServer.startLWCServer(mockLogger, '/fake/path', 'fake-token', 'test-user@example.com');
expect.fail('Expected function to throw an error');
} catch (error) {
expect(error).to.be.an('error');
expect((error as Error).message).to.include(
'JWT authentication expired or invalid for user test-user@example.com'
);
expect((error as Error).message).to.include(
"Please re-authenticate using 'sf org login jwt' or 'sf org login web'"
);
}
});
it('should throw helpful error when JWT token is invalid', async () => {
const authError = new Error('Invalid JWT token for user test-user@example.com');
$$.SANDBOX.stub(AuthInfo, 'create').rejects(authError);
try {
await lwcDevServer.startLWCServer(mockLogger, '/fake/path', 'fake-token', 'test-user@example.com');
expect.fail('Expected function to throw an error');
} catch (error) {
expect(error).to.be.an('error');
expect((error as Error).message).to.include(
'JWT authentication expired or invalid for user test-user@example.com'
);
expect((error as Error).message).to.include(
"Please re-authenticate using 'sf org login jwt' or 'sf org login web'"
);
}
});
it('should throw helpful error for generic authentication failures', async () => {
const authError = new Error('Some other authentication error');
$$.SANDBOX.stub(AuthInfo, 'create').rejects(authError);
try {
await lwcDevServer.startLWCServer(mockLogger, '/fake/path', 'fake-token', 'test-user@example.com');
expect.fail('Expected function to throw an error');
} catch (error) {
expect(error).to.be.an('error');
expect((error as Error).message).to.include(
'JWT authentication not found or invalid for user test-user@example.com'
);
expect((error as Error).message).to.include('Some other authentication error');
}
});
});
it('calling startLWCServer returns an LWCServer', async () => {
const mockAuthInfo = {
getUsername: () => 'test-user@example.com',
};
const authInfoStub = $$.SANDBOX.stub(AuthInfo, 'create').resolves(mockAuthInfo as unknown as AuthInfo);
const fakeIdentityToken = 'PFT1vw8v65aXd2b9HFvZ3Zu4OcKZwjI60bq7BEjj5k4=';
const s = await lwcDevServer.startLWCServer(mockLogger, '/fake/path', fakeIdentityToken, 'test-user@example.com');
expect(s).to.equal(server);
expect(getLocalDevServerPortsStub.calledOnce).to.be.true;
expect(getLocalDevServerWorkspaceStub.calledOnce).to.be.true;
expect(authInfoStub.calledOnceWith({ username: 'test-user@example.com' })).to.be.true;
});
});