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 3 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
12 changes: 8 additions & 4 deletions core/ecschema-metadata/src/test/Context/SchemaCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,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 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, describe, expect, it } from "vitest";
import { assert, describe, expect, it, vi } from "vitest";
import { CAProviderTuple } from "../../Deserialization/AbstractParser";
import {
ConstantProps, EntityClassProps, EnumerationPropertyProps, EnumerationProps, EnumeratorProps, InvertedUnitProps, MixinProps,
Expand Down Expand Up @@ -2802,7 +2802,12 @@ describe("XmlParser", () => {

const testClass = await getTestCAClass(propertyJson);
const providers = getCAProviders(itemXml);
sinon.stub(testClass!.schema, "lookupItemSync").withArgs("TestSchema.TestStringEnumeration").returns(undefined);
vi.spyOn(testClass!.schema, "lookupItemSync").mockImplementation((name) => {
if (name === "TestSchema.TestStringEnumeration") {
return undefined;
}
return testClass!.schema.lookupItemSync(name);
});

expect(() => providers[0][1](testClass!)).to.throw(ECObjectsError, `The Enumeration class 'TestSchema.TestStringEnumeration' could not be found.`);
});
Expand Down
9 changes: 5 additions & 4 deletions core/ecschema-metadata/src/test/Metadata/Class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ describe("ECClass", () => {
},
};

await expect(Schema.fromJson(schemaJson, new SchemaContext())).to.be.rejectedWith(ECObjectsError);
await expect(Schema.fromJson(schemaJson, new SchemaContext())).rejects.toThrow(ECObjectsError);
});

