Skip to content

Commit

Permalink
docs: add with firestore docs page
Browse files Browse the repository at this point in the history
  • Loading branch information
thekip committed Jan 15, 2024
1 parent dfcd6d1 commit 343679a
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 0 deletions.
67 changes: 67 additions & 0 deletions website/versioned_docs/version-29.4/Firestore.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
id: firestore
title: Using with Firestore
---

With Jest's [Global Setup/Teardown](Configuration.md#globalsetup-string) and [Async Test Environment](Configuration.md#testenvironment-string) APIs, the developer experience for [Firestore](https://firebase.google.com/docs/firestore) is improved compared to using the `firebase emulators:exec` command.

## Use jest-firestore Preset

[Jest Firestore](https://github.com/thekip/jest-firestore) provides all required configuration to run your tests using Firestore Emulator.

1. First install `@thekip/jest-firestore`

```bash npm2yarn
npm install --save-dev @thekip/jest-firestore
```

2. Specify preset in your Jest configuration:

```json
{
"preset": "jest-firestore"
}
```

3. Write your test

```ts
import { type Firestore, getFirestore } from 'firebase-admin/firestore';
import { initializeApp } from 'firebase-admin/app';

describe('insert', () => {
let firestore: Firestore;
// setup deffiernet database for each jest worker to enable parallelism
const databaseName = 'test-' + process.pid;

Check failure on line 35 in website/versioned_docs/version-29.4/Firestore.md

View workflow job for this annotation

GitHub Actions / Lint

Unexpected string concatenation

beforeAll(() => {
// `firebase-admin` automatically discover
// FIRESTORE_EMULATOR_HOST provided by jest-firestore
const app = initializeApp();
firestore = getFirestore(app, databaseName);
});

beforeEach(async () => {
// clear database before each test
await fetch(
`http://${process.env.FIRESTORE_EMULATOR_HOST}/emulator/v1/projects/${process.env.GCLOUD_PROJECT}/databases/${databaseName}/documents`,
{ method: 'DELETE' },
);
});

it('should insert a doc into collection', async () => {
const users = firestore.collection('users');

const mockUser = { _id: 'some-user-id', name: 'John' };
const { id } = await users.add(mockUser);

const insertedUser = await users.doc(id).get();

expect(insertedUser.data()).toEqual(mockUser);
});
});
```

There's no need to load any dependencies.

See [documentation](https://github.com/thekip/jest-firestore) for details.
67 changes: 67 additions & 0 deletions website/versioned_docs/version-29.5/Firestore.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
id: firestore
title: Using with Firestore
---

With Jest's [Global Setup/Teardown](Configuration.md#globalsetup-string) and [Async Test Environment](Configuration.md#testenvironment-string) APIs, the developer experience for [Firestore](https://firebase.google.com/docs/firestore) is improved compared to using the `firebase emulators:exec` command.

## Use jest-firestore Preset

[Jest Firestore](https://github.com/thekip/jest-firestore) provides all required configuration to run your tests using Firestore Emulator.

1. First install `@thekip/jest-firestore`

```bash npm2yarn
npm install --save-dev @thekip/jest-firestore
```

2. Specify preset in your Jest configuration:

```json
{
"preset": "jest-firestore"
}
```

3. Write your test

```ts
import { type Firestore, getFirestore } from 'firebase-admin/firestore';
import { initializeApp } from 'firebase-admin/app';

describe('insert', () => {
let firestore: Firestore;
// setup deffiernet database for each jest worker to enable parallelism
const databaseName = 'test-' + process.pid;

Check failure on line 35 in website/versioned_docs/version-29.5/Firestore.md

View workflow job for this annotation

GitHub Actions / Lint

Unexpected string concatenation

beforeAll(() => {
// `firebase-admin` automatically discover
// FIRESTORE_EMULATOR_HOST provided by jest-firestore
const app = initializeApp();
firestore = getFirestore(app, databaseName);
});

beforeEach(async () => {
// clear database before each test
await fetch(
`http://${process.env.FIRESTORE_EMULATOR_HOST}/emulator/v1/projects/${process.env.GCLOUD_PROJECT}/databases/${databaseName}/documents`,
{ method: 'DELETE' },
);
});

it('should insert a doc into collection', async () => {
const users = firestore.collection('users');

const mockUser = { _id: 'some-user-id', name: 'John' };
const { id } = await users.add(mockUser);

const insertedUser = await users.doc(id).get();

expect(insertedUser.data()).toEqual(mockUser);
});
});
```

There's no need to load any dependencies.

See [documentation](https://github.com/thekip/jest-firestore) for details.
67 changes: 67 additions & 0 deletions website/versioned_docs/version-29.6/Firestore.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
id: firestore
title: Using with Firestore
---

With Jest's [Global Setup/Teardown](Configuration.md#globalsetup-string) and [Async Test Environment](Configuration.md#testenvironment-string) APIs, the developer experience for [Firestore](https://firebase.google.com/docs/firestore) is improved compared to using the `firebase emulators:exec` command.

## Use jest-firestore Preset

[Jest Firestore](https://github.com/thekip/jest-firestore) provides all required configuration to run your tests using Firestore Emulator.

1. First install `@thekip/jest-firestore`

```bash npm2yarn
npm install --save-dev @thekip/jest-firestore
```

2. Specify preset in your Jest configuration:

```json
{
"preset": "jest-firestore"
}
```

3. Write your test

```ts
import { type Firestore, getFirestore } from 'firebase-admin/firestore';
import { initializeApp } from 'firebase-admin/app';

