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

Introduce vitest to core/ecschema-metadata #7298

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@
"cwd": "${workspaceFolder}/core/ecschema-metadata",
"type": "node",
"request": "launch",
"runtimeExecutable": "npm",
"runtimeExecutable": "pnpm",
"runtimeArgs": [
"test"
],
"outFiles": [
"${workspaceFolder}/core/*/lib/**/*.js"
]
"autoAttachChildProcesses": true,
"smartStep": true,
"console": "integratedTerminal",
},
{
"name": "EC Schema Locater Tests",
Expand Down Expand Up @@ -1123,7 +1123,10 @@
"group": "0_CoreTests",
"order": 2
},
"configurations": ["Attach to Vitest Browser", "Run Vitest Browser"],
"configurations": [
"Attach to Vitest Browser",
"Run Vitest Browser"
],
"stopAll": true
},
{
Expand Down
22 changes: 4 additions & 18 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 6 additions & 16 deletions core/ecschema-metadata/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
"copy:test-assets": "cpx \"./src/test/assets/**/*\" ./lib/cjs/test/assets",
"extract-api": "betools extract-api --entry=ecschema-metadata",
"lint": "eslint \"./src/**/*.ts\" 1>&2",
"test": "mocha",
"test": "vitest --run",
"docs": "betools docs --includes=../../generated-docs/extract --json=../../generated-docs/core/ecschema-metadata/file.json --tsIndexFile=./ecschema-metadata.ts --onlyJson",
"cover": "nyc npm -s test",
"cover": "vitest --run --coverage",
"start": "npm run -s lint && npm run -s clean && npm run -s build && npm run -s test & npm run -s cover & npm run -s docs"
},
"keywords": [
Expand All @@ -42,28 +42,18 @@
"@itwin/core-quantity": "workspace:*",
"@itwin/eslint-plugin": "5.0.0-dev.1",
"@types/benchmark": "^2.1.0",
"@types/chai": "4.3.1",
"@types/chai-as-promised": "^7",
"@types/mocha": "^10.0.6",
"@types/node": "~18.16.20",
"@types/sinon": "^17.0.2",
"@vitest/coverage-v8": "^2.1.0",
"@xmldom/xmldom": "~0.8.5",
"benchmark": "^2.1.4",
"chai": "^4.3.10",
"chai-as-promised": "^7.1.1",
"cpx2": "^3.0.0",
"eslint": "^9.13.0",
"mocha": "^10.2.0",
"nyc": "^15.1.0",
"rimraf": "^3.0.2",
"sinon": "^17.0.2",
"typescript": "~5.6.2"
"typescript": "~5.6.2",
"vitest": "^2.1.0"
},
"peerDependencies": {
"@itwin/core-bentley": "workspace:^4.10.0-dev.33",
"@itwin/core-quantity": "workspace:^4.10.0-dev.33"
},
"nyc": {
"extends": "./node_modules/@itwin/build-tools/.nycrc"
}
}
}
26 changes: 12 additions & 14 deletions core/ecschema-metadata/src/test/Context/SchemaCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,12 @@
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/

import * as chai from "chai";
import * as chaiAsPromised from "chai-as-promised";
import { assert, describe, expect, it } from "vitest";
import { SchemaCache, SchemaContext } from "../../Context";
import { ECObjectsError } from "../../Exception";
import { Schema } from "../../Metadata/Schema";
import { SchemaKey } from "../../SchemaKey";

const assert = chai.assert;
const expect = chai.expect;

chai.use(chaiAsPromised);

