Skip to content

Commit

Permalink
add beforeStart
Browse files Browse the repository at this point in the history
  • Loading branch information
menduz committed Feb 4, 2022
1 parent b2ef908 commit 3329b8d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
5 changes: 4 additions & 1 deletion etc/test-helpers.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { IFetchComponent } from '@well-known-components/http-server';
import { Lifecycle } from '@well-known-components/interfaces';
import { default as sinon_2 } from 'sinon';

// @public (undocumented)
export type BeforeStartFunction<TestComponents extends Record<string, any> = any> = () => Promise<void> | void;

// @public
export function createLocalFetchCompoment(configComponent: IConfigComponent): Promise<IFetchComponent>;

Expand All @@ -27,9 +30,9 @@ export type TestArguments<TestComponents extends Record<string, any>> = {
stubComponents: {
readonly [T in keyof TestComponents]: sinon_2.SinonStubbedInstance<TestComponents[T]>;
};
beforeStart(fn: BeforeStartFunction<TestComponents>): void;
};


// (No @packageDocumentation comment for this package)

```
16 changes: 16 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ export type TestArguments<TestComponents extends Record<string, any>> = {
stubComponents: {
readonly [T in keyof TestComponents]: sinon.SinonStubbedInstance<TestComponents[T]>
}

/**
* Functions to run before the Lifecycle helpers start the components' lifecycle
*/
beforeStart(fn: BeforeStartFunction<TestComponents>): void
}

export type BeforeStartFunction<TestComponents extends Record<string, any> = any> = () => Promise<void> | void

export { createLocalFetchCompoment, defaultServerConfig } from "./localFetch"

declare var before: typeof beforeAll
Expand Down Expand Up @@ -56,6 +63,8 @@ export function createRunner<TestComponents extends Record<string, any>>(
return stubComponentInstances.get(key)!
}

const beforeStartFunctions: BeforeStartFunction[] = []

const testArgs: TestArguments<TestComponents> = {
components: new Proxy(
{},
Expand All @@ -73,10 +82,15 @@ export function createRunner<TestComponents extends Record<string, any>>(
},
}
) as any,
beforeStart(fn) {
beforeStartFunctions.push(fn)
},
}

describe(name, () => {
_beforeAll(async () => {
jest.resetModules()
for (let fn of beforeStartFunctions) await fn()
program = await Lifecycle.run<TestComponents>(options)
})

Expand All @@ -86,10 +100,12 @@ export function createRunner<TestComponents extends Record<string, any>>(
// reset spy objects
sinon.reset()
sinon.resetBehavior()
jest.resetAllMocks()
})

afterEach(() => {
sandbox.restore()
jest.restoreAllMocks()
})

suite(testArgs)
Expand Down
38 changes: 38 additions & 0 deletions test/simple-smoke-test/beforeStart.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { createRunner } from "../../src"

type Components = {
config: {
readonly getValue: string
}
}

const AAA_VALUE = Math.random().toString()

const test = createRunner<Components>({
async main(program) {
await program.startComponents()
},
async initComponents() {
const config = {
start() {
expect(process.env.AAA).toEqual(AAA_VALUE)
},
get getValue() {
expect(process.env.AAA).toEqual(AAA_VALUE)
return process.env.AAA!
},
}

return { config }
},
})

test("beforeStart works", ({ beforeStart, components }) => {
beforeStart(async () => {
process.env.AAA = AAA_VALUE
})
it("works", () => {
expect(components.config.getValue).toEqual(AAA_VALUE)
delete process.env.AAA
})
})
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"declaration": true, /* Generates corresponding '.d.ts' file. */
"declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
"stripInternal": true,
"types": [
"node",
"jest"
Expand Down

0 comments on commit 3329b8d

Please sign in to comment.