GCloud
Install
| npm install @testcontainers/gcloud --save-dev
|
Examples
Firestore
These examples use the following libraries:
Choose an image from the container registry and substitute IMAGE
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | await using container = await new FirestoreEmulatorContainer(image).start();
const firestore = admin.initializeApp({ projectId: "test-project" }, `test-app-${randomUuid()}`).firestore();
firestore.settings({
host: container.getEmulatorEndpoint(),
ssl: false,
});
const collection = "test-collection";
const document = "test-doc";
const docRef = firestore.collection(collection).doc(document);
await docRef.set({ message: "Hello, Firestore!" });
const snapshot = await docRef.get();
expect(snapshot.data()).toEqual({ message: "Hello, Firestore!" });
|
Datastore
These examples use the following libraries:
Choose an image from the container registry and substitute IMAGE
.
1
2
3
4
5
6
7
8
9
10
11
12
13 | await using container = await new DatastoreEmulatorContainer(image).start();
const datastore = new Datastore({
projectId: "test-project",
apiEndpoint: container.getEmulatorEndpoint(),
});
const key = datastore.key(["test-kind", "123"]);
const data = { message: "Hello, Datastore!" };
await datastore.save({ key, data });
const [entity] = await datastore.get(key);
expect(entity).toEqual({ message: "Hello, Datastore!", [Datastore.KEY]: key });
|
Cloud PubSub
These examples use the following libraries:
Choose an image from the container registry and substitute IMAGE
.
| await using container = await new PubSubEmulatorContainer(IMAGE).start();
const pubSub = new PubSub({
projectId: "test-project",
apiEndpoint: container.getEmulatorEndpoint(),
});
const [createdTopic] = await pubSub.createTopic("test-topic");
expect(createdTopic.name).toContain("test-topic");
|
Cloud Storage
These examples use the following libraries:
Choose an image from the container registry and substitute IMAGE
.
| await using container = await new CloudStorageEmulatorContainer(IMAGE).start();
const storage = new Storage({
projectId: "test-project",
apiEndpoint: container.getExternalUrl(),
});
await storage.createBucket("test-bucket");
const [buckets] = await storage.getBuckets();
expect(buckets[0].name).toBe("test-bucket");
|
Cloud Spanner
These examples use the following libraries:
Choose an image from the container registry and substitute IMAGE
.
Connect via client
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | await using container = await new SpannerEmulatorContainer(IMAGE).withProjectId("test-project").start();
const spanner = new Spanner({
projectId: container.getProjectId(),
apiEndpoint: container.getHost(),
port: container.getGrpcPort(),
sslCreds: container.getSslCredentials(),
});
const admin = spanner.getInstanceAdminClient();
const [configs] = await admin.listInstanceConfigs({
parent: admin.projectPath(container.getProjectId()),
});
const expectedConfigName = admin.instanceConfigPath(container.getProjectId(), "emulator-config");
expect(configs.map((c) => c.name)).toContain(expectedConfigName);
|
Connect via environment
| process.env.SPANNER_EMULATOR_HOST = container.getEmulatorGrpcEndpoint();
const spanner = new Spanner({ projectId: container.getProjectId() });
|
Helper usage
1
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 | await using container = await new SpannerEmulatorContainer(IMAGE).start();
const helper = new SpannerEmulatorHelper(container);
const instanceId = "test-instance";
const databaseId = "test-db";
// create resources
await helper.createInstance(instanceId);
await helper.createDatabase(instanceId, databaseId);
const client = await helper.client();
// verify instance exists
const [instanceExists] = await client.instance(instanceId).exists();
expect(instanceExists).toBe(true);
// verify database exists
const [dbExists] = await client.instance(instanceId).database(databaseId).exists();
expect(dbExists).toBe(true);
// delete resources
await helper.deleteDatabase(instanceId, databaseId);
await helper.deleteInstance(instanceId);
// verify deletions
const [dbExistsAfter] = await client.instance(instanceId).database(databaseId).exists();
expect(dbExistsAfter).toBe(false);
const [instanceExistsAfter] = await client.instance(instanceId).exists();
expect(instanceExistsAfter).toBe(false);
|
BigQuery
These examples use the following libraries:
Choose an image from the container registry and substitute IMAGE
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 | await using container = await new BigQueryEmulatorContainer(IMAGE).start();
const bigQuery = new BigQuery({
projectId: container.getProjectId(),
apiEndpoint: container.getEmulatorEndpoint(),
});
const dataset = "test-dataset";
const table = "test-table";
const schema: TableSchema = { fields: [{ name: "message", type: "STRING" }] };
await bigQuery.dataset(dataset).create();
await bigQuery.dataset(dataset).table(table).create({ schema: schema });
await bigQuery
.dataset(dataset)
.table(table)
.insert([{ message: "Hello, BigQuery!" }]);
const [rows] = await bigQuery.dataset(dataset).table(table).getRows();
expect(rows).toEqual([{ message: "Hello, BigQuery!" }]);
|