describe("Schema Cache", () => {
it("adding should succeed", async () => {
const cache = new SchemaCache();
Expand All @@ -32,16 +26,20 @@ describe("Schema Cache", () => {
await cache.addSchema(schema1);

const schema2 = new Schema(context, new SchemaKey("TestSchema"), "ts");
await expect(cache.addSchema(schema2)).to.be.rejectedWith(ECObjectsError, "The schema, TestSchema.00.00.00, already exists within this cache.");
await expect(cache.addSchema(schema2)).rejects.toThrow(ECObjectsError);
await expect(cache.addSchema(schema2)).rejects.toThrow("The schema, TestSchema.00.00.00, already exists within this cache.");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hl662 is this the way to do it? This will call addSchema twice just to verify the message of the Error?


const schema3 = new Schema(context, new SchemaKey("TestSchema", 1), "ts");
await expect(cache.addSchema(schema3)).to.be.rejectedWith(ECObjectsError, "The schema, TestSchema.01.00.00, already exists within this cache.");
await expect(cache.addSchema(schema3)).rejects.toThrow(ECObjectsError);
await expect(cache.addSchema(schema3)).rejects.toThrow("The schema, TestSchema.01.00.00, already exists within this cache.");

const schema4 = new Schema(context, new SchemaKey("TestSchema", 1, 0), "ts");
await expect(cache.addSchema(schema4)).to.be.rejectedWith(ECObjectsError, "The schema, TestSchema.01.00.00, already exists within this cache.");
await expect(cache.addSchema(schema4)).rejects.toThrow(ECObjectsError);
await expect(cache.addSchema(schema4)).rejects.toThrow("The schema, TestSchema.01.00.00, already exists within this cache.");

const schema5 = new Schema(context, "TestSchema", "ts", 1, 0, 0);
await expect(cache.addSchema(schema5)).to.be.rejectedWith(ECObjectsError, "The schema, TestSchema.01.00.00, already exists within this cache.");
await expect(cache.addSchema(schema5)).rejects.toThrow(ECObjectsError);
await expect(cache.addSchema(schema5)).rejects.toThrow("The schema, TestSchema.01.00.00, already exists within this cache.");
});

it("getAllSchemas should return added schemas", async () => {
Expand All @@ -55,8 +53,8 @@ describe("Schema Cache", () => {
await cache.addSchema(schema2);

const schemas = cache.getAllSchemas();
expect(schemas.length).to.equal(2);
expect(schemas[0].schemaKey.matches(schema1.schemaKey)).to.be.true;
expect(schemas[1].schemaKey.matches(schema2.schemaKey)).to.be.true;
expect(schemas.length).toEqual(2);
expect(schemas[0].schemaKey.matches(schema1.schemaKey)).toBe(true);
expect(schemas[1].schemaKey.matches(schema2.schemaKey)).toBe(true);
});
});
30 changes: 12 additions & 18 deletions core/ecschema-metadata/src/test/Context/SchemaContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/

import * as chai from "chai";
import * as chaiAsPromised from "chai-as-promised";
import { assert, describe, expect, it } from "vitest";
import { SchemaCache, SchemaContext } from "../../Context";
import { SchemaMatchType } from "../../ECObjects";
import { ECObjectsError } from "../../Exception";
import { Schema } from "../../Metadata/Schema";
import { SchemaKey } from "../../SchemaKey";

const assert = chai.assert;
const expect = chai.expect;

chai.use(chaiAsPromised);

describe("Schema Context", () => {
it("should succeed locating added schema", async () => {
const context = new SchemaContext();
Expand Down Expand Up @@ -46,7 +40,7 @@ describe("Schema Context", () => {
const schema2 = new Schema(context, "TestSchema", "ts", 1, 0, 5);

await context.addSchema(schema);
await expect(context.addSchema(schema2)).to.be.rejectedWith(ECObjectsError);
await expect(context.addSchema(schema2)).rejects.toThrow(ECObjectsError);
});

it("schema added, getCachedSchema returns the schema", async () => {
Expand All @@ -58,7 +52,7 @@ describe("Schema Context", () => {
const testKey = new SchemaKey("TestSchema", 1, 5, 9);
const loadedSchema = await context.getCachedSchema(testKey);

expect(loadedSchema).to.equal(schema);
expect(loadedSchema).toEqual(schema);
});

it("schema not added, getCachedSchema returns undefined", async () => {
Expand Down Expand Up @@ -90,7 +84,7 @@ describe("Schema Context", () => {
const testKey = new SchemaKey("TestSchema", 1, 5, 8);
const loadedSchema = await context.getCachedSchema(testKey, SchemaMatchType.LatestReadCompatible);

expect(loadedSchema).to.equal(schema);
expect(loadedSchema).toEqual(schema);
});

it("schema added, getCachedSchema called with different schema version with compatible match type (default), returns true", async () => {
Expand All @@ -102,7 +96,7 @@ describe("Schema Context", () => {
const testKey = new SchemaKey("TestSchema", 1, 5, 8);
const loadedSchema = await context.getCachedSchema(testKey);

expect(loadedSchema).to.equal(schema);
expect(loadedSchema).toEqual(schema);
});

it("successfully finds schema from added locater", async () => {
Expand All @@ -113,18 +107,18 @@ describe("Schema Context", () => {
await cache.addSchema(schema);

context.addLocater(cache);
expect(await context.getSchema(schema.schemaKey)).to.equal(schema);
expect(await context.getSchema(schema.schemaKey, SchemaMatchType.Exact)).to.equal(schema);
expect(await context.getSchema(schema.schemaKey)).toEqual(schema);
expect(await context.getSchema(schema.schemaKey, SchemaMatchType.Exact)).toEqual(schema);

// Check if the schema is found if it is added to the cache after the cache is added as a locater
const cache2 = new SchemaCache();
context.addLocater(cache2);
const schema2 = new Schema(context, "TestSchema", "ts", 1, 0, 10);
await cache2.addSchema(schema2);
expect(await context.getSchema(schema2.schemaKey, SchemaMatchType.Exact)).to.equal(schema2);
expect(await context.getSchema(schema2.schemaKey, SchemaMatchType.Exact)).toEqual(schema2);

// We should still get TestSchema 1.0.5 for SchemaMatchType.Latest, since cache was added _before_ cache2
expect(await context.getSchema(schema2.schemaKey)).to.equal(schema);
expect(await context.getSchema(schema2.schemaKey)).toEqual(schema);
});

it("getKnownSchemas should return all schemas from schema cache", async () => {
Expand All @@ -137,8 +131,8 @@ describe("Schema Context", () => {
await context.addSchema(schema2);

const schemas = context.getKnownSchemas();
expect(schemas.length).to.equal(2);
expect(schemas[0].schemaKey.matches(schema1.schemaKey)).to.be.true;
expect(schemas[1].schemaKey.matches(schema2.schemaKey)).to.be.true;
expect(schemas.length).toEqual(2);
expect(schemas[0].schemaKey.matches(schema1.schemaKey)).toBe(true);
expect(schemas[1].schemaKey.matches(schema2.schemaKey)).toBe(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { assert } from "chai";
import { assert, describe, it } from "vitest";
import { SchemaLoader } from "../../SchemaLoader";
import { ECObjectsError } from "../../Exception";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/

import { assert } from "chai";
import { assert, beforeEach, describe, it } from "vitest";
import { JsonParser } from "../../Deserialization/JsonParser";
import { ECObjectsError } from "../../Exception";
import { createSchemaJsonWithItems } from "../TestUtils/DeserializationHelpers";
Expand Down
Loading
Loading