Skip to content

Commit 7b16140

Browse files
committed
fix unit tests once and for all
1 parent ec57625 commit 7b16140

File tree

4 files changed

+17
-26
lines changed

4 files changed

+17
-26
lines changed

src/eventHandlers/WorkerInitHandler.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,14 @@ export class WorkerInitHandler extends EventHandler<'workerInitRequest', 'worker
6363
export function logColdStartWarning(channel: WorkerChannel, delayInMs?: number): void {
6464
// On reading a js file with function code('require') NodeJs tries to find 'package.json' all the way up to the file system root.
6565
// In Azure files it causes a delay during cold start as connection to Azure Files is an expensive operation.
66-
if (
67-
process.env.WEBSITE_CONTENTAZUREFILECONNECTIONSTRING &&
68-
process.env.WEBSITE_CONTENTSHARE &&
69-
process.env.AzureWebJobsScriptRoot
70-
) {
66+
const scriptRoot = process.env.AzureWebJobsScriptRoot;
67+
if (process.env.WEBSITE_CONTENTAZUREFILECONNECTIONSTRING && process.env.WEBSITE_CONTENTSHARE && scriptRoot) {
7168
// Add delay to avoid affecting coldstart
7269
if (!delayInMs) {
7370
delayInMs = 5000;
7471
}
7572
setTimeout(() => {
76-
access(path.join(process.env.AzureWebJobsScriptRoot!, 'package.json'), constants.F_OK, (err) => {
73+
access(path.join(scriptRoot, 'package.json'), constants.F_OK, (err) => {
7774
if (isError(err)) {
7875
channel.log({
7976
message:

test/eventHandlers/FunctionEnvironmentReloadHandler.test.ts

-10
Original file line numberDiff line numberDiff line change
@@ -70,23 +70,13 @@ describe('FunctionEnvironmentReloadHandler', () => {
7070
let stream: TestEventStream;
7171
let channel: WorkerChannel;
7272

73-
// Reset `process.env` and process.cwd() after this test suite so it doesn't affect other tests
74-
let originalEnv: NodeJS.ProcessEnv;
75-
let originalCwd: string;
7673
before(() => {
77-
originalEnv = { ...process.env };
78-
originalCwd = process.cwd();
7974
({ stream, channel } = beforeEventHandlerSuite());
8075
channel.hostVersion = '2.7.0';
8176
});
8277

83-
after(() => {
84-
Object.assign(process.env, originalEnv);
85-
});
86-
8778
afterEach(async () => {
8879
mock.restore();
89-
process.chdir(originalCwd);
9080
await stream.afterEachEventHandlerTest();
9181
});
9282

test/eventHandlers/TestEventStream.ts

+13
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ import { AzureFunctionsRpcMessages as rpc } from '../../azure-functions-language
88
import { IEventStream } from '../../src/GrpcClient';
99

1010
export class TestEventStream extends EventEmitter implements IEventStream {
11+
originalEnv: NodeJS.ProcessEnv;
12+
originalCwd: string;
1113
written: sinon.SinonSpy;
1214
constructor() {
1315
super();
1416
this.written = sinon.spy();
17+
this.originalEnv = { ...process.env };
18+
this.originalCwd = process.cwd();
1519
}
1620
write(message: rpc.IStreamingMessage) {
1721
this.written(message);
@@ -69,6 +73,15 @@ export class TestEventStream extends EventEmitter implements IEventStream {
6973
* Verifies the test didn't send any extraneous messages
7074
*/
7175
async afterEachEventHandlerTest(): Promise<void> {
76+
// Reset `process.env` and process.cwd() after each test so it doesn't affect other tests
77+
process.chdir(this.originalCwd);
78+
for (const key of Object.keys(process.env)) {
79+
if (!(key in this.originalEnv)) {
80+
delete process.env[key];
81+
}
82+
}
83+
Object.assign(process.env, this.originalEnv);
84+
7285
// minor delay so that it's more likely extraneous messages are associated with this test as opposed to leaking into the next test
7386
await new Promise((resolve) => setTimeout(resolve, 20));
7487
await this.assertCalledWith();

test/startApp.test.ts

+1-10
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,18 @@ describe('startApp', () => {
4343
let stream: TestEventStream;
4444
let coreApi: typeof coreTypes;
4545
let testDisposables: coreTypes.Disposable[] = [];
46-
let originalEnv: NodeJS.ProcessEnv;
47-
let originalCwd: string;
4846

4947
before(async () => {
50-
originalCwd = process.cwd();
51-
originalEnv = { ...process.env };
5248
({ stream, channel } = beforeEventHandlerSuite());
5349
coreApi = await import('@azure/functions-core');
5450
});
5551

56-
after(() => {
57-
Object.assign(process.env, originalEnv);
58-
});
59-
6052
afterEach(async () => {
61-
await stream.afterEachEventHandlerTest();
6253
coreApi.Disposable.from(...testDisposables).dispose();
6354
testDisposables = [];
64-
process.chdir(originalCwd);
6555
channel.appHookData = {};
6656
channel.appLevelOnlyHookData = {};
57+
await stream.afterEachEventHandlerTest();
6758
});
6859

6960
it('runs app start hooks in non-specialization scenario', async () => {

0 commit comments

Comments
 (0)