const oneCustomAttributeJson = {
Expand Down Expand Up @@ -668,7 +668,8 @@ describe("ECClass", () => {
},
};
it("async - Custom Attributes must be an array", async () => {
await expect(Schema.fromJson(mustBeAnArrayJson, new SchemaContext())).to.be.rejectedWith(ECObjectsError, `The ECClass TestSchema.testClass has an invalid 'customAttributes' attribute. It should be of type 'array'.`);
await expect(Schema.fromJson(mustBeAnArrayJson, new SchemaContext())).rejects.toThrow(ECObjectsError);
await expect(Schema.fromJson(mustBeAnArrayJson, new SchemaContext())).rejects.toThrow(`The ECClass TestSchema.testClass has an invalid 'customAttributes' attribute. It should be of type 'array'.`);
});
it("sync - Custom Attributes must be an array", async () => {
assert.throws(() => Schema.fromJsonSync(mustBeAnArrayJson, new SchemaContext()), ECObjectsError, `The ECClass TestSchema.testClass has an invalid 'customAttributes' attribute. It should be of type 'array'.`);
Expand Down Expand Up @@ -1247,7 +1248,7 @@ describe("ECClass", () => {
childClass.baseClass = new DelayedPromiseWithProps(baseClass!.key, async () => baseClass!);
(testSchema as MutableSchema).addItem(childClass);

await expect(childClass!.toXml(newDom)).to.be.rejectedWith(ECObjectsError, `The schema '${refSchema.name}' has an invalid alias.`);
await expect(childClass!.toXml(newDom)).rejects.toThrow(ECObjectsError, `The schema '${refSchema.name}' has an invalid alias.`);
}); */

it("Serialization with one custom attribute defined in ref schema, only class name", async () => {
Expand Down Expand Up @@ -1816,7 +1817,7 @@ describe("ECClass", () => {
],
});

await assert.isRejected(Schema.fromJson(json, new SchemaContext()), "The Navigation Property TestCA.testNavProp is invalid, because only EntityClasses, Mixins, and RelationshipClasses can have NavigationProperties.");
await expect(Schema.fromJson(json, new SchemaContext())).rejects.toThrow("The Navigation Property TestCA.testNavProp is invalid, because only EntityClasses, Mixins, and RelationshipClasses can have NavigationProperties.");
});

it("should throw synchronously", () => {
Expand Down
156 changes: 78 additions & 78 deletions core/ecschema-metadata/src/test/Validation/SchemaWalker.test.ts
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 { beforeEach, describe, expect, it } from "vitest";
import { beforeAll, describe, expect, it, vi } from "vitest";
import { SchemaContext } from "../../Context";
import { SchemaReadHelper } from "../../Deserialization/Helper";
import { JsonParser } from "../../Deserialization/JsonParser";
Expand Down Expand Up @@ -134,34 +134,32 @@ describe("SchemaWalker tests", () => {
},
};

type Mock<T> = { readonly [P in keyof T]: sinon.SinonSpy; };
let mockVisitor: Mock<ISchemaPartVisitor>;
const mockVisitor: ISchemaPartVisitor = {
visitClass: vi.fn(),
visitCustomAttributeContainer: vi.fn(),
visitProperty: vi.fn(),
visitRelationshipClass: vi.fn(),
visitRelationshipConstraint: vi.fn(),
visitEntityClass: vi.fn(),
visitStructClass: vi.fn(),
visitMixin: vi.fn(),
visitCustomAttributeClass: vi.fn(),
visitEnumeration: vi.fn(),
visitKindOfQuantity: vi.fn(),
visitPropertyCategory: vi.fn(),
visitUnit: vi.fn(),
visitInvertedUnit: vi.fn(),
visitUnitSystem: vi.fn(),
visitPhenomenon: vi.fn(),
visitFormat: vi.fn(),
visitConstant: vi.fn(),
visitFullSchema: vi.fn(),
};

beforeEach(async () => {
mockVisitor = {
visitClass: sinon.spy(),
visitCustomAttributeContainer: sinon.spy(),
visitProperty: sinon.spy(),
visitRelationshipClass: sinon.spy(),
visitRelationshipConstraint: sinon.spy(),
visitEntityClass: sinon.spy(),
visitStructClass: sinon.spy(),
visitMixin: sinon.spy(),
visitCustomAttributeClass: sinon.spy(),
visitEnumeration: sinon.spy(),
visitKindOfQuantity: sinon.spy(),
visitPropertyCategory: sinon.spy(),
visitUnit: sinon.spy(),
visitInvertedUnit: sinon.spy(),
visitUnitSystem: sinon.spy(),
visitPhenomenon: sinon.spy(),
visitFormat: sinon.spy(),
visitConstant: sinon.spy(),
visitFullSchema: sinon.spy(),
};
const context = new SchemaContext();
testSchema = new Schema(context);

const context = new SchemaContext();
testSchema = new Schema(context);
beforeAll(async () => {
const reader = new SchemaReadHelper(JsonParser, context);
testSchema = await reader.readSchema(testSchema, schemaJson);
});
Expand All @@ -171,92 +169,94 @@ describe("SchemaWalker tests", () => {
testSchema = await reader.traverseSchema(testSchema);
expect(testSchema).to.exist;

expect(mockVisitor!.visitFullSchema!.calledOnce).to.be.true;
expect(mockVisitor!.visitFullSchema!.calledWithExactly(testSchema)).to.be.true;
expect(mockVisitor!.visitFullSchema!.calledBefore(mockVisitor!.visitClass)).to.be.true;
expect(mockVisitor!.visitCustomAttributeContainer!.calledWithExactly(testSchema)).to.be.true;
expect(mockVisitor.visitFullSchema).toHaveBeenCalledOnce();
expect(mockVisitor.visitFullSchema).toHaveBeenCalledWith(testSchema);
expect(mockVisitor.visitCustomAttributeContainer).toHaveBeenCalledWith(testSchema);


const testEntityBase = await testSchema.getItem("TestEntityBase") as ECClass;
expect(mockVisitor!.visitClass!.calledWithExactly(testEntityBase)).to.be.true;
expect(mockVisitor!.visitCustomAttributeContainer!.calledWithExactly(testEntityBase)).to.be.true;
expect(mockVisitor!.visitEntityClass!.calledWithExactly(testEntityBase)).to.be.true;
expect(mockVisitor.visitClass).toHaveBeenCalledWith(testEntityBase);
expect(mockVisitor.visitCustomAttributeContainer).toHaveBeenCalledWith(testEntityBase);
expect(mockVisitor.visitEntityClass).toHaveBeenCalledWith(testEntityBase);

const props = [...testEntityBase.properties!];
const aProp = props[0];
const bProp = props[1];
expect(mockVisitor!.visitProperty!.calledTwice).to.be.true;
expect(mockVisitor!.visitProperty!.calledOnceWithExactly(aProp));
expect(mockVisitor!.visitProperty!.calledOnceWithExactly(bProp));
expect(mockVisitor!.visitCustomAttributeContainer!.calledWithExactly(aProp)).to.be.true;
expect(mockVisitor!.visitCustomAttributeContainer!.calledWithExactly(bProp)).to.be.true;
expect(mockVisitor.visitProperty).toHaveBeenCalledTimes(2);
expect(mockVisitor.visitProperty).toHaveBeenCalledWith(aProp);
expect(mockVisitor.visitProperty).toHaveBeenNthCalledWith(2, bProp);
expect(mockVisitor.visitCustomAttributeContainer).toHaveBeenCalledWith(aProp);
expect(mockVisitor.visitCustomAttributeContainer).toHaveBeenCalledWith(bProp);

const testEntityA = await testSchema.getItem("TestEntityA") as ECClass;
expect(mockVisitor!.visitClass!.calledWithExactly(testEntityA)).to.be.true;
expect(mockVisitor!.visitCustomAttributeContainer!.calledWithExactly(testEntityA)).to.be.true;
expect(mockVisitor!.visitEntityClass!.calledWithExactly(testEntityA)).to.be.true;
expect(mockVisitor.visitClass).toHaveBeenCalledWith(testEntityA);
expect(mockVisitor.visitCustomAttributeContainer).toHaveBeenCalledWith(testEntityA);
expect(mockVisitor.visitEntityClass).toHaveBeenCalledWith(testEntityA);

const testEntityB = await testSchema.getItem("TestEntityB") as ECClass;
expect(mockVisitor!.visitClass!.calledWithExactly(testEntityB)).to.be.true;
expect(mockVisitor!.visitCustomAttributeContainer!.calledWithExactly(testEntityB)).to.be.true;
expect(mockVisitor!.visitEntityClass!.calledWithExactly(testEntityB)).to.be.true;
expect(mockVisitor.visitClass).toHaveBeenCalledWith(testEntityB);
expect(mockVisitor.visitCustomAttributeContainer).toHaveBeenCalledWith(testEntityB);
expect(mockVisitor.visitEntityClass).toHaveBeenCalledWith(testEntityB);

const testRelationship = await testSchema.getItem("TestRelationship") as RelationshipClass;
expect(mockVisitor!.visitRelationshipClass!.calledWithExactly(testRelationship)).to.be.true;
expect(mockVisitor!.visitCustomAttributeContainer!.calledWithExactly(testRelationship)).to.be.true;
expect(mockVisitor!.visitRelationshipConstraint!.calledWithExactly(testRelationship.source)).to.be.true;
expect(mockVisitor!.visitRelationshipConstraint!.calledWithExactly(testRelationship.target)).to.be.true;
expect(mockVisitor!.visitCustomAttributeContainer!.calledWithExactly(testRelationship.source)).to.be.true;
expect(mockVisitor!.visitCustomAttributeContainer!.calledWithExactly(testRelationship.target)).to.be.true;
expect(mockVisitor.visitClass).toHaveBeenCalledWith(testRelationship);
expect(mockVisitor.visitRelationshipClass).toHaveBeenCalledWith(testRelationship);
expect(mockVisitor.visitCustomAttributeContainer).toHaveBeenCalledWith(testRelationship);
expect(mockVisitor.visitRelationshipConstraint).toHaveBeenCalledTimes(2);
expect(mockVisitor.visitRelationshipConstraint).toHaveBeenCalledWith(testRelationship.source);
expect(mockVisitor.visitRelationshipConstraint).toHaveBeenCalledWith(testRelationship.target);
expect(mockVisitor.visitCustomAttributeContainer).toHaveBeenCalledWith(testRelationship.source);
expect(mockVisitor.visitCustomAttributeContainer).toHaveBeenCalledWith(testRelationship.target);

const testStruct = await testSchema.getItem("TestStruct") as ECClass;
expect(mockVisitor!.visitClass!.calledWithExactly(testStruct)).to.be.true;
expect(mockVisitor!.visitCustomAttributeContainer!.calledWithExactly(testStruct)).to.be.true;
expect(mockVisitor!.visitStructClass!.calledWithExactly(testStruct)).to.be.true;
expect(mockVisitor.visitClass).toHaveBeenCalledWith(testStruct);
expect(mockVisitor.visitCustomAttributeContainer).toHaveBeenCalledWith(testStruct);
expect(mockVisitor.visitStructClass).toHaveBeenCalledWith(testStruct);

const testMixin = await testSchema.getItem("TestMixin") as ECClass;
expect(mockVisitor!.visitClass!.calledWithExactly(testMixin)).to.be.true;
expect(mockVisitor!.visitCustomAttributeContainer!.calledWithExactly(testMixin)).to.be.true;
expect(mockVisitor!.visitMixin!.calledWithExactly(testMixin)).to.be.true;
expect(mockVisitor.visitClass).toHaveBeenCalledWith(testMixin);
expect(mockVisitor.visitCustomAttributeContainer).toHaveBeenCalledWith(testMixin);
expect(mockVisitor.visitMixin).toHaveBeenCalledWith(testMixin);

const testCAClass = await testSchema.getItem("TestCAClass") as ECClass;
expect(mockVisitor!.visitClass!.calledWithExactly(testCAClass)).to.be.true;
expect(mockVisitor!.visitCustomAttributeContainer!.calledWithExactly(testCAClass)).to.be.true;
expect(mockVisitor!.visitCustomAttributeClass!.calledWithExactly(testCAClass)).to.be.true;
expect(mockVisitor.visitClass).toHaveBeenCalledWith(testCAClass);
expect(mockVisitor.visitCustomAttributeContainer).toHaveBeenCalledWith(testCAClass);
expect(mockVisitor.visitCustomAttributeClass).toHaveBeenCalledWith(testCAClass);

const testEnum = await testSchema.getItem("TestEnum");
expect(mockVisitor!.visitEnumeration!.calledOnce).to.be.true;
expect(mockVisitor!.visitEnumeration!.calledWithExactly(testEnum)).to.be.true;
expect(mockVisitor.visitEnumeration).toHaveBeenCalledOnce();
expect(mockVisitor.visitEnumeration).toHaveBeenCalledWith(testEnum);

const testCategory = await testSchema.getItem("TestCategory");
expect(mockVisitor!.visitPropertyCategory!.calledOnce).to.be.true;
expect(mockVisitor!.visitPropertyCategory!.calledWithExactly(testCategory)).to.be.true;
expect(mockVisitor.visitPropertyCategory).toHaveBeenCalledOnce();
expect(mockVisitor.visitPropertyCategory).toHaveBeenCalledWith(testCategory);

const testKoq = await testSchema.getItem("TestKoQ");
expect(mockVisitor!.visitKindOfQuantity!.calledOnce).to.be.true;
expect(mockVisitor!.visitKindOfQuantity!.calledWithExactly(testKoq)).to.be.true;
expect(mockVisitor.visitKindOfQuantity).toHaveBeenCalledOnce();
expect(mockVisitor.visitKindOfQuantity).toHaveBeenCalledWith(testKoq);

const testUnitSystem = await testSchema.getItem("Metric");
expect(mockVisitor!.visitUnitSystem!.calledOnce).to.be.true;
expect(mockVisitor!.visitUnitSystem!.calledWithExactly(testUnitSystem)).to.be.true;
expect(mockVisitor.visitUnitSystem).toHaveBeenCalledOnce();
expect(mockVisitor.visitUnitSystem).toHaveBeenCalledWith(testUnitSystem);

const testUnit = await testSchema.getItem("M");
expect(mockVisitor!.visitUnit!.calledOnce).to.be.true;
expect(mockVisitor!.visitUnit!.calledWithExactly(testUnit)).to.be.true;
expect(mockVisitor.visitUnit).toHaveBeenCalledOnce();
expect(mockVisitor.visitUnit).toHaveBeenCalledWith(testUnit);

const testInvertedUnit = await testSchema.getItem("TestInvertedUnit");
expect(mockVisitor!.visitInvertedUnit!.calledOnce).to.be.true;
expect(mockVisitor!.visitInvertedUnit!.calledWithExactly(testInvertedUnit)).to.be.true;
expect(mockVisitor.visitInvertedUnit).toHaveBeenCalledOnce();
expect(mockVisitor.visitInvertedUnit).toHaveBeenCalledWith(testInvertedUnit);

const testPhenomenon = await testSchema.getItem("Length");
expect(mockVisitor!.visitPhenomenon!.calledOnce).to.be.true;
expect(mockVisitor!.visitPhenomenon!.calledWithExactly(testPhenomenon)).to.be.true;
expect(mockVisitor.visitPhenomenon).toHaveBeenCalledOnce();
expect(mockVisitor.visitPhenomenon).toHaveBeenCalledWith(testPhenomenon);

const testFormat = await testSchema.getItem("TestFormat");
expect(mockVisitor!.visitFormat!.calledOnce).to.be.true;
expect(mockVisitor!.visitFormat!.calledWithExactly(testFormat)).to.be.true;
expect(mockVisitor.visitFormat).toHaveBeenCalledOnce();
expect(mockVisitor.visitFormat).toHaveBeenCalledWith(testFormat);

const testConstant = await testSchema.getItem("TestConstant");
expect(mockVisitor!.visitConstant!.calledOnce).to.be.true;
expect(mockVisitor!.visitConstant!.calledWithExactly(testConstant)).to.be.true;
expect(mockVisitor.visitConstant).toHaveBeenCalledOnce();
expect(mockVisitor.visitConstant).toHaveBeenCalledWith(testConstant);
});
});
24 changes: 24 additions & 0 deletions core/ecschema-metadata/vitest.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { coverageConfigDefaults, defineConfig } from 'vitest/config';
export default defineConfig({
test: {
dir: "src",
coverage: {
provider: "v8",
include: [
"src/**/*"
],
exclude: [
...coverageConfigDefaults.exclude,
"src/test/**/*",
"**/*.d.ts",
"**/*.d.tsx"
],
reporter: [
"text-summary",
"lcov",
"cobertura"
],
reportsDirectory: "./lib/cjs/test/coverage",
}
}
})
Loading