@guardian/end-to-end-testing 0.0.0-stage → 0.1.0
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 +167 -2
- package/dist/ApplicationContainer.d.ts +8 -0
- package/dist/ApplicationContainer.js +17 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -0
- package/package.json +36 -4
package/README.md
CHANGED
|
@@ -1,3 +1,168 @@
|
|
|
1
|
-
#
|
|
1
|
+
# end-to-end-testing
|
|
2
|
+
Provide reusable code and docker images to help set up a local stack for end-to-end testing
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
## Application Image
|
|
5
|
+
|
|
6
|
+
[container-images/application.Dockerfile](container-images/application.Dockerfile)
|
|
7
|
+
is a base image for running an application under test in an end-to-end stack:
|
|
8
|
+
Ubuntu with build essentials and [mise](https://mise.jdx.dev/) for installing
|
|
9
|
+
the application's toolchain. It contains no application code.
|
|
10
|
+
|
|
11
|
+
CI tests the TypeScript package and builds the image for `linux/amd64` and
|
|
12
|
+
`linux/arm64`. Pull requests build without publishing. Pushes to `main`, version
|
|
13
|
+
tags, and manual runs publish to:
|
|
14
|
+
|
|
15
|
+
```text
|
|
16
|
+
ghcr.io/guardian/end-to-end-testing/application:sha-<full-commit>
|
|
17
|
+
ghcr.io/guardian/end-to-end-testing/application:latest
|
|
18
|
+
ghcr.io/guardian/end-to-end-testing/application:v0.1.0
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
`latest` is only produced from the default branch; version tags are produced
|
|
22
|
+
only for corresponding Git tags. Pin a SHA tag or digest in consuming projects.
|
|
23
|
+
|
|
24
|
+
Publishing uses `GITHUB_TOKEN` with `packages: write`. Enable Actions and package
|
|
25
|
+
creation for the repository. GHCR packages are private by default on first
|
|
26
|
+
publication.
|
|
27
|
+
|
|
28
|
+
### Sharing Within the Organisation
|
|
29
|
+
|
|
30
|
+
After the first publish, open the package under the `guardian` organisation
|
|
31
|
+
on GitHub, then open **Package settings**. Under **Manage Actions access**, add
|
|
32
|
+
each consuming repository with the **Read** role. Being in the same organisation
|
|
33
|
+
does not automatically give another repository's `GITHUB_TOKEN` access.
|
|
34
|
+
|
|
35
|
+
In each consuming repository's workflow, grant `packages: read` and log in before
|
|
36
|
+
Docker or Testcontainers pulls the image:
|
|
37
|
+
|
|
38
|
+
```yaml
|
|
39
|
+
permissions:
|
|
40
|
+
contents: read
|
|
41
|
+
packages: read
|
|
42
|
+
|
|
43
|
+
jobs:
|
|
44
|
+
e2e:
|
|
45
|
+
runs-on: ubuntu-latest
|
|
46
|
+
steps:
|
|
47
|
+
- uses: actions/checkout@v4
|
|
48
|
+
- uses: docker/login-action@v3
|
|
49
|
+
with:
|
|
50
|
+
registry: ghcr.io
|
|
51
|
+
username: ${{ github.actor }}
|
|
52
|
+
password: ${{ secrets.GITHUB_TOKEN }}
|
|
53
|
+
- run: docker pull ghcr.io/guardian/end-to-end-testing/application:latest
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Replace `latest` with a published SHA tag or digest for reproducible tests, then
|
|
57
|
+
run the repository's e2e suite after login. No shared personal access token is
|
|
58
|
+
needed for these Actions workflows.
|
|
59
|
+
|
|
60
|
+
For local development, users need read access to the package and can authenticate
|
|
61
|
+
with `docker login ghcr.io` using a personal access token (classic) with
|
|
62
|
+
`read:packages`, authorised for organisation SSO when required. Alternatively,
|
|
63
|
+
make the package public to allow unauthenticated pulls; this also makes it
|
|
64
|
+
available outside the organisation.
|
|
65
|
+
|
|
66
|
+
### Local Image Build
|
|
67
|
+
|
|
68
|
+
From the repository root, with Docker BuildKit available:
|
|
69
|
+
|
|
70
|
+
```sh
|
|
71
|
+
docker build -f container-images/application.Dockerfile -t end-to-end-testing/application:local container-images
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## TypeScript Package
|
|
75
|
+
|
|
76
|
+
Requires Node.js 22.22 or newer. From the repository root:
|
|
77
|
+
|
|
78
|
+
```sh
|
|
79
|
+
npm ci
|
|
80
|
+
npm test
|
|
81
|
+
npm pack
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
The tarball contains JavaScript and TypeScript declarations. Install that
|
|
85
|
+
tarball in a consuming project alongside `testcontainers@^12.2.1`.
|
|
86
|
+
|
|
87
|
+
### Publishing to npm
|
|
88
|
+
|
|
89
|
+
The package workflow builds, tests and checks the package contents on pull
|
|
90
|
+
requests and pushes to `main`. After those checks pass on `main`, Changesets
|
|
91
|
+
opens or updates a release pull request with version and changelog updates.
|
|
92
|
+
Merging that pull request publishes the new version to the public npm registry.
|
|
93
|
+
If the current version has never been published, a run on `main` with no pending
|
|
94
|
+
changesets can publish it directly.
|
|
95
|
+
|
|
96
|
+
For each package change, run `npm run changeset`, choose the version bump, and
|
|
97
|
+
commit the generated file alongside the change. `npm run release` builds and
|
|
98
|
+
publishes unpublished versions; it is normally run by CI.
|
|
99
|
+
|
|
100
|
+
### Snapshot Releases
|
|
101
|
+
|
|
102
|
+
Run `npm run changeset`, then commit and push the generated changeset with your
|
|
103
|
+
code. The snapshot job fails if no changeset file is present.
|
|
104
|
+
|
|
105
|
+
In GitHub Actions, select **Build, test and publish npm package**, choose **Run
|
|
106
|
+
workflow**, and select a branch other than `main`. The snapshot job only runs
|
|
107
|
+
for manual triggers on non-`main` branches, after the test job passes. Pull
|
|
108
|
+
requests run the normal CI tests without publishing. Manual runs on `main` do
|
|
109
|
+
not publish. The workflow must exist on the default branch before GitHub shows
|
|
110
|
+
the **Run workflow** button.
|
|
111
|
+
|
|
112
|
+
Configure an npm trusted publisher for `guardian/end-to-end-testing` with the
|
|
113
|
+
workflow filename `ci.yml` and permission to publish directly with `npm publish`.
|
|
114
|
+
This is shared with normal releases; no npm token secret is required.
|
|
115
|
+
|
|
116
|
+
The workflow uses the committed changeset to version the package with a unique
|
|
117
|
+
snapshot label containing the run ID and attempt, then builds and tests it.
|
|
118
|
+
Manual runs publish under the `snapshot-<run-id>-<run-attempt>` npm tag. It does
|
|
119
|
+
not commit version changes, create Git tags, or update npm's `latest` tag. The
|
|
120
|
+
committed changeset remains available for the normal release. For example, run
|
|
121
|
+
`123456789`, attempt `1`:
|
|
122
|
+
|
|
123
|
+
```sh
|
|
124
|
+
npm install @guardian/end-to-end-testing@snapshot-123456789-1
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
The workflow summary includes an install command using that run's tag. Each run
|
|
128
|
+
and retry uses a different tag, so later snapshots do not move earlier tags.
|
|
129
|
+
|
|
130
|
+
### Usage
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
import { ApplicationContainer } from "end-to-end-testing";
|
|
134
|
+
import { Network } from "testcontainers";
|
|
135
|
+
|
|
136
|
+
const network = await new Network().start();
|
|
137
|
+
try {
|
|
138
|
+
const app = await new ApplicationContainer(
|
|
139
|
+
"ghcr.io/guardian/end-to-end-testing/application:sha-<full-commit>",
|
|
140
|
+
{ port: 9000, healthCheckPath: "/healthcheck" },
|
|
141
|
+
)
|
|
142
|
+
.withNetwork(network)
|
|
143
|
+
.withNetworkAliases("app")
|
|
144
|
+
.withBindMounts([{ source: "/path/to/app", target: "/app" }])
|
|
145
|
+
.withWorkingDir("/app")
|
|
146
|
+
.withCommand(["/app/start.sh"])
|
|
147
|
+
.start();
|
|
148
|
+
|
|
149
|
+
try {
|
|
150
|
+
console.log(`http://${app.getHost()}:${app.getMappedPort(9000)}`);
|
|
151
|
+
} finally {
|
|
152
|
+
await app.stop();
|
|
153
|
+
}
|
|
154
|
+
} finally {
|
|
155
|
+
await network.stop();
|
|
156
|
+
}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
The image has no application or start command, so the caller supplies them,
|
|
160
|
+
along with any backing services the application needs on the network.
|
|
161
|
+
|
|
162
|
+
`ApplicationContainer` extends `GenericContainer`, so network, environment,
|
|
163
|
+
bind mounts, log consumers, and other Testcontainers options remain available.
|
|
164
|
+
It pulls the specified image rather than building it, exposes the given port,
|
|
165
|
+
and waits up to ten minutes for HTTP 200 from the given health-check path.
|
|
166
|
+
|
|
167
|
+
Unit tests exercise configuration without Docker. They do not verify a running
|
|
168
|
+
stack.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { GenericContainer } from "testcontainers";
|
|
2
|
+
export type ApplicationContainerOptions = {
|
|
3
|
+
port: number;
|
|
4
|
+
healthCheckPath: string;
|
|
5
|
+
};
|
|
6
|
+
export declare class ApplicationContainer extends GenericContainer {
|
|
7
|
+
constructor(image: string, { port, healthCheckPath }: ApplicationContainerOptions);
|
|
8
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ApplicationContainer = void 0;
|
|
4
|
+
const testcontainers_1 = require("testcontainers");
|
|
5
|
+
class ApplicationContainer extends testcontainers_1.GenericContainer {
|
|
6
|
+
constructor(image, { port, healthCheckPath }) {
|
|
7
|
+
if (!image.trim()) {
|
|
8
|
+
throw new Error("A published application image reference is required");
|
|
9
|
+
}
|
|
10
|
+
super(image);
|
|
11
|
+
this.withExposedPorts(port);
|
|
12
|
+
// Allows for compiling the application on first start.
|
|
13
|
+
this.withStartupTimeout(10 * 60 * 1000);
|
|
14
|
+
this.withWaitStrategy(testcontainers_1.Wait.forHttp(healthCheckPath, port).forStatusCode(200));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.ApplicationContainer = ApplicationContainer;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ApplicationContainer = void 0;
|
|
4
|
+
var ApplicationContainer_1 = require("./ApplicationContainer");
|
|
5
|
+
Object.defineProperty(exports, "ApplicationContainer", { enumerable: true, get: function () { return ApplicationContainer_1.ApplicationContainer; } });
|
package/package.json
CHANGED
|
@@ -1,6 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@guardian/end-to-end-testing",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Shared Docker images and Testcontainers building blocks for end-to-end stacks.",
|
|
5
|
+
"type": "commonjs",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"repository": {
|
|
9
|
+
"url": "https://github.com/guardian/end-to-end-testing"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=22.22"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsc -p tsconfig.json",
|
|
25
|
+
"test": "npm run build && node --test test/*.test.cjs",
|
|
26
|
+
"prepack": "npm run build",
|
|
27
|
+
"changeset": "npx changeset"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"testcontainers": "^12.2.1"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@changesets/cli": "^3.0.3",
|
|
34
|
+
"@types/node": "^22.0.0",
|
|
35
|
+
"testcontainers": "^12.2.1",
|
|
36
|
+
"typescript": "~5.9.3"
|
|
37
|
+
}
|
|
38
|
+
}
|