Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type updates #6036

Merged
merged 15 commits into from
Aug 14, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* File format: generates Realms with format v23 (reads and upgrades file format v5 or later for non-synced Realm, upgrades file format v10 or later for synced Realms).

### Internal
* Fix types in integration tests and added type checking to the lint command.
<!-- * Either mention core version or upgrade -->
<!-- * Using Realm Core vX.Y.Z -->
<!-- * Upgraded Realm Core from vX.Y.Z to vA.B.C -->
Expand Down
10 changes: 9 additions & 1 deletion integration-tests/tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@
"build": "wireit",
"start": "wireit",
"test": "wireit",
"lint": "eslint --ext .js,.ts .",
"lint": "wireit",
"coverage": "wireit",
"ci:coverage": "wireit"
},
"wireit": {
"lint": {
"command": "eslint --ext .js,.ts . && tsc --noEmit",
"dependencies": [
"../../packages/realm-network-transport:bundle",
"../../packages/realm:bundle",
"build-dependencies"
]
},
"build": {
"command": "tsc",
"dependencies": [
Expand Down
6 changes: 3 additions & 3 deletions integration-tests/tests/src/hooks/import-app-before.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function importAppBefore(config: AppConfig | { config: AppConfig }, sdkCo
return importAppBefore(config.config, sdkConfig);
}

before(importAppBefore.name, async function (this: Partial<AppContext> & Mocha.Context) {
before(importAppBefore.name, async function (this: AppContext & Mocha.Context) {
// Importing an app might take up to 5 minutes when the app has a MongoDB Atlas service enabled.
this.longTimeout();
if (this.app) {
Expand All @@ -50,7 +50,7 @@ export function importAppBefore(config: AppConfig | { config: AppConfig }, sdkCo
this.app = new Realm.App({ id: appId, baseUrl, ...sdkConfig });

// Extract the sync database name from the config
const databaseNames: string[] = config.services
const databaseNames: (string | undefined)[] = config.services
.filter(([service]) => service.type === mongodbServiceType)
.map(([service]) => {
if ("sync" in service.config) {
Expand All @@ -60,7 +60,7 @@ export function importAppBefore(config: AppConfig | { config: AppConfig }, sdkCo
}
})
.filter((name) => typeof name === "string");
if (databaseNames.length === 1) {
if (databaseNames.length === 1 && databaseNames[0]) {
this.databaseName = databaseNames[0];
} else if (databaseNames.length > 1) {
throw new Error("Expected at most 1 database name in the config");
Expand Down
10 changes: 4 additions & 6 deletions integration-tests/tests/src/hooks/open-realm-before.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//
////////////////////////////////////////////////////////////////////////////

import Realm from "realm";
import Realm, { User } from "realm";

import { openRealm, OpenRealmConfiguration } from "../utils/open-realm";

Expand All @@ -29,17 +29,15 @@ import { openRealm, OpenRealmConfiguration } from "../utils/open-realm";
* @returns Promise which resolves when complete
*/
export function openRealmHook(config: OpenRealmConfiguration = {}) {
return async function openRealmHandler(
this: Partial<RealmContext> & Partial<UserContext> & Mocha.Context,
): Promise<void> {
return async function openRealmHandler(this: Partial<RealmContext> & UserContext & Mocha.Context): Promise<void> {
this.longTimeout();
if (this.realm) {
throw new Error("Unexpected realm on context, use only one openRealmBefore per test");
} else {
this.closeRealm = async () => {
console.warn("🤷 Skipped closing a Realm that failed to open");
};
const { realm, config: actualConfig } = await openRealm(config, this.user);
const { realm, config: actualConfig } = await openRealm(config, this.user as unknown as User);
this.realm = realm;
this.closeRealm = async ({
clearTestState = true,
Expand All @@ -58,7 +56,7 @@ export function openRealmHook(config: OpenRealmConfiguration = {}) {
Realm.clearTestState();
}
if (reopen) {
const { realm } = await openRealm(actualConfig, this.user);
const { realm } = await openRealm(actualConfig, this.user as unknown as User);
this.realm = realm;
}
};
Expand Down
12 changes: 9 additions & 3 deletions integration-tests/tests/src/performance-tests/property-reads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//
////////////////////////////////////////////////////////////////////////////

import Realm from "realm";
import Realm, { ObjectSchema, PropertySchema } from "realm";

import { describePerformance } from "../utils/benchmark";

Expand Down Expand Up @@ -47,10 +47,16 @@ function describeTypeRead({ type, value, schema = [] }: TestParameters) {
const objectSchemaName = type + "Class";
const propertyName = type + "Prop";

const defaultSchema = {
const defaultSchema: ObjectSchema = {
name: objectSchemaName,
properties: {
[propertyName]: type,
[propertyName]:
typeof type === "object"
? type
: ({
type,
optional: true,
} as PropertySchema),
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const PersonSchema: Realm.ObjectSchema = {
},
};

export class Person extends Realm.Object<Person> implements IPerson {
export class Person extends Realm.Object<Person> {
_id!: Realm.BSON.ObjectId;
name!: string;
age!: number;
Expand Down Expand Up @@ -67,7 +67,7 @@ export const DogSchema: Realm.ObjectSchema = {
},
};

export class Dog extends Realm.Object<Dog> implements IDog {
export class Dog extends Realm.Object<Dog> {
_id!: Realm.BSON.ObjectId;
name!: string;
age!: number;
Expand Down
10 changes: 5 additions & 5 deletions integration-tests/tests/src/tests/credentials/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ describe.skipIf(environment.missingServer, "custom-function credentials", () =>
exports = async function (loginPayload) {
// Get a handle for the app.users collection
const users = context.services.get("mongodb").db("app").collection("users");

// Parse out custom data from the FunctionCredential

const { username, secret } = loginPayload;

if (secret !== "v3ry-s3cret") {
throw new Error("Ah ah ah, you didn't say the magic word");
}
// Query for an existing user document with the specified username

const user = await users.findOne({ username });

if (user) {
// If the user document exists, return its unique ID
return user._id.toString();
Expand Down
9 changes: 5 additions & 4 deletions integration-tests/tests/src/tests/dictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const EmbeddedChild = {
},
};

const DictTypedSchema = {
const DictTypedSchema: Realm.ObjectSchema = {
name: "TypedDictionary",
properties: {
dict1: { type: "dictionary", objectType: "Children" }, // dictionary of objects is nullable by default
Expand Down Expand Up @@ -363,15 +363,16 @@ describe("Dictionary", () => {
expect(stringifiedAndParsed).deep.equals({ dict: values });
});

// TODO: Unskip once https://github.com/realm/realm-core/issues/4805 is fixed
it.skip("throws a meaningful error if accessed after deletion", function (this: RealmContext) {
it("throws a meaningful error if accessed after deletion", function (this: RealmContext) {
this.realm.write(() => {
const item = this.realm.create<Item>("Item", {});
const dict = item.dict;
this.realm.delete(item);
expect(() => {
JSON.stringify(dict);
}).throws("Access to invalidated Dictionary object");
}).throws(
"Dictionary is no longer valid. Either the parent object was deleted or the containing Realm has been invalidated or closed.",
);
});
});

Expand Down
10 changes: 6 additions & 4 deletions integration-tests/tests/src/tests/iterators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ describe("Iterating", () => {
}).throws("Object type 'SomeOtherClass' not found in schema");
});

type CollectionContext = { collection: Collection<unknown, Person, [unknown, Person], unknown> } & RealmContext;
type OrderedCollectionContext = { collection: OrderedCollection<Person> } & RealmContext;
type CollectionContext = { collection: Realm.Collection<unknown, Person, [unknown, Person], unknown> } & RealmContext;
type OrderedCollectionContext = { collection: Realm.OrderedCollection<Person> } & RealmContext;

function ifOrderedCollectionIt(title: string, test: (collection: OrderedCollection<Person>) => void) {
function ifOrderedCollectionIt(title: string, test: (collection: Realm.OrderedCollection<Person>) => void) {
it(title, function (this: OrderedCollectionContext) {
const { collection } = this;
if (!(collection instanceof OrderedCollection)) {
Expand All @@ -124,7 +124,9 @@ describe("Iterating", () => {
});
}

function collectionBefore(getCollection: (realm: Realm) => Collection<unknown, Person, [unknown, Person], unknown>) {
function collectionBefore(
getCollection: (realm: Realm) => Realm.Collection<unknown, Person, [unknown, Person], unknown>,
) {
before(function (this: CollectionContext) {
this.collection = getCollection(this.realm);
});
Expand Down
Loading
Loading