-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: misc improvements in tests (#60)
* chore: add fast-check dependency * refactor: type add-cmd result Add type information indicating that result code will always be either 0 or 1 * refactor: adjust typings Use information found in test usage to adjust types throughout the project. Also type constants in tests * refactor: add mock pkg registry for tests Add utility functions to de-duplicate the common logic of adding packages and search results to the mock package registry in tests * refactor: reuse constant * refactor: split file Split utilities file into mock-work-dir and mock-console * refactor: add type-alias Hide implementation detail of what mock console is used from tests * refactor: deduplicate test code Add helpers to check for correct manifest in add cmd tests * refactor: deduplicate logic in tests Move logic for, creating, resetting and querying mock console content into dedicated functions * deps: bump packages To avoid warnings. Only dev dependencies * refactor: remove unnecessary await * refactor: extract helpers Move manifest assertion helpers to own module so they can be reused * deps: remove stub type package * refactor: manifest assertion prepare for allowing to chain multiple assertions on same manifest * refactor: deduplicate code Use manifest assertions in cmd remove tests. Also add a few new ones * refactor: deduplicate code Use manifest assertions in manifest tests * refactor: use better type for pkg-info name There should never be a version inside the name * deps: remove fast check * refactor: optimize imports * refactor: remove duplicate code * refactor: simplify defined assertions
- Loading branch information
1 parent
5d9a0d5
commit 3abf506
Showing
22 changed files
with
7,661 additions
and
719 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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,41 @@ | ||
import { PkgManifest, PkgName, PkgVersion } from "../src/types/global"; | ||
import { loadManifest } from "../src/utils/manifest"; | ||
import should from "should"; | ||
|
||
export function shouldHaveManifest(): PkgManifest { | ||
const manifest = loadManifest(); | ||
should(manifest).not.be.null(); | ||
return manifest!; | ||
} | ||
|
||
export function shouldHaveNoManifest() { | ||
const manifest = loadManifest(); | ||
should(manifest).be.null(); | ||
} | ||
|
||
export function shouldHaveDependency( | ||
manifest: PkgManifest, | ||
name: PkgName, | ||
version: PkgVersion | ||
) { | ||
should(manifest.dependencies[name]).equal(version); | ||
} | ||
|
||
export function shouldNotHaveAnyDependencies(manifest: PkgManifest) { | ||
should(manifest.dependencies).be.empty(); | ||
} | ||
export function shouldNotHaveDependency(manifest: PkgManifest, name: PkgName) { | ||
should(manifest.dependencies[name]).be.undefined(); | ||
} | ||
|
||
export function shouldHaveRegistryWithScopes( | ||
manifest: PkgManifest, | ||
scopes: PkgName[] | ||
) { | ||
should(manifest.scopedRegistries).not.be.undefined(); | ||
manifest | ||
.scopedRegistries!.some((registry) => | ||
scopes.every((scope) => registry.scopes.includes(scope)) | ||
) | ||
.should.be.true("At least one scope was missing"); | ||
} |
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,23 @@ | ||
import testConsole from "test-console"; | ||
|
||
type Stream = "out" | "error"; | ||
|
||
export type MockConsole = { | ||
hasLineIncluding(stream: Stream, text: string): boolean; | ||
detach(): void; | ||
}; | ||
|
||
export function attachMockConsole(): MockConsole { | ||
const out = testConsole.stdout.inspect(); | ||
const error = testConsole.stderr.inspect(); | ||
return { | ||
hasLineIncluding(stream: Stream, text: string): boolean { | ||
const inspector = stream === "out" ? out : error; | ||
return inspector.output.some((line) => line.includes(text)); | ||
}, | ||
detach() { | ||
out.restore(); | ||
error.restore(); | ||
}, | ||
}; | ||
} |
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,45 @@ | ||
import { PkgInfo, PkgName } from "../src/types/global"; | ||
import nock from "nock"; | ||
import { SearchEndpointResult } from "./types"; | ||
|
||
export const unityRegistryUrl = "https://packages.unity.com"; | ||
export const exampleRegistryUrl = "http://example.com"; | ||
|
||
export function startMockRegistry() { | ||
if (!nock.isActive()) nock.activate(); | ||
} | ||
|
||
export function registerRemotePkg(pkg: PkgInfo) { | ||
nock(exampleRegistryUrl) | ||
.persist() | ||
.get(`/${pkg.name}`) | ||
.reply(200, pkg, { "Content-Type": "application/json" }); | ||
} | ||
|
||
export function registerRemoteUpstreamPkg(pkg: PkgInfo) { | ||
nock(unityRegistryUrl).persist().get(`/${pkg.name}`).reply(200, pkg, { | ||
"Content-Type": "application/json", | ||
}); | ||
nock(exampleRegistryUrl).persist().get(`/${pkg.name}`).reply(404); | ||
} | ||
|
||
export function registerMissingPackage(name: PkgName) { | ||
nock(exampleRegistryUrl).persist().get(`/${name}`).reply(404); | ||
nock(unityRegistryUrl).persist().get(`/${name}`).reply(404); | ||
} | ||
|
||
export function registerSearchResult( | ||
searchText: string, | ||
result: SearchEndpointResult | ||
) { | ||
nock(exampleRegistryUrl) | ||
.get(new RegExp(`-\\/v1\\/search\\?text=${searchText}`)) | ||
.reply(200, result, { | ||
"Content-Type": "application/json", | ||
}); | ||
} | ||
|
||
export function stopMockRegistry() { | ||
nock.restore(); | ||
nock.cleanAll(); | ||
} |
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
Oops, something went wrong.