Skip to content

Commit 0cebd1d

Browse files
committed
Add hasEnv delayed assertion and use for getJobUUID test
1 parent d2f5cbb commit 0cebd1d

2 files changed

Lines changed: 27 additions & 10 deletions

File tree

src/status-report.test.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,16 @@ import { BuildMode, ConfigurationError, withTmpDir, wrapError } from "./util";
2929
setupTests(test);
3030

3131
test("getJobUUID - generates valid UUIDs", async (t) => {
32-
const exportVariableStub: sinon.SinonStub<[string, string], void> =
33-
sinon.stub();
34-
3532
await callee(getJobUUID)
3633
.withArgs()
37-
.withActions((env) => {
38-
env.exportVariable = exportVariableStub;
39-
})
4034
.logs(t, "Job run UUID is ")
35+
.hasEnv(t, (val) => {
36+
return {
37+
[EnvVar.JOB_RUN_UUID]: val,
38+
};
39+
})
4140
.passes((val) => {
4241
t.true(uuid.validate(val));
43-
44-
const calls = exportVariableStub.getCalls();
45-
t.is(calls.length, 1);
46-
t.deepEqual(calls[0].args, [EnvVar.JOB_RUN_UUID, val]);
4742
});
4843
});
4944

src/testing-utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,28 @@ abstract class BaseEnvBuilder<
378378
return result;
379379
}
380380

381+
/**
382+
* Adds a delayed check that the environment variables returned by `fn`
383+
* are present in the environment after the main assertion passes.
384+
*/
385+
public hasEnv(
386+
t: ExecutionContext<unknown>,
387+
fn: (
388+
value: Awaited<R> | undefined,
389+
error: ThrownError<ErrorConstructor | Error> | undefined,
390+
) => Record<string, string | undefined>,
391+
): this {
392+
const result = this.clone();
393+
result.checks.push(async (env, r) => {
394+
const value = r.orElse(undefined);
395+
const error = r.isFailure() ? r.value : undefined;
396+
const expected = fn(value, error);
397+
398+
t.like(env.getState().env.get(), expected);
399+
});
400+
return result;
401+
}
402+
381403
/**
382404
* Adds a delayed check that `messages` are not logged. The check will be
383405
* performed after the main assertion passes.

0 commit comments

Comments
 (0)