describe('insert', () => {
let firestore: Firestore;
// setup deffiernet database for each jest worker to enable parallelism
const databaseName = 'test-' + process.pid;

Check failure on line 35 in website/versioned_docs/version-29.6/Firestore.md

View workflow job for this annotation

GitHub Actions / Lint

Unexpected string concatenation

beforeAll(() => {
// `firebase-admin` automatically discover
// FIRESTORE_EMULATOR_HOST provided by jest-firestore
const app = initializeApp();
firestore = getFirestore(app, databaseName);
});

beforeEach(async () => {
// clear database before each test
await fetch(
`http://${process.env.FIRESTORE_EMULATOR_HOST}/emulator/v1/projects/${process.env.GCLOUD_PROJECT}/databases/${databaseName}/documents`,
{ method: 'DELETE' },
);
});

it('should insert a doc into collection', async () => {
const users = firestore.collection('users');

const mockUser = { _id: 'some-user-id', name: 'John' };
const { id } = await users.add(mockUser);

const insertedUser = await users.doc(id).get();

expect(insertedUser.data()).toEqual(mockUser);
});
});
```

There's no need to load any dependencies.

See [documentation](https://github.com/thekip/jest-firestore) for details.
67 changes: 67 additions & 0 deletions website/versioned_docs/version-29.7/Firestore.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
id: firestore
title: Using with Firestore
---

With Jest's [Global Setup/Teardown](Configuration.md#globalsetup-string) and [Async Test Environment](Configuration.md#testenvironment-string) APIs, the developer experience for [Firestore](https://firebase.google.com/docs/firestore) is improved compared to using the `firebase emulators:exec` command.

## Use jest-firestore Preset

[Jest Firestore](https://github.com/thekip/jest-firestore) provides all required configuration to run your tests using Firestore Emulator.

1. First install `@thekip/jest-firestore`

```bash npm2yarn
npm install --save-dev @thekip/jest-firestore
```

2. Specify preset in your Jest configuration:

```json
{
"preset": "jest-firestore"
}
```

3. Write your test

```ts
import { type Firestore, getFirestore } from 'firebase-admin/firestore';
import { initializeApp } from 'firebase-admin/app';

describe('insert', () => {
let firestore: Firestore;
// setup deffiernet database for each jest worker to enable parallelism
const databaseName = 'test-' + process.pid;

Check failure on line 35 in website/versioned_docs/version-29.7/Firestore.md

View workflow job for this annotation

GitHub Actions / Lint

Unexpected string concatenation

beforeAll(() => {
// `firebase-admin` automatically discover
// FIRESTORE_EMULATOR_HOST provided by jest-firestore
const app = initializeApp();
firestore = getFirestore(app, databaseName);
});

beforeEach(async () => {
// clear database before each test
await fetch(
`http://${process.env.FIRESTORE_EMULATOR_HOST}/emulator/v1/projects/${process.env.GCLOUD_PROJECT}/databases/${databaseName}/documents`,
{ method: 'DELETE' },
);
});

it('should insert a doc into collection', async () => {
const users = firestore.collection('users');

const mockUser = { _id: 'some-user-id', name: 'John' };
const { id } = await users.add(mockUser);

const insertedUser = await users.doc(id).get();

expect(insertedUser.data()).toEqual(mockUser);
});
});
```

There's no need to load any dependencies.

See [documentation](https://github.com/thekip/jest-firestore) for details.
1 change: 1 addition & 0 deletions website/versioned_sidebars/version-29.4-sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"puppeteer",
"mongodb",
"dynamodb",
"firestore",
"tutorial-jquery",
"watch-plugins",
"migration-guide",
Expand Down
1 change: 1 addition & 0 deletions website/versioned_sidebars/version-29.5-sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"puppeteer",
"mongodb",
"dynamodb",
"firestore",
"tutorial-jquery",
"watch-plugins",
"migration-guide",
Expand Down
1 change: 1 addition & 0 deletions website/versioned_sidebars/version-29.6-sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"puppeteer",
"mongodb",
"dynamodb",
"firestore",
"tutorial-jquery",
"watch-plugins",
"migration-guide",
Expand Down
1 change: 1 addition & 0 deletions website/versioned_sidebars/version-29.7-sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"puppeteer",
"mongodb",
"dynamodb",
"firestore",
"tutorial-jquery",
"watch-plugins",
"migration-guide",
Expand Down

0 comments on commit 343679a

Please sign in to comment.