@magnit-ce/code-tests 0.0.12 → 0.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +95 -64
- package/dist/code-tests.d.ts +172 -58
- package/dist/code-tests.js +1178 -1012
- package/dist/code-tests.min.js +1 -475
- package/dist/code-tests.umd.js +1434 -0
- package/dist/code-tests.umd.min.js +1 -0
- package/package.json +25 -24
- package/dist/code-tests.cjs +0 -1296
- package/dist/code-tests.d.cts +0 -58
package/README.md
CHANGED
|
@@ -1,89 +1,120 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
1
|
+
# The `code-tests` Custom Element
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
A custom HTML element that renders code in a syntax-highlight text block, and the results of that code in a display frame.
|
|
5
|
+
|
|
6
|
+
|||||
|
|
7
|
+
|-|-|-|-|
|
|
8
|
+
|Package size, **verbose**:|~36kb|Package size, **minified**:|~24kb|
|
|
3
9
|
|
|
4
|
-
Package size: ~24kb minified, ~36kb verbose.
|
|
5
10
|
|
|
6
|
-
|
|
11
|
+
|
|
12
|
+
## Quick Start
|
|
7
13
|
```html
|
|
8
14
|
<code-tests src="tests/library.tests.js" title="Library"></code-tests>
|
|
9
15
|
<script type="module" src="/path/to/code-tests[.min].js"></script>
|
|
10
16
|
```
|
|
11
17
|
|
|
12
|
-
##
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
-
|
|
17
|
-
|
|
18
|
-
-
|
|
19
|
-
- <s>Safari</s> (Has not been tested; should be supported, based on custom element support)
|
|
18
|
+
## Links
|
|
19
|
+
### Demo:
|
|
20
|
+
https://catapart.github.io/magnitce-code-tests
|
|
21
|
+
### Documentation:
|
|
22
|
+
https://catapart.github.io/magnitce-code-tests/documentation
|
|
23
|
+
### Automated Testing:
|
|
24
|
+
https://catapart.github.io/magnitce-code-tests/test-runner
|
|
20
25
|
|
|
21
|
-
##
|
|
22
|
-
|
|
26
|
+
## Repo Notes
|
|
27
|
+
This section provides instructions for how to use the repo to develop the library.
|
|
23
28
|
|
|
24
|
-
###
|
|
25
|
-
|
|
26
|
-
```html
|
|
27
|
-
<script type="module" src="/path/to/code-tests[.min].js"></script>
|
|
28
|
-
```
|
|
29
|
-
#### npm
|
|
29
|
+
### Installation
|
|
30
|
+
Install the repo using a package manager:
|
|
30
31
|
```cmd
|
|
31
|
-
npm install
|
|
32
|
+
npm install
|
|
32
33
|
```
|
|
33
34
|
|
|
34
|
-
###
|
|
35
|
-
####
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
### Configuration
|
|
36
|
+
#### `tsconfig.json`
|
|
37
|
+
The `tsconfig.json` file is generated by the `vite` dependency and has not been modified by this library. It uses strict linting and defaults to `es` modules, rather than `commonjs` modules. For more information about each of the `tsconfig.json` options, see the [tsconfig documentation](https://www.typescriptlang.org/tsconfig/).
|
|
38
|
+
#### `vite.config`
|
|
39
|
+
The `vite.config` file sets defaults options for the dev, build, and release processes, which allows the code to be packaged as a library (rather than a website) and defines custom inputs and outputs to exclude tests and create both `es` and `umd` modules. It also includes settings that allow for multiple file outputs with well-defined names and selective minification. For more information about configuring `vite`, see the [vite config documentation](https://vite.dev/config/).
|
|
40
|
+
#### `vite.config.tests.ts`
|
|
41
|
+
This specialized `vite.config` file defines custom inputs and outputs that include tests and then are packaged into the `test-runner` directory, rather than built to the `dist` directory.
|
|
38
42
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
43
|
+
### Scripts
|
|
44
|
+
#### `dev`
|
|
45
|
+
Use `vite` to begin a server with Hot Module Replacement, which will automatically reload any time a change in a source file is saved.
|
|
46
|
+
#### `build`
|
|
47
|
+
Use `vite` to transpile the source code into code chunks or library files. Files are output into the `dist` directory by default.
|
|
48
|
+
#### `preview`
|
|
49
|
+
Use `vite` to serve the `dist` directory. Useful for non-library projects, where the `dist` directory will contain an `index.html` file, or other browse-able entry point.
|
|
50
|
+
#### `test:build`
|
|
51
|
+
Use `vite` to transpile all files with a `.tests.[ts|js|tsx|jsx]` extension into the `test-runner` subdirectory. Creates javascript files that can be served to the `<test-runner>` and `<code-tests>` components.
|
|
52
|
+
#### `test`
|
|
53
|
+
Runs the `test:build` script and then serves the library website using `serve`, to prevent module resolution issues that occur when running the `dev` script.
|
|
54
|
+
#### `changeset`
|
|
55
|
+
Runs the `npx @changesets/cli` command to create a new changeset which will prepare the library to be published.
|
|
56
|
+
#### `release`
|
|
57
|
+
Use `vite` to transpile the source code and then use `changesets` to begin the publication procedure for the repo environment (github or gitlab).
|
|
58
|
+
If this script is run during CI/CD for the `main` branch, the library is published to [npm](https://www.npmjs.com/). If it is run during CI/CD for any other branch, the library is linted and compiled. It is expected that this command will *only* be used by a CI/CD pipeline, and **never** by a library dev. Running the `release` script on a local codebase is not supported.
|
|
44
59
|
|
|
45
|
-
import { CodeTestsElement, CodeTests, expect[, etc...] } from "@magnit-ce/code-tests";
|
|
46
|
-
```
|
|
47
60
|
|
|
48
|
-
|
|
49
|
-
---
|
|
50
|
-
---
|
|
61
|
+
### Publishing
|
|
51
62
|
|
|
52
|
-
|
|
53
|
-
The
|
|
63
|
+
#### Releasing with `changesets`
|
|
64
|
+
Publishing is handled using the `changesets` library which starts the process by requiring a manual entry into a running version log. Once you have defined a set of changes as a version change, the automated processes can prepare the library to be published on to `npm`. When the automation has prepared the release, it will create a pull request from a new branch containing the release package. The publishing developer will be required to merge that branch into main, using the pull request, in order to publish a new version.
|
|
54
65
|
|
|
55
|
-
|
|
56
|
-
beforeAll:
|
|
57
|
-
beforeEach:
|
|
58
|
-
afterEach:
|
|
59
|
-
afterAll:
|
|
66
|
+
The step-by-step process is outline below for both Github and Gitlab:
|
|
60
67
|
|
|
61
|
-
|
|
62
|
-
`
|
|
68
|
+
##### Github Steps
|
|
69
|
+
1. **Run `changeset` command**: `npm run changeset` | `bun run changeset` | `pnpm changesets` | `yarn changesets` | `npx @changesets/cli`
|
|
70
|
+
1. **Log version info**: Add a message that describes the changes in this version
|
|
71
|
+
1. **Check in**: Push changes to repo
|
|
72
|
+
1. **Merge**: Merge branch into `main`
|
|
73
|
+
1. **Merge Automation Branch**: Await repo automation's creation of a new pull request from a newly created branch. Accept the pull request, merging the branch into main.
|
|
63
74
|
|
|
64
|
-
|
|
65
|
-
The `<code-tests>` element dispatches the following events:
|
|
66
|
-
- `start`:
|
|
67
|
-
- `finish`:
|
|
75
|
+
After automation completes, the new version will be published to `NPM`.
|
|
68
76
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
77
|
+
##### Gitlab Steps
|
|
78
|
+
1. **Run `changeset` command**: `npm run changeset` | `bun run changeset` | `pnpm changesets` | `yarn changesets` | `npx @changesets/cli`
|
|
79
|
+
1. **Log version info**: Add a message that describes the changes in this version
|
|
80
|
+
1. **Check in**: Push changes to repo
|
|
81
|
+
1. **Merge**: Merge branch into `main`
|
|
82
|
+
1. **Merge Automation Branch**: Await repo automation's creation of a new merge request from a newly created branch. Merge the merge request into main.
|
|
72
83
|
|
|
73
|
-
|
|
84
|
+
After automation completes, the new version will be published to `NPM`.
|
|
74
85
|
|
|
75
|
-
|
|
76
|
-
|
|
86
|
+
### Dependencies
|
|
87
|
+
|
|
88
|
+
#### Library Dependencies
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
#### Development Dependencies
|
|
92
|
+
|
|
93
|
+
##### [`typescript`](https://www.typescriptlang.org/)
|
|
94
|
+
Typescript provides type checking at dev time which adds compile-time safety to the codebase for a wide range of issues. For more information on Typescript, visit its [website](https://www.typescriptlang.org/).
|
|
95
|
+
|
|
96
|
+
##### [`@changesets/cli`](https://github.com/changesets/changesets)
|
|
97
|
+
The `changesets` library uses a strict process to handle versioning and publishing which makes the update and deployment of any library to NPM a structured and repeatable series of steps. Versioning is managed by a command-line utility which enforces release notes and semantic versions. Publishing is handled automatically by CI/CD pipelines when code is checked in. The structure allows for automated pipelines to handle linting and testing, while requiring manual directives to invoke action. Versions are not accidentally published, because all code changes that haven't been packaged into a changeset are transparent to the library. As soon as a version is updated (by the library), the CI/CD pipelines will trigger and the library developer will need to merge branches in order to complete the publishing. By enforcing these procedures, `changesets` keeps this library releasing traceable and well-versioned packages.
|
|
98
|
+
|
|
99
|
+
##### [`vite`](https://vite.dev/)
|
|
100
|
+
Vite is a packaging library that includes a lot of features that make development faster and easier. Packaging and minifying are built in as well as "hot module reload" so that saving any source file will reload the script in the development environment instantly. That is made even better because Vite also dynamically swaps typescript references for javascript references, meaning that a development build can target and debug with the typescript files directly. It is also configurable enough to account for a wide variety of project types.
|
|
101
|
+
|
|
102
|
+
##### [`vite-plugin-dts`](https://www.npmjs.com/package/vite-plugin-dts)
|
|
103
|
+
This plugin for the `vite` library allows the packaging process to also produce a `.d.ts` file to describe all of the Typescript types exposed by the library. Without that export, other projects that use Typescript would not be able to see the full manifest of exported types.
|
|
104
|
+
|
|
105
|
+
##### [`@rollup/plugin-terser`](https://www.npmjs.com/package/@rollup/plugin-terser)
|
|
106
|
+
This plugin, which was developed for one of `vite`'s dependencies (`rollup`) but is used in this project by `vite`, provides a type of minification that is not supported by the default library. Using this plugin, the library can be packaged with both unminified and minified code, so that developers who implement the library can debug the code in un-minified files, while still being able to ship a minified version without having to minify it in their own projects.
|
|
107
|
+
|
|
108
|
+
##### [`@magnit-ce/test-runner`](https://github.com/catapart/magnitce-test-runner)
|
|
109
|
+
This library adds custom HTML elements that allow developers to run simple definitions for automated tests directly in a browser. These elements are used in the `test-runner` directory to provide live automated testing.
|
|
110
|
+
##### [`glob`](https://www.npmjs.com/package/glob)
|
|
111
|
+
`glob` is a low-level library that provides full-featured search capabilities to a command-line input. These search features are used by this library to differentiate between test files and source files within the `vite.config` files.
|
|
112
|
+
|
|
113
|
+
##### [`@web/dev-server`](https://modern-web.dev/docs/dev-server/overview/)
|
|
114
|
+
This library provides a local server to serve the project directory without module resolution conflicts between `vite` and the `<test-runner>` element. This library is not necessary if `vite` can be configured to stop wrapping import links for specific run modes (like "testing" vs "development"). At this point, no such configuration has been included in this project so the `web/dev-server` library is used as a work-around for that deficiency.
|
|
77
115
|
|
|
78
|
-
In this example, the `header` part is being selected for styling:
|
|
79
|
-
```css
|
|
80
|
-
code-tests::part(header)
|
|
81
|
-
{
|
|
82
|
-
/* styling */
|
|
83
|
-
}
|
|
84
|
-
```
|
|
85
116
|
|
|
86
|
-
|
|
117
|
+
# Warning - Non-production package
|
|
118
|
+
This project is being prepared for production, but is not ready to be used as a dependency for anything. There will be breaking changes and unrecoverable states. Do not use until this warning has been removed.
|
|
87
119
|
|
|
88
|
-
|
|
89
|
-
This library is in the public domain. You do not need permission, nor do you need to provide attribution, in order to use, modify, reproduce, publish, or sell it or any works using it or derived from it.
|
|
120
|
+
When the library reaches the `1.0.0` designation, that will be a production package. From that point, you can use the major version number (`1` in `1.0.0`) to recognize when breaking changes are introduced.
|
package/dist/code-tests.d.ts
CHANGED
|
@@ -1,58 +1,172 @@
|
|
|
1
|
-
declare class
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
declare
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
cancel(): void;
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
1
|
+
export declare class CodeTestElement extends HTMLElement {
|
|
2
|
+
#private;
|
|
3
|
+
state: CodeTestState;
|
|
4
|
+
setState(state: CodeTestState): void;
|
|
5
|
+
setStateProperties(state: Partial<CodeTestState>): void;
|
|
6
|
+
setTestStateProperties(key: keyof CodeTestState, state: Partial<TestState>): void;
|
|
7
|
+
findElement<T extends HTMLElement = HTMLElement>(query: string): T;
|
|
8
|
+
findElements<T extends HTMLElement = HTMLElement>(query: string): T[];
|
|
9
|
+
isRunning(): boolean;
|
|
10
|
+
hasRun(): boolean;
|
|
11
|
+
resultCategory(): TestResultCategory | undefined;
|
|
12
|
+
connectedCallback(): void;
|
|
13
|
+
enable(): void;
|
|
14
|
+
disable(): void;
|
|
15
|
+
runTest(contextManager: ContextManager, testContext: TestContext): Promise<void>;
|
|
16
|
+
reset(): void;
|
|
17
|
+
getMessageElement(): HTMLElement;
|
|
18
|
+
static observedAttributes: string[];
|
|
19
|
+
attributeChangedCallback(attributeName: string, _oldValue: string, newValue: string): void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export declare const CodeTestEvent: {
|
|
23
|
+
BeforeAll: string;
|
|
24
|
+
AfterAll: string;
|
|
25
|
+
BeforeTest: string;
|
|
26
|
+
AfterTest: string;
|
|
27
|
+
BeforeHook: string;
|
|
28
|
+
AfterHook: string;
|
|
29
|
+
Cancel: string;
|
|
30
|
+
Context: string;
|
|
31
|
+
Reset: string;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export declare class CodeTestsElement extends HTMLElement {
|
|
35
|
+
#private;
|
|
36
|
+
state: CodeTestsState;
|
|
37
|
+
setState(state: CodeTestsState): void;
|
|
38
|
+
setStateProperties(state: Partial<CodeTestsState>): void;
|
|
39
|
+
setTestStateProperties(key: keyof CodeTestsState, state: Partial<TestState>): void;
|
|
40
|
+
findElement<T extends HTMLElement = HTMLElement>(query: string): T;
|
|
41
|
+
findElements<T extends HTMLElement = HTMLElement>(query: string): Array<T>;
|
|
42
|
+
getIsRunning(): boolean;
|
|
43
|
+
getResultCategory(): "none" | "success" | "fail";
|
|
44
|
+
constructor();
|
|
45
|
+
connectedCallback(): void;
|
|
46
|
+
disconnectedCallback(): void;
|
|
47
|
+
collectTestResults(): GroupTestResults;
|
|
48
|
+
runTests(): Promise<void>;
|
|
49
|
+
reloadTests(): Promise<void>;
|
|
50
|
+
reset(): Promise<void>;
|
|
51
|
+
cancel(): void;
|
|
52
|
+
static observedAttributes: string[];
|
|
53
|
+
attributeChangedCallback(attributeName: string, _oldValue: string, newValue: string): void;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export declare type CodeTestsState = {
|
|
57
|
+
isCanceled: boolean;
|
|
58
|
+
beforeAllState?: TestState;
|
|
59
|
+
afterAllState?: TestState;
|
|
60
|
+
beforeEachState?: TestState;
|
|
61
|
+
afterEachState?: TestState;
|
|
62
|
+
requiredBeforeAnyState?: TestState;
|
|
63
|
+
requiredAfterAnyState?: TestState;
|
|
64
|
+
resetHook?: Test;
|
|
65
|
+
contextHook?: Test;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
declare type CodeTestState = {
|
|
69
|
+
testId: string;
|
|
70
|
+
description: string;
|
|
71
|
+
isDisabled: boolean;
|
|
72
|
+
testState?: TestState;
|
|
73
|
+
beforeEachState?: TestResultState;
|
|
74
|
+
afterEachState?: TestResultState;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
declare class ContextManager {
|
|
78
|
+
#private;
|
|
79
|
+
codeTestsElement: CodeTestsElement;
|
|
80
|
+
constructor(parent: CodeTestsElement);
|
|
81
|
+
loadTests(path?: string): Promise<void>;
|
|
82
|
+
shouldContinueRunningTests: boolean;
|
|
83
|
+
runTests(tests: CodeTestElement[]): Promise<void>;
|
|
84
|
+
runTest(test: CodeTestElement | undefined, inLoop: boolean, testContext?: TestContext): Promise<void>;
|
|
85
|
+
runHook(testStateName: keyof Pick<CodeTestsState, 'requiredBeforeAnyState' | 'requiredAfterAnyState' | 'beforeEachState' | 'afterEachState' | 'beforeAllState' | 'afterAllState'>, test: CodeTestElement | undefined, testContext: TestContext): Promise<string | void | HTMLElement | TestResult | typeof NOTESTDEFINED>;
|
|
86
|
+
parseTestResult(result: TestResultType | typeof NOTESTDEFINED, finishedTest: boolean, error?: Error): {
|
|
87
|
+
result: string | HTMLElement;
|
|
88
|
+
resultCategory: TestResultCategory;
|
|
89
|
+
};
|
|
90
|
+
createTestContext(testElement?: CodeTestElement): Promise<TestContext>;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export declare function createElementFromTemplate(target: string | HTMLTemplateElement, parent?: HTMLElement): HTMLElement;
|
|
94
|
+
|
|
95
|
+
export declare function expect<T>(value: T): TestPromise<T>;
|
|
96
|
+
|
|
97
|
+
declare type GroupTestResults = {
|
|
98
|
+
totalTests: number;
|
|
99
|
+
totalPassed: number;
|
|
100
|
+
totalPercentage: number;
|
|
101
|
+
duration: number;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
export declare const Hook: {
|
|
105
|
+
readonly BeforeAll: "beforeall";
|
|
106
|
+
readonly AfterAll: "afterall";
|
|
107
|
+
readonly BeforeEach: "beforeeach";
|
|
108
|
+
readonly AfterEach: "aftereach";
|
|
109
|
+
readonly RequiredBeforeAny: "requiredbeforeany";
|
|
110
|
+
readonly RequiredAfterAny: "requiredafterany";
|
|
111
|
+
readonly Reset: "reset";
|
|
112
|
+
readonly Context: "context";
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
export declare type HookType = typeof Hook[keyof typeof Hook];
|
|
116
|
+
|
|
117
|
+
declare const NOTESTDEFINED: unique symbol;
|
|
118
|
+
|
|
119
|
+
declare function prompt_2(host: CodeTestElement, parentElement: HTMLElement, message: string, options?: PromptOptions): Promise<boolean>;
|
|
120
|
+
export { prompt_2 as prompt }
|
|
121
|
+
|
|
122
|
+
export declare type PromptOptions = {
|
|
123
|
+
template?: HTMLTemplateElement;
|
|
124
|
+
acceptLabel?: string;
|
|
125
|
+
rejectLabel?: string;
|
|
126
|
+
onAccept?: () => void;
|
|
127
|
+
onReject?: () => void;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
declare type Test = <T extends TestResultType>(context: TestContext) => T | Promise<T>;
|
|
131
|
+
|
|
132
|
+
declare type TestContext = {
|
|
133
|
+
codeTestsElement: CodeTestsElement;
|
|
134
|
+
testElement?: CodeTestElement;
|
|
135
|
+
messageElement?: HTMLElement;
|
|
136
|
+
detail: any;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
export declare class TestPromise<T> extends Promise<T> {
|
|
140
|
+
toBeDefined(valueName?: string): Promise<void>;
|
|
141
|
+
toBe(value: any, exact?: boolean): Promise<void>;
|
|
142
|
+
toContainText(value: string): Promise<false | {
|
|
143
|
+
success: boolean;
|
|
144
|
+
index: number;
|
|
145
|
+
}>;
|
|
146
|
+
toHaveAttribute(value: string): Promise<void>;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
declare type TestResult = {
|
|
150
|
+
success: boolean;
|
|
151
|
+
expected?: any;
|
|
152
|
+
value: string | HTMLElement;
|
|
153
|
+
data?: any;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
declare type TestResultCategory = 'none' | 'success' | 'fail';
|
|
157
|
+
|
|
158
|
+
declare type TestResultState = {
|
|
159
|
+
hasRun: boolean;
|
|
160
|
+
isRunning: boolean;
|
|
161
|
+
resultCategory: TestResultCategory;
|
|
162
|
+
resultContent: string | boolean | number | HTMLElement;
|
|
163
|
+
duration: number;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
declare type TestResultType = void | undefined | string | TestResult | HTMLElement;
|
|
167
|
+
|
|
168
|
+
declare type TestState = TestResultState & {
|
|
169
|
+
test: Test;
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
export { }
|