-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lexus Drumgold <[email protected]>
- Loading branch information
1 parent
98710bf
commit 11ef39b
Showing
18 changed files
with
439 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* @file Test Setup Files - Environment | ||
* @module tests/setup/env | ||
*/ | ||
|
||
import INPUT_API from '#fixtures/input-api.fixture' | ||
import INPUT_TOKEN from '#fixtures/input-token.fixture' | ||
import OWNER from '#fixtures/owner.fixture' | ||
import REPO from '#fixtures/repo.fixture' | ||
import pathe from '@flex-development/pathe' | ||
import { join, noop, type EmptyArray, type Fn } from '@flex-development/tutils' | ||
|
||
/** | ||
* Stub environment variables. | ||
* | ||
* @param {Fn<EmptyArray, void>?} [fn=noop] - Stub callback to execute | ||
* @return {void} Nothing when complete | ||
*/ | ||
const env = (fn: Fn<EmptyArray, void> = noop): void => { | ||
vi.stubEnv('GITHUB_REPOSITORY', join([OWNER, REPO], pathe.sep)) | ||
|
||
vi.stubEnv('INPUT_API', INPUT_API) | ||
vi.stubEnv('INPUT_TOKEN', INPUT_TOKEN) | ||
vi.stubEnv('INPUT_WORKSPACE', process.cwd()) | ||
|
||
return void fn() | ||
} | ||
|
||
export default env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/subdomains/config/__tests__/config.module.functional.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* @file Functional Tests - ConfigModule | ||
* @module repostructure/config/tests/functional/ConfigModule | ||
*/ | ||
|
||
import * as mlly from '@flex-development/mlly' | ||
import pathe from '@flex-development/pathe' | ||
import type { Dot, EmptyString, Join } from '@flex-development/tutils' | ||
import json5 from 'json5' | ||
import * as yaml from 'yaml' | ||
import TestSubject from '../config.module' | ||
|
||
vi.mock('@flex-development/mlly', () => ({ getSource: vi.fn() })) | ||
vi.mock('yaml', async og => ({ parse: vi.fn((await og<typeof yaml>()).parse) })) | ||
|
||
describe('functional:config/ConfigModule', () => { | ||
describe('.infrastructure', () => { | ||
let workspace: string | ||
|
||
beforeAll(() => { | ||
workspace = import.meta.env.INPUT_WORKSPACE | ||
}) | ||
|
||
describe.each<Join<[Dot, 'json' | 'json5' | 'jsonc'], EmptyString>>([ | ||
'.json', | ||
'.json5', | ||
'.jsonc' | ||
])('%s', ext => { | ||
let file: string | ||
let source: string | ||
|
||
beforeAll(() => { | ||
file = pathe.join('.github', 'infrastructure' + ext) | ||
source = '{}' | ||
}) | ||
|
||
beforeEach(() => { | ||
vi.spyOn(mlly, 'getSource').mockImplementationOnce(async () => source) | ||
vi.spyOn(json5, 'parse') | ||
}) | ||
|
||
it(`should parse file with ${ext} extension`, async () => { | ||
// Act | ||
await TestSubject.infrastructure(file, workspace) | ||
|
||
// Expect | ||
expect(vi.mocked(json5.parse)).toHaveBeenCalledOnce() | ||
expect(vi.mocked(json5.parse)).toHaveBeenCalledWith(source) | ||
}) | ||
}) | ||
|
||
describe.each<Join<[Dot, 'yaml' | 'yml'], EmptyString>>([ | ||
'.yaml', | ||
'.yml' | ||
])('%s', ext => { | ||
let file: string | ||
let source: string | ||
|
||
beforeAll(() => { | ||
file = pathe.join('.github', 'infrastructure' + ext) | ||
source = '' | ||
}) | ||
|
||
beforeEach(() => { | ||
vi.spyOn(mlly, 'getSource').mockImplementationOnce(async () => source) | ||
vi.spyOn(yaml, 'parse') | ||
}) | ||
|
||
it(`should parse file with ${ext} extension`, async () => { | ||
// Act | ||
await TestSubject.infrastructure(file, workspace) | ||
|
||
// Expect | ||
expect(vi.mocked(yaml.parse)).toHaveBeenCalledOnce() | ||
expect(vi.mocked(yaml.parse)).toHaveBeenCalledWith(source, { | ||
logLevel: 'error', | ||
prettyErrors: true, | ||
schema: 'core', | ||
sortMapEntries: true, | ||
version: 'next' | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* @file Unit Tests - ConfigModule | ||
* @module repostructure/config/tests/unit/ConfigModule | ||
*/ | ||
|
||
import CLIENT_MUTATION_ID from '#fixtures/client-mutation-id.fixture' | ||
import OWNER from '#fixtures/owner.fixture' | ||
import REPO from '#fixtures/repo.fixture' | ||
import env from '#tests/setup/env' | ||
import type { NodeError } from '@flex-development/errnode' | ||
import pathe from '@flex-development/pathe' | ||
import type { Type } from '@nestjs/common' | ||
import { ConfigService } from '@nestjs/config' | ||
import { Test, TestingModuleBuilder } from '@nestjs/testing' | ||
import { fileURLToPath } from 'node:url' | ||
import TestSubject from '../config.module' | ||
|
||
describe('unit:config/ConfigModule', () => { | ||
beforeEach(() => { | ||
env((): void => { | ||
return void vi.stubEnv('INPUT_CONFIG', '.github/infrastructure.yml') | ||
}) | ||
}) | ||
|
||
it('should export ConfigService', async () => { | ||
// Arrange | ||
const imports: Type[] = [TestSubject] | ||
const ref: TestingModuleBuilder = Test.createTestingModule({ imports }) | ||
|
||
// Act | ||
const result = (await ref.compile()).get(ConfigService) | ||
|
||
// Expect | ||
expect(result).to.be.instanceof(ConfigService) | ||
}) | ||
|
||
describe('.infrastructure', () => { | ||
it('should throw if file extension is invalid', async () => { | ||
// Arrange | ||
const workspace: string = import.meta.env.INPUT_WORKSPACE | ||
const path: string = fileURLToPath(import.meta.url) | ||
const file: string = pathe.relative(workspace, path) | ||
let error!: NodeError | ||
|
||
// Act | ||
try { | ||
await TestSubject.infrastructure(file, workspace) | ||
} catch (e: unknown) { | ||
error = <typeof error>e | ||
} | ||
|
||
// Expect | ||
expect(error).to.have.property('code', 'ERR_UNKNOWN_FILE_EXTENSION') | ||
}) | ||
}) | ||
|
||
describe('.load', () => { | ||
it('should return configuration object', async () => { | ||
// Act | ||
const result = await TestSubject.load() | ||
|
||
// Expect | ||
expect(result).toMatchObject({ | ||
api: import.meta.env.INPUT_API, | ||
id: CLIENT_MUTATION_ID, | ||
infrastructure: expect.any(Object), | ||
owner: OWNER, | ||
repo: REPO, | ||
token: import.meta.env.INPUT_TOKEN | ||
}) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.